AudioPlayer
AudioPlayer copied to clipboard
Pause and stop issues
Hey,
Not sure if I'm doing something right, but I can't get the pause and stop functions to work properly
I'm writing:
def sendmsg(msg): if msg == "0": AudioPlayer("mixed.wav").play(block=True) print("play") elif msg == "111": AudioPlayer("mixed.wav").stop() print("stop")
(ignore the sendmsg function and the if else statements - part of the program I'm writing)
I'm able to get play working, but not stop and/or pause (have tried both) - had the code print messages so I know the rest of the program is working
Any clue what I may be doing wrong?
Ignore the lack of indentations - the code runs like I said, just messed up when copying it into github for this post
I would expect
AudioPlayer("mixed.wav").play(block=True)
to be blocking main thread until "mixed.wav" is done playing completely.
Also if I'm not wrong, calling multiple times
AudioPlayer("mixed.wav")
just opens the same file multiple times. You need to create a player (save a reference to it) and then implement your logic on an existing player.
Try:
import time
player = AudioPlayer("mixed.wav")
player.play(block=False)
# wait for 1 sec (given that mixed.wav is longer than 1 second)
time.sleep(1)
player.stop()
Got it to work, thanks. Just moved the "player = AudioPlayer("mixed.wav")" outside of the function because I think a new instance was being created each time the function was called, so it wasn't actually pausing the one that was being played. Not sure if that was the issue but either way it works now, thanks!
@dpinzon30 , check the effect of block argument on play func
block (bool) – If true, blocks the thread until playback ends.
I don't think you can pause when it's in blocking mode...
Is it a GUI application? I don't think it's a good idea to block the main thread if so.