UNPKG

videojs-contrib-media-sources

Version:
74 lines (58 loc) 2.25 kB
<!doctype html> <html> <head> <title>Media Sources Example</title> <link href="node_modules/video.js/dist/video-js/video-js.css" rel="stylesheet"> <style> p { background-color: #ddd; border: thin solid #333; padding: 8px; } </style> </head> <body> <p>The video below is generated by passing in the bytes of an FLV directly into video.js with <a href="https://dvcs.w3.org/hg/html-media/raw-file/tip/media-source/media-source.html">Media Source Extensions</a>. Load this page up through a web server to see it in action.</p> <video id='video' class="video-js vjs-default-skin" height="300" width="600" controls></video> <script src="node_modules/video.js/dist/video-js/video.js"></script> <script src="src/videojs-media-sources.js"></script> <script> var video, req = new XMLHttpRequest(); // initialize video.js video = videojs('video'); // the flash-based media sources implementation only supports FLV video data // use XMLHttpRequest2 to get the raw byte array of an example FLV req.open('GET', 'barsandtone.flv', true); req.responseType = 'arraybuffer'; req.onload = function(event){ var // create a new media source to hold the data buffers mediaSource = new videojs.MediaSource(), // wrap the arraybuffer in a view so we can easily work with the // individual bytes bytes = new Uint8Array(req.response), url; // when a media source is assigned to a video element the `sourceopen` // event fires mediaSource.addEventListener('sourceopen', function(event){ // construct the video data buffer and set the appropriate MIME type var sourceBuffer = mediaSource.addSourceBuffer('video/flv; codecs="vp6,aac"'); // start feeding bytes to the buffer // the video element that is reading from the associated media buffer is // ready to start playing now sourceBuffer.appendBuffer(bytes, video); }, false); // to assign a media source to a video element, you have to create a URL for it url = videojs.URL.createObjectURL(mediaSource); // assign the media source URL to video.js video.src({ src: url, type: "video/flv" }); }; req.send(null); </script> </body> </html>