mp3play有时工作,有时不工作

2024-09-25 06:24:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用mp3play和google tts api播放机器人语音输出: 代码很简单,但有时是好的,有时是坏的:

def robot_voice(text):
    text=text.split()
    text='+'.join(text)
    url = "http://translate.google.com/translate_tts?tl=en&q="+text
    request = urllib2.Request(url)
    request.add_header('User-agent', 'Mozilla/5.0') 
    opener = urllib2.build_opener()

    f = open("data.mp3", "wb")
    f.write(opener.open(request).read())
    f.close()
    time.sleep(0.5)
    import mp3play
    mp3=mp3play.load('data.mp3')
    mp3.play()
    time.sleep(mp3.seconds())
    mp3.stop()

if __name__=="__main__":
    robot_voice("hello world")

就像我把文件放在一个文件夹里时,它只会发出“你好,世界”的声音,但当我复制粘贴到另一个文件夹中时,会出现错误消息:

Error 277 for "open" data.mp3 "alias mp3_0.395468745176": problem occurred while initializing MCI. Error 263 for "set mp3_0.395468745176 time format milliseconds": The specified device is not open or is not recognized by MCI. Error 263 for "status mp3_0.395468745176 length": The specified device is not open or is not recognized by MCI.

Traceback (most recent call last): File "C:\Python27\Sample\synthesis\robot_voice.py", line 28, in robot_voice("hello world") File "C:\Python27\Sample\synthesis\robot_voice.py", line 19, in robot_voice mp3=mp3play.load('data.mp3') File "build\bdist.win-amd64\egg\mp3play__init__.py", line 10, in load return AudioClip(filename) File "build\bdist.win-amd64\egg\mp3play__init__.py", line 17, in init self._clip = _PlatformSpecificAudioClip(filename) File "build\bdist.win-amd64\egg\mp3play\windows.py", line 43, in init self._length_ms = int(buf) ValueError: invalid literal for int() with base 10: '\xd6\xb8\xb6\xa8\xb5\xc4\xc9\xe8\xb1\xb8\xce\xb4\xb4\xf2\xbf\xaa\xa3\xac\xbb\xf2\xb2\xbb\xb1\xbb MCI \xcb\xf9\xca\xb6\xb1\xf0\xa1\xa3'


Tags: textinpybuildfordataisline