MediaSource 音视频合并 发表于 2020-07-24 | 热度 ℃ | 字数统计 415 | 阅读时长 2 引自Javascrit中使用MediaSource播放加密视频 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105<html> <head> <title>MSE Demo</title> <style> video { max-width: 100%; } </style> <script type="text/javascript"> var audioFragments = [ "media/audio/init.mp4", "media/audio/seg-1.mp4", "media/audio/seg-2.mp4", "media/audio/seg-3.mp4", "media/audio/seg-4.mp4", "media/audio/seg-5.mp4", "media/audio/seg-6.mp4", "media/audio/seg-7.mp4", ]; var videoFragments = [ "media/video/init.mp4", "media/video/seg-1.mp4", "media/video/seg-2.mp4", "media/video/seg-3.mp4", "media/video/seg-4.mp4", "media/video/seg-5.mp4", "media/video/seg-6.mp4", "media/video/seg-7.mp4", ]; function MSELoadTrack(fragments, type, mediaSource, name) { return new Promise(function(resolve, reject) { var sourceBuffer; var curFragment = 0; function addNextFragment() { if (mediaSource.readyState == "closed") { return; } if (curFragment >= fragments.length) { resolve(); return; } var fragmentFile = fragments[curFragment++]; var req = new XMLHttpRequest(); req.open("GET", fragmentFile); req.responseType = "arraybuffer"; req.addEventListener("load", function() { sourceBuffer.appendBuffer(new Uint8Array(req.response)); }); req.addEventListener("error", function(){reject();}); req.addEventListener("abort", function(){reject();}); req.send(null); } sourceBuffer = mediaSource.addSourceBuffer(type); sourceBuffer.addEventListener("updateend", addNextFragment); addNextFragment(); }); } </script> </head> <body> <h1>Media Source Extensions Demo</h1> <video id="v" controls preload="auto"></video> <script> function Load() { var video = document.getElementById("v"); const audioContentType = 'audio/mp4; codecs="mp4a.40.2"'; // AAC-LC const videoContentType = 'video/mp4; codecs="avc1.64001F"'; // High profile level 3.1 var ms = new MediaSource(); video.src = URL.createObjectURL(ms); var SourceOpen = function () { ms.removeEventListener("sourceopen", SourceOpen); Promise.all([MSELoadTrack(videoFragments, videoContentType, ms, "video"), MSELoadTrack(audioFragments, audioContentType, ms, "audio")]).then(function(){ms.endOfStream();}); } ms.addEventListener("sourceopen", SourceOpen); video.addEventListener("canplay", function(){video.play();}); } Load(); </script> </body></html> -------------感谢您的阅读 有问题请留言(或mailto:frostbelt@sina.cn)-------------