我们正在尝试构建一个应用程序,以向多个订阅者广播实时音频。服务器(使用go编写)接受通过块传输的pcm数据,客户端使用pyaudio可以接入麦克风并使用以下代码发送此数据。我们已经测试过这个功能,它可以在任何浏览器上通过订阅者URL播放音频。
import pyaudio
import requests
import time
p = pyaudio.PyAudio()
# frames per buffer ?
CHUNK = 1024
# 16 bits per sample ?
FORMAT = pyaudio.paInt16
# 44.1k sampling rate ?
RATE = 44100
# number of channels
CHANNELS = 1
STREAM = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK
)
print "initialized stream"
def get_chunks(stream):
while True:
try:
chunk = stream.read(CHUNK,exception_on_overflow=False)
yield chunk
except IOError as ioe:
print "error %s" % ioe
url = "https://<server-host>/stream/publish/<uuid>/"
s = requests.session()
s.headers.update({'Content-Type': "audio/x-wav;codec=pcm"})
resp = s.post(url, data=get_chunks(STREAM))
我们需要一个浏览器、iOS和Android客户端来完成与上述客户端相同的功能。我们可以使用浏览器上的getUserMedia API从麦克风中获取音频,但无法像上面的Python代码那样将此音频发送到服务器上。有人能指点一下正确的方向吗?