简体   繁体   中英

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*

    }

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM