Home > Mobile >  Playsound does not work with multiprocessing
Playsound does not work with multiprocessing

Time:12-24

I want to use multiprocessing to play and stop a sound file at any time, but when I start the process no sound was played.

from multiprocessing import Process
from playsound import playsound

    
def sound():
    playsound('sound.mp3')


p = Process(target=sound)
p.start()

When I tried using sound() on its own it works perfectly fine, but with multiprocessing it doesn't work.

Even if I add p.join(), the code just finishes as soon as I start it, no sound gets played, and no error message is shown in the IDLE Shell.

How can I fix this?

CodePudding user response:

Putting the execution code inside the "if" clause as below solved the problem on my computer. If this does not work for you, please explain the situation more in detail, e.g. how you are running the code, your OS, Python version, etc.

from multiprocessing import Process
from playsound import playsound

    
def sound():
    playsound('sound.mp3')

if __name__ == "__main__":
    p = Process(target=sound)
    p.start()
  • Related