播放声音文件(mp3、playsound 模块)时出现 Python 3 权限错误

新手上路,请多包涵

前几天程序还好好的,今天就停了。一个字母都没有改变。我的故障排除步骤之一是删除文件“output1.mp3”并检查它是否可以正常工作,但事实并非如此。另一件事是当它没有打印出错误时,它会继续播放这个声音文件,不管它说的是否正确。这是我得到的最新错误:

 Traceback (most recent call last):
  File "main3.py", line 123, in <module>
    start()
  File "main3.py", line 117, in start
    tts(say)
  File "main3.py", line 24, in tts
    play('output1.mp3')
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\playsound.py", line 35, in _playsoundWin
    winCommand('open "' + sound + '" alias', alias)
  File "C:\Program Files (x86)\Python36-32\lib\site-packages\playsound.py", line 31, in winCommand
    raise PlaysoundException(exceptionMessage)
playsound.PlaysoundException:
    Error 275 for command:
        open "output1.mp3" alias playsound_0.8842337577803419
    Cannot find the specified file.  Make sure the path and filename are correct.

这是我使用的代码:

 import boto3                               # used to 'pythonize' Amazon Polly TTS
import speech                              # speech recognition
from playsound import playsound as play    # for playing sound files
import sys                                 # basically only for exiting
#  import locator                          # for determining the location of the user based on IP address
import locator2                            # for confirming auto-detected location
#  import locator3                         # for definitely confirming auto-detection location
import question                            # module for answering any question

from datetime import datetime              # for displaying the date and time
#  from time import sleep                  # sleep (wai()t for) function
from os import popen as read               # for reading command outputs "read('COMMAND').read()"

def tts(text):
    polly = boto3.client("polly")
    spoken_text = polly.synthesize_speech(Text=str(text),
                                          OutputFormat='mp3',
                                          VoiceId='Brian')
    with open('output11.mp3', 'wb') as f:
        f.write(spoken_text['AudioStream'].read())
        f.close()
    play('output11.mp3')

def ask(query):
    question.question(query)
    response = question.answer
    print(response)
    tts(response)
    ai()

def time():
    now = datetime.now()
    print("Current date and time: ")
    print(now.strftime("%H:%M") + "\n")
    tts("It is " + now.strftime("%H:%M"))
    ai()

def weather():
    response = "Based on your IP address, I've detected that you're located in %s. Is that correct?" % locator2.city
    print(response)
    tts(response)
    speech.speech()
    if 'yes' in speech.x:
        z = read('weather "%s"' % locator2.city).read()
        print(z)
        tts(z)
        ai()
    else:
        response = 'Please say the name of the city you would like the weather information for. \n'
        print(response)
        tts(response)
        speech.speech()
        city = speech.x
        wdr = read('weather "%s"' % city).read()
        print(wdr)
        tts(wdr)
        ai()

def thank():
    response = "You're very welcome! \n"
    print(response)
    tts(response)
    ai()

def ext():
    response = "Goodbye!"
    print(response)
    tts(response)
    sys.exit()

def error():
    response = "Invalid request detected, please try again...\n"
    print(response)
    tts(response)
    ai()

def ai():
    print('Started listening - Speak!')
    speech.speech()
    spoken = speech.x

    # TODO new commands should be written above this, and their trigger words below :)
    question_words = ['?', 'what', 'where', 'when', 'who', 'how', 'why']

    if 'time' in spoken:
        time()

    elif 'weather' in spoken:
        weather()

    elif any(word in spoken for word in question_words):
        ask(spoken)

    elif 'thank' in spoken:
        thank()

    elif 'exit' or 'quit' or 'deactivate' in spoken:
        ext()

    else:
        error()

def start():
    say = "Hello! My name is Dave, and I'm your personal assistant. How may I help you today? \n"
    print(say)
    tts(say)
    ai()

if __name__ == '__main__':
    try:
        start()
    except KeyboardInterrupt:
        ext()

语音合成器是 Amazon Polly 。顺便说一句,我使用 PyCharm 作为 IDE 并在 Windows 10 上工作。当我切换到我的 Linux 机器时,语音识别部分中断了。

更新:我稍微调整了代码并设法修复了 pyaudio 错误,但我在这个过程中遇到了另一个错误,这次是关于权限的。这是错误日志:

 Traceback (most recent call last):
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 123, in <module>
    start()
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 118, in start
    ai()
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 96, in ai
    time()
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 39, in time
    tts("It is " + now.strftime("%H:%M"))
  File "C:/Users/Despot/Desktop/DAv3/main3.py", line 21, in tts
    with open('output11.mp3', 'wb') as f:
PermissionError: [Errno 13] Permission denied: 'output11.mp3'

更新 2 :我一直在纠结,我发现这个问题只出现在我的 Windows 10 机器上,该程序在 Linux 上运行良好。

原文由 Desamax1 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
1 个回答

尝试使用音频文件的绝对路径(完整路径)而不是相对路径。

例如:“C:/Users/Adam/Desktop/dolphin.wav”而不仅仅是“dolphin.wav”

这对我有用。

原文由 Martin Medro 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题