写入音频文件时出现错误。
基本上,每当我的函数被调用然后播放时,我都会覆盖 mp3 中的数据。
它第一次通过,但后来给了我
[Errno 13] Permission denied: 'file.mp3'
然后。
这是代码:
def speech(self, response):
audio_file = "response.mp3"
tts = gTTS(text=str(response), lang="en")
tts.save(audio_file)
pygame.mixer.init()
pygame.mixer.music.load(audio_file)
pygame.mixer.music.play()
错误在
tts.save
线上。
更多信息:
Traceback (most recent call last):
File "myprojectpath", line 63, in speech
tts.save(audio_file)
File "C:\Python35\lib\site-packages\gtts\tts.py", line 93, in save
with open(savefile, 'wb') as f:
谢谢!
from gtts import gTTS
import playsound
import os
x = ['sunny', 'sagar', 'akhil']
tts = 'tts'
for i in range(0,3):
tts = gTTS(text= x[i], lang = 'en')
file1 = str("hello" + str(i) + ".mp3")
tts.save(file1)
playsound.playsound(file1,True)
print 'after'
os.remove(file1)
每次保存时更改文件名,这对我有用。
你每次都应该为你的文件命名一个唯一的名字,所以我意识到的解决方案是像这样用date命名你的文件==>
date_string = datetime.now().strftime("%d%m%Y%H%M%S")
filename = "voice"+date_string+".mp3"
tts.save(filename)
playsound.playsound(filename)
您可以在同一文件夹中创建另一个文件,只需复制并重命名即可。
您要使用的文件:("C:/.../file.mp3") 复制的文件:(“C:/.../file_copy.mp3”)
可以把mixer.music.load("C:/.../file.mp3")中加载的文件改成mixer.music.load("C:/.../file_copy.mp3")删除delete ("C:/.../file.mp3") 使用这个没有问题:
from pygame import mixer
import os
mixer.music.load("C:/.../file.mp3")
mixer.music.play()
mixer.music.load("C:/.../file_copy.mp3")
os.remove("C:/.../file.mp3")
但是上面的代码会在播放之前删除文件,所以你可以使用一种方法来等待它播放:
from pygame import mixer
import os
mixer.music.load("C:/.../file.mp3")
mixer.music.play()
while mixer.music.get_busy(): # check if the file is playing
pass
mixer.music.load("C:/.../file_copy.mp3")
os.remove("C:/.../file.mp3")
尝试保存fi打开其他文件whitout播放,然后你将有权删除它这里是谈话功能:
def Talk (mytext, language= "fr" ):
myobj = gTTS(text=mytext, lang=language, slow=False)
myobj.save("1.mp3")
mixer.init()
mixer.music.load("1.mp3")
mixer.music.set_volume(1)
mixer.music.play()
mixer.music.get_endevent()
while mixer.music.get_busy():
continue
mixer.music.load("Talker\empty.mp3")
os.remove("1.mp3")
你收到这个错误是因为混音器没有发布那个音频文件。如果您想混音器发布该文件,您可以将无用或空文件加载到混音器中。然后你可以覆盖那个音频文件。 例如..
Mixer.load("C:/...../empty.mp3")
在这个先前加载的文件被释放之后。
如果您不想再保存之前的文件,您可以在保存下一个文件之前删除音频文件。因此,您可以使用相同的名称保存新文件。 下面是我成功运行的代码
import speech_recognition as ram
from playsound import playsound
from gtts import gTTS
import os
r=ram.Recognizer()
with ram.Microphone() as source:
while True:
r.adjust_for_ambient_noise(source,duration=1)
print("I'm Listening....")
playsound('audio/listen.mp3')
audio=r.listen(source, timeout=5)
text=r.recognize_google(audio)
tts = gTTS("Boss. you said "+str(text)+". i'm opening it")
tts.save('audio/text.mp3')
print("you said "+text+".")
playsound('audio/text.mp3')
os.remove('audio/text.mp3')
一个更好的解决方案是,如果我有时间,我也会计算登陆相同文件的概率
from gtts import gTTS
from playsound import playsound
import random
import os
#creating a super random named file
r1 = random.randint(1,10000000)
r2 = random.randint(1,10000000)
randfile = str(r2)+"randomtext"+str(r1) +".mp3"
tts = gTTS(text='hey, STOP It!!', lang='en', slow=True)
tts.save(randfile)
playsound(randfile)
print(randfile)
os.remove(randfile)
我遇到了同样的问题,然后发现文件指向了之前的位置,所以我不得不释放它。它工作正常,重要的是它也不会创建 mp3 文件。如果你不释放它,它第一次可以正常工作并保存音频文件然后第二次出现错误。
from gtts import gTTS
import os
import playsound
def tts(text, lang):
file = gTTS(text = text, lang = lang)
file.save("audio/speak_func.mp3")
playsound.playsound('audio/speak_func.mp3', True)
os.remove("audio/speak_func.mp3")
我知道这篇文章已经很老了,但是抛出这个错误的原因是因为 mp3 文件被加载到 pygames 混音器中,这意味着你不能写入它。您可以简单地使用
pygame.mixer.music.unload()
方法在再次写入之前卸载文件。
def speak(text):
pygame.mixer.music.unload()
tts = gTTS(text, lang='en')
tts.save('s.mp3')
pygame.mixer.music.load("s.mp3")
pygame.mixer.music.play()
while pygame.mixer.get_busy():
continue