av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python如何播放還存在StringIO中的MP3?

瀏覽:119日期:2022-07-16 16:21:40

問(wèn)題描述

我嘗試?yán)冒俣日Z(yǔ)音云API來(lái)實(shí)現(xiàn)一個(gè)包含TTS的桌面服務(wù)。已經(jīng)成功獲取返回MP3,并緩存在cStringIO中,但是不知道如何播放。代碼播放拋出錯(cuò)誤。

參考了http://guzalexander.com/2012/... 中提到的pywave/pyaudio/pyglet等方法。

代碼如下:

#!/usr/bin/env python#encoding=utf-8 #import wave import pyaudioimport pygletimport urllib, urllib2, pycurl import base64 import json import cStringIOimport binascii## get access token by api key & secret key def get_token(): apiKey = 'xxxxxxxxxx' secretKey = 'yyyyyyyyyyyyyyyyyyyyy' auth_url = 'https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + apiKey + '&client_secret=' + secretKey;res = urllib2.urlopen(auth_url) json_data = res.read() return json.loads(json_data)[’access_token’] def dump_res(buf): #print buf pass ## post audio to server def use_cloud(token): fp = wave.open(’vad_0.wav’, ’rb’) nf = fp.getnframes() f_len = nf * 2 audio_data = fp.readframes(nf) cuid = 'xxxxxxxxxx' #my xiaomi phone MAC srv_url = ’http://vop.baidu.com/server_api’ + ’?cuid=’ + cuid + ’&token=’ + token http_header = [ ’Content-Type: audio/pcm; rate=8000’, ’Content-Length: %d’ % f_len ] c = pycurl.Curl() c.setopt(pycurl.URL, str(srv_url)) #curl doesn’t support unicode #c.setopt(c.RETURNTRANSFER, 1) c.setopt(c.HTTPHEADER, http_header) #must be list, not dict c.setopt(c.POST, 1) c.setopt(c.CONNECTTIMEOUT, 30) c.setopt(c.TIMEOUT, 30) c.setopt(c.WRITEFUNCTION, dump_res) c.setopt(c.POSTFIELDS, audio_data) c.setopt(c.POSTFIELDSIZE, f_len) c.perform() #pycurl.perform() has no return val def get_audio(token): cuid = '00030DAF5784' # MAC address text = '從前有座山,山上有個(gè)廟,廟里有個(gè)老和尚和小和尚,老和尚對(duì)小和尚說(shuō):' #baidu_url = 'http://tsn.baidu.com/text2audio?tex=' + urllib.urlencode(text) + '&lan=zh&cuid=' + cuid + '&ctp=1&tok=' + token baidu_url = 'http://tsn.baidu.com/text2audio?tex=' + urllib.quote(text) + '&lan=zh&cuid=' + cuid + '&ctp=1&tok=' + token print('URL:t{0}'.format(baidu_url)) buf = cStringIO.StringIO() c = pycurl.Curl() c.setopt(c.URL, str(baidu_url)) #curl doesn’t support unicode #c.setopt(c.HTTPHEADER, http_header) #must be list, not dict #c.setopt(c.GET, 1) c.setopt(c.WRITEFUNCTION, buf.write) c.setopt(c.CONNECTTIMEOUT, 30) c.setopt(c.TIMEOUT, 30) c.setopt(c.WRITEFUNCTION, dump_res) c.perform() #pycurl.perform() has no return val #print 'type:t{0}'.format(c.CONTENT_TYPE) print(c.getinfo(c.CONTENT_TYPE)) #print buf.getvalue() song = pyglet.media.load(buf) song.play() buf.close() if __name__ == '__main__': token = get_token() print('token:t{0}'.format(token)) #use_cloud(token) get_audio(token)

拋出錯(cuò)誤如下:

Traceback (most recent call last): File 'C:BOM_Plannersourcesoldering_assistant_mvp.py', line 87, in <module> get_audio(token) File 'C:BOM_Plannersourcesoldering_assistant_mvp.py', line 79, in get_audio song = pyglet.media.load(buf) File 'C:Python27libsite-packagespygletmedia__init__.py', line 1429, in load source = get_source_loader().load(filename, file) File 'C:Python27libsite-packagespygletmedia__init__.py', line 1410, in load return riff.WaveSource(filename, file) File 'C:Python27libsite-packagespygletmediariff.py', line 185, in __init__ file = open(filename, ’rb’)TypeError: coercing to Unicode: need string or buffer, cStringIO.StringO found

問(wèn)題解答

回答1:

你寫(xiě)的Python3就推薦pygame,可以用pygame.mixen.music來(lái)播放本地mp3文件。當(dāng)時(shí)我也找了很多都沒(méi)有成功,最后用pygame播放的。

這是我寫(xiě)的一個(gè)例子。可以看看,我是把網(wǎng)絡(luò)音樂(lè)保存到本地mp3文件再播放的,readme里面有相關(guān)信息。https://github.com/kompasim/p...

回答2:

如果是Linux環(huán)境下解決方案就比較多了,如果要實(shí)現(xiàn)跨平臺(tái)建議用pygame。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 91精品久久久久久久久久入口 | a看片 | 综合国产在线 | 97人澡人人添人人爽欧美 | www.午夜 | 日韩精品在线一区 | 久久久久久国产免费视网址 | 99精品视频一区二区三区 | 国产一区二区三区 | 日本一区二区三区在线观看 | 欧美 日韩 国产 在线 | 亚洲成人福利视频 | 久久毛片| 午夜精品福利视频 | 中文一区二区视频 | 男人天堂国产 | 天天射美女 | 久草在线在线精品观看 | 一级大片| 99久久精品免费看国产高清 | 精品国产一区二区三区日日嗨 | 福利国产| 国产精品欧美一区喷水 | 国产精品美女久久久久久免费 | 久久成人一区 | 丁香五月缴情综合网 | 亚洲欧美日韩精品久久亚洲区 | 人人干人人超 | 9191在线播放 | 视频一区二区在线观看 | 久久久国产一区 | 青青久久 | 亚洲欧美中文字幕 | 国产一区二区a | 91av视频在线播放 | 亚洲综合色视频在线观看 | 91久久国产综合久久 | 亚洲色视频 | 综合久久综合久久 | 久热精品视频 | 国产精品一区二区视频 |