Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 912
pdm LVW Avatar asked Nov 11 '18 08:11

pdm LVW


2 Answers

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.

like image 150
Forlanda Avatar answered Oct 14 '22 10:10

Forlanda


Use threading library :

from threading import Thread
T = Thread(target=playy) # create thread
T.start() # Launch created thread
like image 4
Calvin-Ruiz Avatar answered Oct 14 '22 12:10

Calvin-Ruiz