You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行蜂鸣测试命令时未播放音频反而返回菜单的问题排查

问题:多进程播放音频失效,直接跳过进程执行计时器逻辑

执行蜂鸣测试命令时,程序未播放音频就回到菜单,之后才运行计时器并打印提示,仿佛multiprocessing.Process()被完全跳过。尝试将多任务逻辑移至独立.py文件导入使用,问题依旧,但该独立文件单独运行正常。推测是主程序的条件判断或第三方函数干扰了代码执行。

主程序代码

import os, playsound, multiprocessing, time

cwd= os.getcwd()
def beep_test():
    audio= "".join((cwd+"\\BTsound.mp3"))
    playsound.playsound(audio,True)

def multitasking():
            sound_process = multiprocessing.Process(target=beep_test)
            sound_process.start()

            timer = 10

            while timer > 0 and sound_process.is_alive(): 
                time.sleep(1) 
                print(f"..{timer}") 
                timer -= 1

            if timer == 0: 
                sound_process.terminate()
                print("Time's up, runners must grab a water bottle and walk around. Do not sit down.")

comm_mode=input("""How would you like to input your commands?
1. Silent (Typed)
2. Prod (Speech Recognition)
""")
if __name__ == "__main__":
    if "1" in comm_mode:
        while True:
            try:
                message = input("Type 'ghost' to begin.\n")
            except:
                message = input("")
            if "ghost" in message:
                print("GHOST online")
                message = input("What do you need?\n")

                if "begin beep test" in message:
                    multitasking()
                            
    else:
        print("Don't worry about it, error's on 1.")

独立测试文件代码(单独运行正常)

import os, playsound, multiprocessing, time
cwd = os.getcwd()
def beep_test():
    audio= "".join((cwd+"\\beep-test-audio.mp3"))
    playsound.playsound(audio,True)

def thingy():
        sound_process = multiprocessing.Process(target=beep_test)
        sound_process.start()

        timer = 10

        while timer > 0 and sound_process.is_alive(): 
            time.sleep(1) 
            print(f"..{timer}") 
            timer -= 1

        if timer == 0: 
            sound_process.terminate()
            print("Time's up, runners must grab a water bottle and walk around. Do not sit down.")

print(__name__)
if __name__ == "__main__":
    
    thingy()

解决方案

1. 修复多进程启动的__main__保护问题

Windows系统下,multiprocessing启动子进程时会重新导入主模块,导致主程序顶层代码(如comm_mode=input(...))被重复执行,干扰子进程初始化。需将所有交互逻辑完全放入if __name__ == "__main__":块内:

import os, playsound, multiprocessing, time

cwd= os.getcwd()
def beep_test():
    audio= os.path.join(cwd, "BTsound.mp3")  # 用os.path.join替代字符串拼接,避免路径分隔符问题
    playsound.playsound(audio, True)

def multitasking():
    sound_process = multiprocessing.Process(target=beep_test)
    sound_process.start()

    timer = 10

    while timer > 0 and sound_process.is_alive(): 
        time.sleep(1) 
        print(f"..{timer}") 
        timer -= 1

    if timer == 0: 
        sound_process.terminate()
        print("Time's up, runners must grab a water bottle and walk around. Do not sit down.")

# 所有交互逻辑移至__main__块内
if __name__ == "__main__":
    comm_mode=input("""How would you like to input your commands?
1. Silent (Typed)
2. Prod (Speech Recognition)
""")
    if "1" in comm_mode:
        while True:
            try:
                message = input("Type 'ghost' to begin.\n")
            except:
                message = input("")
            if "ghost" in message:
                print("GHOST online")
                message = input("What do you need?\n")

                if "begin beep test" in message:
                    multitasking()
                            
    else:
        print("Don't worry about it, error's on 1.")

2. 统一音频文件路径

主程序使用BTsound.mp3,独立测试文件使用beep-test-audio.mp3,确认主程序的音频文件存在于当前工作目录,或直接使用绝对路径定位文件。

3. 处理playsound多进程兼容性

playsound在多进程环境下可能存在初始化问题,可在子进程中添加短延迟确保音频加载完成:

def beep_test():
    import time
    time.sleep(0.5)  # 给音频播放留初始化时间
    audio= os.path.join(cwd, "BTsound.mp3")
    playsound.playsound(audio, True)

内容的提问来源于stack exchange,提问作者Cassidy Mettraux

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 21:57:51