我正在尝试使用python中的gTTS(Google Text To Speach)功能,保存mp3文件有效(该文件正在保存并且可以播放)。
现在我正尝试使用以下代码直接播放文件,但它抛出错误
代码:
import gtts
import pyglet
import os
import time
text = ("Hello World")
obj = gtts.gTTS(text=text, lang='en')
speech_filename = 'c:/test_voice.mp3'
obj.save(speech_filename)
print("Play sound...")
music = pyglet.media.load(speech_filename, streaming=False)
music.play
sleep.time(music.duration) #prevent from killing
os.remove(speech_filename) #remove temp file
错误:
回溯(最近通话最近): 在第16行的文件“ C:\ python \ text-to-speach.py” 音乐= pyglet.media.load(speech_filename,stream = False)
文件“ C:\ Python \ lib \ site-packages \ pyglet \ media \ sources \ loader.py”,第63行,已加载 源= get_source_loader()。load(文件名,文件)
文件“ C:\ Python \ lib \ site-packages \ pyglet \ media \ sources \ loader.py”,第84行,已加载 返回WaveSource(文件名,文件)
init
中的文件“ C:\ Python \ lib \ site-packages \ pyglet \ media \ sources \ riff.py”,第200行“需要AVbin来解码压缩媒体”) pyglet.media.sources.riff.WAVEFormatException:需要AVbin才能解码压缩媒体
答案 0 :(得分:1)
它正在寻找AVbin,
检查以下内容对您有帮助
https://stackoverflow.com/questions/10302873/python-pyglet-avbin-how-to-install-avbin
答案 1 :(得分:0)
好的,这就是我的解决方法,我还添加了一个延迟循环,等待音频文件完成,然后将其删除。
import gtts
import pygame
#install pyglet and install http://avbin.github.io/AVbin/Download.html
#extract the avbin.dll from windows/system32/ folder to windows/system/ folder
import os
import time
pygame.mixer.init()
text = ("Hello, World")
obj = gtts.gTTS(text=text, lang='en')
speech_filename = "c:/python/code/test_voice.mp3"
obj.save(speech_filename)
print("Play sound...")
pygame.mixer.music.load(speech_filename)
pygame.mixer.music.play()
busy = True
while busy == True:
if pygame.mixer.music.get_busy() == False:
busy = False
pygame.quit()
os.remove(speech_filename) #remove temp file - remove line to keep file