首页 > 解决方案 > 播放声音文件时 Python 3 权限错误(mp3,playsound 模块)

问题描述

该程序几天前运行良好,今天刚刚停止。没有一个字母被改变。我的故障排除步骤之一是删除文件'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 上运行良好。

标签: pythonpython-3.xpython-playsound

解决方案


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

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

这对我有用。


推荐阅读