简体   繁体   English

Playsound 不适用于多处理

[英]Playsound does not work with multiprocessing

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.当我尝试单独使用sound()时,它工作得非常好,但是使用多处理它就不起作用了。

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.即使我添加p.join() ,代码一启动就完成了,没有声音播放,IDLE Shell 中也没有显示错误消息。

How can I fix this?我怎样才能解决这个问题?

Putting the execution code inside the "if" clause as below solved the problem on my computer.如下将执行代码放在“if”子句中解决了我电脑上的问题。 If this does not work for you, please explain the situation more in detail, eg how you are running the code, your OS, Python version, etc.如果这对您不起作用,请更详细地说明情况,例如您如何运行代码、您的操作系统、Python 版本等。

from multiprocessing import Process
from playsound import playsound

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM