我正在尝试使用flask将Windows 10 PC上的麦克风输入流式传输到带有html5音频标签的基本网页,我的家庭网络上的桌面和Android浏览器都可以正常工作,但它不会流式传输到我的iPhone。我相信IOS需要使用范围标头。当尝试用iPhone浏览器打开流时,我可以看到它first requests bytes=0-1,但我无法只发送标头请求的流字节?下面是迄今为止我的服务器代码,它检查报头,但是如何仅返回范围报头请求的流字节
from flask import Flask, Response,render_template, request
import pyaudio
import re
app = Flask(__name__)
def genHeader(sampleRate, bitsPerSample, channels, samples):
datasize = 10240000 # Some veeery big number here instead of: #samples * channels * bitsPerSample // 8
o = bytes("RIFF",'ascii') # (4byte) Marks file as RIFF
o += (datasize + 36).to_bytes(4,'little') # (4byte) File size in bytes excluding this and RIFF marker
o += bytes("WAVE",'ascii') # (4byte) File type
o += bytes("fmt ",'ascii') # (4byte) Format Chunk Marker
o += (16).to_bytes(4,'little') # (4byte) Length of above format data
o += (1).to_bytes(2,'little') # (2byte) Format type (1 - PCM)
o += (channels).to_bytes(2,'little') # (2byte)
o += (sampleRate).to_bytes(4,'little') # (4byte)
o += (sampleRate * channels * bitsPerSample // 8).to_bytes(4,'little') # (4byte)
o += (channels * bitsPerSample // 8).to_bytes(2,'little') # (2byte)
o += (bitsPerSample).to_bytes(2,'little') # (2byte)
o += bytes("data",'ascii') # (4byte) Data Chunk Marker
o += (datasize).to_bytes(4,'little') # (4byte) Data size in bytes
return o
FORMAT = pyaudio.paInt16
CHUNK = 1024 #1024
RATE = 44100
bitsPerSample = 16 #16
CHANNELS = 1
wav_header = genHeader(RATE, bitsPerSample, CHANNELS, CHUNK)
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True, input_device_index=1,
frames_per_buffer=CHUNK)
@app.after_request
def after_request(response):
response.headers.add('Accept-Ranges', 'bytes')
return response
@app.route('/audio.wav')
def audio():
range_header = request.headers.get('Range', None)
print(range_header)
byte1, byte2 = 0, None
if range_header:
match = re.search(r'(\d+)-(\d*)', range_header)
groups = match.groups()
if groups[0]:
byte1 = int(groups[0])
if groups[1]:
byte2 = int(groups[1])
print(str(byte1) + " " + str(byte2))
# sound
def sound():
data = wav_header
data += stream.read(CHUNK)
yield(data)
while True:
data = stream.read(CHUNK)
yield(data)
resp = Response(sound(), mimetype="audio/x-wav")
return resp
if __name__ == "__main__":
app.run(host='0.0.0.0', debug=True, threaded=True,port=5000)
还有html
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<audio controls>
<source src="http://192.168.8.10:5000/audio.wav" type="audio/x-wav">
Your browser does not support the audio element.
</audio>
</body>
</html>
目前没有回答
相关问题 更多 >
编程相关推荐