Files
shaka-player/tutorials/sample7.txt
T
Natalie Harris 5a459cac61 Partial tutorial for offline. Issue #60.
Change-Id: If8388520b9dcbe8fb8ff95abe378a4e104f98e05
2015-04-29 17:24:41 +00:00

104 lines
3.2 KiB
Plaintext

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>TurtleTube - Offline</title>
<!-- Load the Shaka Player library. -->
<script src="shaka-player.compiled.js"></script>
</head>
<body>
<video id="video"
width="640" height="480"
crossorigin="anonymous"
controls>
Your browser does not support HTML5 video.
</video>
</body>
<script>
function initPlayer() {
// Install polyfills.
shaka.polyfill.installAll();
// Find the video element.
var video = document.getElementById('video');
// Attach the player to the window so that it can be easily debugged.
window.player = new shaka.player.Player(video);
// Listen for errors from the Player.
player.addEventListener('error', function(event) {
console.error(event);
});
}
function chooseTracks(videoSource) {
var ids = [];
var videoTracks = videoSource.getVideoTracks();
if (videoTracks.length) {
videoTracks.sort(shaka.player.VideoTrack.compare);
// Choosing the smallest track.
var track = videoTracks[0];
ids.push(track.id);
}
var audioTracks = videoSource.getAudioTracks();
if (audioTracks.length) {
// The video source gives you the preferred language first.
// Remove any tracks from other languages first.
var lang = audioTracks[0].lang;
audioTracks = audioTracks.filter(function(track) {
return track.lang == lang;
});
// From what's left, choose the middle stream. If we have high, medium,
// and low quality audio, this is medium. If we only have high and low,
// this is high.
var index = Math.floor(audioTracks.length / 2);
ids.push(audioTracks[index].id);
}
// Return IDs of chosen tracks.
return Promise.resolve(ids);
}
function storeContent() {
if (!window.player) {
initPlayer();
}
// Construct an OfflineVideoSource.
var estimator = new shaka.util.EWMABandwidthEstimator();
var offlineSource = new shaka.player.OfflineVideoSource(
null, // groupId, not used when storing content.
estimator);
// Listen for progress events from the OfflineVideoSource.
offlineSource.addEventListener('progress', function(event) {
// Percentage complete is the detail field of the event.
console.log(
'Content storage is ' + event.detail.toFixed(2) + '% complete.');
});
// Store content from MPD url.
var mpdUrl = 'http://turtle-tube.appspot.com/t/t2/dash.mpd';
var preferredLanguage = 'en-US';
offlineSource.store(
mpdUrl,
preferredLanguage,
null, // interpretContentProtection, not needed for clear content.
chooseTracks.bind(null, offlineSource)
).then(
function(groupId) {
window.groupId = groupId;
console.log('Stored content under group ID ' + window.groupId);
}
).catch(
function(e) {
console.error(e);
});
}
document.addEventListener('DOMContentLoaded', storeContent);
</script>
</html>