I need to play a sound in my Python program so I used playsound module for that:
def playy():
playsound('beep.mp3')
How can I modify this to run inside main method as a new thread? I need to run this method inside the main method if a condition is true. When it is false the thread needs to stop.
You may not have to worry about using a thread. You can simply call playsound as follows:
def playy():
playsound('beep.mp3', block = False)
This will allow the program to keep running without waiting for the sound play to finish.
Use threading library :
from threading import Thread
T = Thread(target=playy) # create thread
T.start() # Launch created thread
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With