问题
I am building a web application that generates a new audio file depending on the input from the user. The wav file is generated in our python backend and now I want to be able to play it to the user on the frontend. How can I do this with websockets? (which I'm already using in the application)
I have looked at several other posts here att stackoverflow but none answers my question as I'm not using Angular, Node or React. Just Javascript for frontend and python for the backend.
回答1:
I don't see why you couldn't do this.
- encode your audio into MP3 or OGG or whatever your target browser(s) support
- send it over the web socket to the client
- receive the data into something blob-like
Then, depending on how you want to play things,
- generate an object URL out of the data and pass that in as the
srcfor an<audio>tag, or - use the Web Audio API and decodeAudioData to get a buffer source you can play.
来源:https://stackoverflow.com/questions/55706926/possible-to-send-audio-files-through-websocket