-2

I want to play audio and video simultaneously using Media Source in Javascript. I can play either but not both at the same time.

Here's my code so far:

 let mse = function() {
    if ('MediaSource' in window && MediaSource.isTypeSupported(codecs)) {
        a = new MediaSource();
        a.addEventListener('sourceopen', ma);
        return a;
    } else {
        return false;
    }
}();

I have another 600 lines of code, if needed.

Gautam
  • 2,597
  • 1
  • 28
  • 51
join
  • 1
  • 3
  • 1
    Relevant information would be what codecs you're using, what container, what error you're getting, and whether or not static media works outside of MSE on that browser. (Also... what browser?) – Brad Aug 17 '18 at 21:19
  • i told you my code is fine , and thank you i get the answer any way – join Aug 20 '18 at 08:01
  • "I get the answer any way" - you should post the "correct" answer then. – totaam Sep 17 '19 at 08:45

1 Answers1

1

if you need to play both of video and sound you need to create 2 source buffers . and as you said . you can play just one of them . so i guess you code is fine . so you need to create 2 buffers . like this

 my_media_source.addEventListener("sourceopen",function (){
    var video_source_buffer = my_media_source.addSourceBuffer(video_mimeCodec);
    var audio_source_buffer = my_media_source.addSourceBuffer(audio_mimeCodec);

     //.......

     video_source_buffer.appendBuffer(...);
     audio_source_buffer.appendBuffer(...);
 }

Now you can just buffer both of video and audio , keep in maind that MediaSource will not play you video antil it gets both of data . so for exemple if you buffered the first 5s from video and 3s from audio the player will stop at 3s

just keep your buffer equitable ;)

Hakim Douib
  • 495
  • 1
  • 6
  • 13
  • cant i handle video and audio simultaneously with some codec ? – Tarun Rawat May 13 '20 at 03:33
  • When using this technique for video/webm and audio/webm, I receive an error for the second .addSourceBuffer: "Failed to execute 'addSourceBuffer' on 'MediaSource': This MediaSource has reached the limit of SourceBuffer objects it can handle. No additional SourceBuffer objects may be added." – Ted Phillips Aug 23 '20 at 06:07
  • @TedPhillips can you show some of your code please? – Hakim Douib Aug 23 '20 at 19:19
  • Sorry, code no longer exists. Due to the buffer-count limit, I ended up muxing the video/audio and streaming that into a single sourceBuffer instead. – Ted Phillips Aug 31 '20 at 06:34