[英]Python - Playsound gives a weird UTF-8 error when trying to play an mp3 or wav file
[英]Python 3 permission error when playing a sound file (mp3, playsound module)
该程序几天前运行良好,今天刚刚停止。 没有一个字母被改变。 我的故障排除步骤之一是删除文件“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.
这是我使用的代码,如果您需要我为您提供更多信息,请发表评论:
#!/usr/bin/env python3
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 上运行良好。
尝试使用音频文件的绝对路径(完整路径)而不是相对路径。
例如:“C:/Users/Adam/Desktop/dolphin.wav”而不是“dolphin.wav”
这对我有用。
Playsound 库中有一个 windows 目录。 如果这仅在 Linux 上失败,您应该在 linux 机器上安装 playsound lib,然后只将 main3.py 复制到它。
UPDATE2 的答案:将 output11.mp3 复制到另一个位置并将路径更改为新位置:
with open('CHANGE-THIS-PATH', 'wb') as f:
还要确保 python 以管理员身份运行。
在我开始学习 Playsound 软件包时,我遇到了类似的问题。 我正要运行 playsound 代码的基本第一步,即,
from playsound import playsound playsound('sample.wav')
注意:sample.wav 是我要播放的音频文件。
我试图运行它,但我遇到了与您之前遇到的相同的错误。 几分钟后,我将音频文件从 sample.wav 重命名为 sample,然后将其作为 playsound('sample.wav') 运行
幸运的是!它跑了。 然后我才知道以前它是存储为 sample.wav.wav
因此,让我们将您的音频文件名保留为 output.wav 并尝试将您的音频文件作为 playsound('output.wav.wav') 运行,或者将其重命名为 output 并作为 playsound('output.wav') 运行,对于其他音频格式也是如此也。
希望你得到你的答案!
我通过将 .py 和 .wav 文件移动到文件系统内部不太深的文件夹来解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.