流式传输方法
import requests
import pyaudio
# 流式传输音频的URL,可使用Post
stream_url = 'https://tts.nirvanaworld.cn/tts?character={{character}}&text={{text}}&stream=true'
# 初始化pyaudio
p = pyaudio.PyAudio()
# 打开音频流
stream = p.open(format=p.get_format_from_width(2),
channels=1,
rate=32000,
output=True)
# 使用requests获取音频流,可使用Post
response = requests.get(stream_url, stream=True)
# 读取数据块并播放
for data in response.iter_content(chunk_size=1024):
stream.write(data)
# 停止和关闭流
stream.stop_stream()
stream.close()
# 终止pyaudio
p.terminate()
Last updated
Was this helpful?