-1

While, "strong_beat.play()" is in the while loop, it stops running

*import simpleaudio, time, math 

temp = 0
strong_beat = simpleaudio.WaveObject.from_wave_file('metronome2.wav')
while temp<20:
    strong_beat.play()
    time.sleep(0.5)
    temp = temp + 1
    print (temp)*

however if I remove it it continues to run properly

With:With the function

Without:Without the function

I don't know if there's a fundamental thing I don't understand, but the internet was not helping and I don't really know what to do.

I have limited knowledge of python, but I don't remember this being a problem in the past.

7
  • Are you sure that removing strong_beat.play() is the only change you made to this code? Perhaps you also made some other change that you didn't show? Commented May 27 at 23:05
  • Absolutely, I just tried again, making sure it's the only line of code removed and it produced the same results Commented May 27 at 23:11
  • I should note that strong_beat.play() is calling something in simpleaudio to play the wav file. It plays the audio, but doesn't repeat at all, seems like the function is interrupting the while statement Commented May 27 at 23:13
  • I can only guess that calling .play() on the same object twice isn't allowed, and causes your program to exit when you try. But it's odd that it would exit without any kind of error message. Commented May 27 at 23:20
  • That sounds possible, I'll look into that, I just tried to call a custom function and it ran properly. So it seems something is up with simple audio. I should have realized that sooner. Commented May 27 at 23:25

2 Answers 2

1

The audio is played asynchronously and is probably longer than the 0.5s delay you used, so this results in a silent failure when it is triggered again. Do what Adios Gringo said, use

play_obj.wait_done() 

or increase the 0.5s delay to more.

Sign up to request clarification or add additional context in comments.

2 Comments

I set the time delay to around 2 seconds and the audio is shorter than a second which resulted in the same problem. Every time I ran it it stopped at the timer, anything that was in the while statement after that never got executed. For some reason when I input ".wait_done()" it came up as an error of something that doesn't exist. It's really weird
- right after writing this I understand .wait_done(), it still just... ends using the wait_done line of code though
0

The reason for the failure is the combination of the python version and the latest version of the simpleaudio package.

Using python version 3.12.8 or 3.13.1 and simpleaudio 1.0.4 the following code fails with an "access violation" at line "play_obj.wait_done()".

The very same code (below) will work correctly if instead of using the simpleaudio package, use simpleaudio-312compat version 1.0.4. Note that the import line for simpleaudio stays the same.

import simpleaudio

strong_beat = simpleaudio.WaveObject.from_wave_file('hal2.wav')
temp = 0
while temp < 20:
    temp += 1
    play_obj = strong_beat.play()
    play_obj.wait_done()
    print("temp:", temp)

1 Comment

This worked perfectly!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.