1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 function loadMediaFromURL(video) {
6 installTitleEventHandler(video, 'error');
7 video.addEventListener('playing', function(event) {
8 console.log('Video Playing.');
10 var source = loadMediaSource(QueryString.mediafile, QueryString.mediatype);
11 video.src = window.URL.createObjectURL(source);
14 function loadMediaSource(mediaFiles, mediaTypes, appendSourceCallbackFn) {
15 mediaFiles = convertToArray(mediaFiles);
16 mediaTypes = convertToArray(mediaTypes);
18 if (!mediaFiles || !mediaTypes)
19 failTest('Missing parameters in loadMediaSource().');
21 var totalAppended = 0;
22 function onSourceOpen(e) {
23 console.log('onSourceOpen', e);
24 // We can load multiple media files using the same media type. However, if
25 // more than one media type is used, we expect to have a media type entry
26 // for each corresponding media file.
28 for (var i = 0; i < mediaFiles.length; i++) {
29 if (i == 0 || mediaFiles.length == mediaTypes.length) {
30 console.log('Creating a source buffer for type ' + mediaTypes[i]);
32 srcBuffer = mediaSource.addSourceBuffer(mediaTypes[i]);
34 failTest('Exception adding source buffer: ' + e.message);
38 doAppend(mediaFiles[i], srcBuffer);
42 function doAppend(mediaFile, srcBuffer) {
43 var xhr = new XMLHttpRequest();
44 xhr.open('GET', mediaFile);
45 xhr.responseType = 'arraybuffer';
46 xhr.addEventListener('load', function(e) {
47 var onUpdateEnd = function(e) {
48 console.log('End of appending buffer from ' + mediaFile);
49 srcBuffer.removeEventListener('updateend', onUpdateEnd);
51 if (totalAppended == mediaFiles.length) {
52 if (appendSourceCallbackFn)
53 appendSourceCallbackFn(mediaSource);
55 mediaSource.endOfStream();
58 srcBuffer.addEventListener('updateend', onUpdateEnd);
59 srcBuffer.appendBuffer(new Uint8Array(e.target.response));
64 var mediaSource = new MediaSource();
65 mediaSource.addEventListener('sourceopen', onSourceOpen);