我想构建一个能够播放来自互联网广播服务的音频流的小型应用程序。
我已经找到了将代码保存为mp3文件的代码,但我想立即播放声音。
import requests
stream_url = "http://uk5.internet-radio.com:8097/;stream"
r = requests.get(stream_url, stream=True)
with open('stream.wav', 'wb') as f:
try:
for block in r.iter_content(1024):
f.write(block)
except KeyboardInterrupt:
pass
此代码无法正常工作,因为它不会产生任何可用的声音,并且会发生大量下溢错误:
stream_url = 'http://149.56.147.197:8064/;stream/1'#"""
r = requests.get(stream_url, stream=True)
pya = pyaudio.PyAudio()
stream = pya.open(format=pyaudio.paInt16, frames_per_buffer=2048, channels=2, rate=44100, output=True)
for block in r.iter_content(2048):
data = array.array("h", block)
stream.write(data,exception_on_underflow=True)
stream.stop_stream()
stream.close()
错误消息:
Traceback (most recent call last):
File "/Users/bahe007/Desktop/pythonRadio.py", line 15, in <module>
stream.write(data,exception_on_underflow=True)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pyaudio.py", line 586, in write
exception_on_underflow)
IOError: [Errno -9980] Output underflowed
有没有人有想法如何在Python中实现我的网络电台直播功能?