-
-
Save ryanbekabe/8e1f924696378b70795d0c4e4e4f51d3 to your computer and use it in GitHub Desktop.
Flask streaming an audio file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, Response | |
app = Flask(__name__) | |
@app.route("/wav") | |
def streamwav(): | |
def generate(): | |
with open("signals/song.wav", "rb") as fwav: | |
data = fwav.read(1024) | |
while data: | |
yield data | |
data = fwav.read(1024) | |
return Response(generate(), mimetype="audio/x-wav") | |
@app.route("/ogg") | |
def streamogg(): | |
def generate(): | |
with open("signals/song.ogg", "rb") as fogg: | |
data = fogg.read(1024) | |
while data: | |
yield data | |
data = fogg.read(1024) | |
return Response(generate(), mimetype="audio/ogg") | |
if __name__ == "__main__": | |
app.run(debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment