简体   繁体   中英

How to play a video in a Seamless loop using media source extensions

I am working on media source extension to play the video in a seamless loop without any delay. I have done an extensive r&d about it and also done different things. Now i am working on this code

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
  </head>
  <body>
    <video controls></video>
    <script>
      var video = document.querySelector('video');
      var assetURL = 'test1.mp4';
      // Need to be specific for Blink regarding codecs
      // ./mp4info frag_bunny.mp4 | grep Codec
      var mimeCodec = 'video/mp4; codecs="avc1.42E01E, mp4a.40.2"';
      if ('MediaSource' in window && MediaSource.isTypeSupported(mimeCodec)) {
        var mediaSource = new MediaSource;
        //console.log(mediaSource.readyState); // closed
        video.src = URL.createObjectURL(mediaSource);
        mediaSource.addEventListener('sourceopen', sourceOpen);
      } else {
        console.error('Unsupported MIME type or codec: ', mimeCodec);
      }
      function sourceOpen (_) {
        //console.log(this.readyState); // open
        var mediaSource = this;
        var sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
        fetchAB(assetURL, function (buf) {
          sourceBuffer.addEventListener('updateend', function (_) {
            mediaSource.endOfStream();
            video.play();
            //console.log(mediaSource.readyState); // ended
          });
          sourceBuffer.appendBuffer(buf);
        });
      };
      function fetchAB (url, cb) {
        console.log(url);
        var xhr = new XMLHttpRequest;
        xhr.open('get', url);
        xhr.responseType = 'arraybuffer';
        xhr.onload = function () {
          cb(xhr.response);
        };
        xhr.send();
      };
    </script>
  </body>
</html>

It is working fine but the video is playing again with a slight delay. I want to play the video without any delay and I know that it is possible with media source extensions but after spending a lot of time still didn't get any idea that how to do it. Any help would be really appreciated. Thanks

mode值设置为sequence对我有用,它可以确保连续的媒体播放,无论媒体片段时间戳是否不连续。

sourceBuffer.mode = 'sequence';

May be this code solve Your Problem ....play the video in a seamless loop without any delay

for (let chunk of media) {
        await new Promise(resolve => {
          sourceBuffer.appendBuffer(chunk.mediaBuffer);
          sourceBuffer.onupdateend = e => {
            sourceBuffer.onupdateend = null;
            sourceBuffer.timestampOffset += chunk.mediaDuration;
            console.log(mediaSource.duration);
            resolve()
          }
        })

      }

if you need more information visit this link..... http://www.esjay.org/2020/01/01/videos-play-without-buffering-in-javascript/

You can't really do that because it really depends on how fast your PC runs. Its delay should only be there if you have a slow PC but if not it shouldn't have a noticeable delay. Maybe the video that you have is big or it just delays inside the video because only thing I could tell you is autoplay loop inside the videos element as a attribute unless you find a way to replay the video before the video ends but even if it doesn't delay for you it may delay big or just play over the video for others

@CoolOppo

The simplest thing to do is to just rewind the video when it reaches it's end time (duration).

See demo here: https://vc-lut.000webhostapp.com/demos/public/mse_loop_example1.html
(to play video just click the picture part, since no controls for looping background)

The code:

<!DOCTYPE html>
<html>
<head> <meta charset="utf-8"/> </head>
<body>

<div>
<video id="video_mse" > </video>
</div>

<script>

var assetURL = "testloop.mp4";

//# use correct mime/codecs or else Error is "source not open"...
var mimeCodec = 'video/mp4; codecs="avc1.42C01E"';

var mediaSource; var sourceBuffer;

var video = document.getElementById( "video_mse" );

video.onclick = vid_togglePlay; //# for playing/pausing
video.ontimeupdate = vid_checkTime; //# for looping

//# handle play/pause (because controls are hidden)
function vid_togglePlay() 
{
    if( video.paused == true ) { video.play(); }
    else { video.pause(); }
}

//# handle looping...
function vid_checkTime() 
{
    if( video.currentTime == video.duration)
    { 
        video.currentTime = 0; 
        video.play(); 
    } 
}

if ( 'MediaSource' in window && MediaSource.isTypeSupported(mimeCodec) ) 
{
    mediaSource = new MediaSource;
    
    video.src = URL.createObjectURL(mediaSource);
    mediaSource.addEventListener('sourceopen', sourceOpen);
} 
else 
{ console.error('Unsupported MIME type or codec: ', mimeCodec); }

function sourceOpen() 
{
    //console.log(this.readyState); // open
    var mediaSource = this;
    sourceBuffer = mediaSource.addSourceBuffer(mimeCodec);
    
    fetchAB(assetURL, function (buf) {
        sourceBuffer.addEventListener('updateend', function() {
        mediaSource.endOfStream();
        video.play();
      });
      
      sourceBuffer.appendBuffer(buf);
    
    });
};

function fetchAB (url, callbackfunc ) 
{
    console.log("loading file: " + url);
    var xhr = new XMLHttpRequest;
    xhr.open('get', url);
    xhr.responseType = 'arraybuffer';
    xhr.onload = function() { callbackfunc( xhr.response ); }
    xhr.send();
}
      
</script>
</body>
</html>

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