使用flask将音频从pyaudio流传输到HTML5 [英] Stream audio from pyaudio with Flask to HTML5

查看:890
本文介绍了使用flask将音频从pyaudio流传输到HTML5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Flask将我的麦克风的音频(通过pyaudio记录)通过流传输到任何连接的客户端.

I want to stream the audio of my microphone (that is being recorded via pyaudio) via Flask to any client that connects.

这是音频的来源:

    def getSound(self):
        # Current chunk of audio data
        data = self.stream.read(self.CHUNK)
        self.frames.append(data)
        wave = self.save(list(self.frames))

        return data

这是我的烧瓶代码:

@app.route('/audiofeed')
def audiofeed():
    def gen(microphone):
        while True:
            sound = microphone.getSound()
            #with open('tmp.wav', 'rb') as myfile:
            #   yield myfile.read()

            yield sound

    return Response(stream_with_context(gen(Microphone())))

这是客户:

    <audio controls>
        <source src="{{ url_for('audiofeed') }}" type="audio/x-wav;codec=pcm">
        Your browser does not support the audio element.
    </audio>

有时候确实可以,但是大多数时候我会收到" [Errno 32]管道坏了"

It does work sometimes, but most of the times I'm getting "[Errno 32] Broken pipe"

当用open("tmp.wav")-part取消注释时(self.save()可以选择所有先前的帧并将它们保存在tmp.wav中),我有点像流,但是所有输出扬声器的声音是喀哒"声.

When uncommenting that with open("tmp.wav")-part (the self.save() optionally takes all previous frames and saves them in tmp.wav), I kind of get a stream, but all that comes out of the speakers is a "clicking"-noise.

我愿意接受任何建议.如何将麦克风的输入实时流式传输(无需预先录制!)到网络浏览器?

I'm open for any suggestions. How do I get the input of my microphone live-streamed (no pre-recording!) to a webbrowser?

谢谢!

推荐答案

试试这对我有用. shell cmd"cat"工作正常,请参见代码 使用FLASK进行iam

Try This its worked for me. shell cmd "cat" is working perfect see the code iam using FLASK

import subprocess
import os
import inspect
from flask import Flask
from flask import Response

@app.route('/playaudio')
    def playaudio():
        sendFileName=""
        def generate():

            #  get_list_all_files_name this function gives all internal files inside the folder

   filesAudios=get_list_all_files_name(currentDir+"/streamingAudios/1")

            # audioPath is audio file path in system 
            for audioPath in filesAudios:
                data=subprocess.check_output(['cat',audioPath])
                yield data
        return Response(generate(), mimetype='audio/mp3')

这篇关于使用flask将音频从pyaudio流传输到HTML5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆