Search code examples
pythonpython-3.xmultithreadingpython-playsound

Python execute playsound in separate thread


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.


Solution

  • Use threading library :

    from threading import Thread
    T = Thread(target=playy) # create thread
    T.start() # Launch created thread