'Webrtc how to play mp3 to remote peer
Webrtc is already established between local browser and remote browser.
Audio is working send and receiving
A library is handling webrtc connection so don't have stream or rtcpeerconnection object access in this scope
I want to create functions to play short mp3 sounds to local browser or remote browser
This works fine to play to the local browser
function playAudioToLocal(mp3url) {
const context = new AudioContext();
const response = await fetch(mp3url);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await context.decodeAudioData(arrayBuffer);
const source = context.createBufferSource();
source.buffer = audioBuffer;
source.start(0);
source.connect(context.destination);
}
How would equivalent be to play mp3 to remote?
function playAudioToRemote(mp3url) {
const context = new AudioContext();
const response = await fetch(mp3url);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await context.decodeAudioData(arrayBuffer);
*WANT TO PLAY audioBuffer TO REMOTE*
}
Solution 1:[1]
this code will transform your decoded audio to a MediaStream that you can use with webrtc :
function playAudioToRemote(mp3url) {
const context = new AudioContext();
const response = await fetch(mp3url);
const arrayBuffer = await response.arrayBuffer();
const audioBuffer = await context.decodeAudioData(arrayBuffer);
const destination = context.createMediaStreamDestination();
const bufferSource = context.createBufferSource();
bufferSource.buffer = audioBuffer;
bufferSource.start(0);
bufferSource.connect(destination);
return destination.stream;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pierre Noyelle |
