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

Python语音交互项目运行报PlaysoundException错误求助

问题

运行Python语音交互项目时出现报错:

Exception has occurred: PlaysoundException Error 275 for command: open "welcome.mp3" alias playsound_0.0076682983304099395

项目代码如下:

from gtts import gTTS
import os
from playsound import playsound
from datetime import date
from datetime import datetime
playsound('welcome.mp3')
playsound('req.mp3')
# Initialize the recognizer
r = sr.Recognizer()
# Function to convert text to speech
#def SpeakText(command):6
# Initialize the engine
# engine = pyttsx3.init()
#engine.say(command)
#engine.runAndWait()
def hindi(cmmd):
    speak=gTTS(text=cmmd, lang="hi", slow=False)
    speak.save('vc.mp3')
    playsound('vc.mp3')
    os.remove('vc.mp3')
# Loop infinitely for user to speak
while(1):
    # Exception handling to handle exceptions at the runtime
    try:
        # use the microphone as source for input.
        with sr.Microphone() as source2:
            # wait for a second to let the recognizer adjust the energy threshold based on the surrounding noise level
            r.adjust_for_ambient_noise(source2, duration=0.2)
            #listens for the user's input
            audio2 = r.listen(source2)
            # Using google to recognize audio
            MyText = r.recognize_google(audio2 , language='hi-In')
            MyText = MyText.lower()
            print(MyText)
            hindi(MyText)
            if MyText=="आज क्या तारीख है":
                #today = date.today()
                #d1 = today.strftime("%d %b %Y")
                d1=datetime.now()
                a=d1.strftime("%d %b %Y %H:%M")
                #a=(f"{a}")
                print(a)
                hindi(a)
            if MyText=="आपको किसने बनाया":
                print("मुझ को बनाने वाले का नाम अंशुल कुमार है")
                hindi("मुझ को बनाने वाले का नाम अंशुल कुमार है")
            if MyText=="तुमको किसने बनाया":
                print("मुझ को बनाने वाले का नाम अंशुल कुमार है")
                hindi("मुझ को बनाने वाले का नाम अंशुल कुमार है")
    except sr.RequestError as e:
        print("Could not request results; {0}".format(e))
    except sr.UnknownValueError:
        print("...........")

解决方案

Error 275是playsound库在Windows环境调用WinMM API时的常见问题,核心原因集中在文件路径、库兼容性或文件占用上,按以下步骤排查:

  • 验证音频文件路径
    确认welcome.mp3和req.mp3完全存在于项目的当前工作目录中。如果是邮件接收的项目,可能存在附件遗漏或解压路径错误,可先用绝对路径测试:

    # 替换为你实际的文件路径
    playsound(r'C:\Users\你的用户名\Desktop\项目文件夹\welcome.mp3')
    
  • 回退playsound版本
    新版playsound存在Windows兼容性问题,卸载当前版本后安装稳定旧版:

    pip uninstall playsound
    pip install playsound==1.2.2
    
  • 避免文件占用冲突
    hindi函数中os.remove('vc.mp3')可能在音频未播放完成时执行,导致文件被占用。添加延迟等待播放结束:

    import time
    def hindi(cmmd):
        speak=gTTS(text=cmmd, lang="hi", slow=False)
        speak.save('vc.mp3')
        playsound('vc.mp3')
        time.sleep(1)  # 等待播放完成
        os.remove('vc.mp3')
    
  • 检查系统音频权限
    确认Python程序拥有访问音频设备的权限,Windows可在「设置-隐私和安全性-麦克风/音频」中开启对应权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 10:05:32