Skip to content Skip to sidebar Skip to footer

Play Sound Asynchronously In Python

I have a while loop for my cameras(with opencv) to take a photos when something moves. I would like to call a function to play a sound as well. But when I call and play it, it will

Solution 1:

I don't know what requirements playsound has for the thread it runs on, but the simplest and easiest thing to do is probably just to spawn off a thread to play the sound:

import threading
def alert():
    threading.Thread(target=playsound, args=('ss.mp3',), daemon=True).start()

daemon=True here starts the thread as a daemon thread, meaning that it won't block the program from exiting. (On Python 2, you have do t = threading.Thread(...); t.daemon = True; t.start() instead.)


Post a Comment for "Play Sound Asynchronously In Python"