Combining audio and video tracks into new MediaStream

前端 未结 2 1616
無奈伤痛
無奈伤痛 2021-01-17 11:52

I need to get create a MediaStream using audio and video from different MediaStreams. In Firefox, I can instantiate a new MediaStream from an Array of tracks:



        
相关标签:
2条回答
  • 2021-01-17 12:29

    still vendor-prefixed with webkit:

      var outputTracks = [];
      outputTracks = outputTracks.concat(outputAudioStream.getTracks());
      outputTracks = outputTracks.concat(outputVideoStream.getTracks());
      outputMediaStream = new webkitMediaStream(outputTracks);
    
    0 讨论(0)
  • 2021-01-17 12:33

    Here is an updated solution for this issue,

    According to the MediaStream api, one can add multiple tracks to the same MediaStream as following:

    So say you got stream from getUserMedia:

    const constraints = {audio: true, video: true}
    navigator.mediaDevices.getUserMedia(constraints)
    .then(function(stream) {
       // stream is of MediaStream type
       let new_stream = new MediaStream()
    
       // getTracks method returns an array of all stream inputs
       // within a MediaStream object, in this case we have
       // two tracks, an audio and a video track
       let audio_track_from_stream = stream.getTracks()[0] 
       new_stream.addTrack(audio_track_from_stream)
    
       // now play stream locally, or stream it with RTCPeerConnection or Peerjs
       let speaker = document.getElementById('speaker')
       speaker.srcObject = new_stream;
    })
    

    The code above will prompt the user access to his/her microphone and camera, then once the user allows the usage, it will run the callback giving us a stream, which is a MediaStream object. This mediaStream contains two tracks, an audio and a video track. We then take one of those tracks (I assumed track retrieved on the 0 index was the audio track) and add that track to a brand new MediaStream object, then we put that stream in an HTML audio element.

    You would do similar thing to add the video track to this new MediaStream object, just need to retrieve the track located at index 1 of stream.getTracks().

    Note that it also works if you pass a CanvasCaptureMediaStreamTrack to addTrack method.

    Hope it helps others with similar issues

    0 讨论(0)
提交回复
热议问题