source: main/trunk/model-sites-dev/respooled/collect/popup-video-respooled/js/media-player.js@ 29863

Last change on this file since 29863 was 29863, checked in by davidb, 9 years ago

First cut at respooled site/collection

File size: 6.4 KB
Line 
1// Sample Media Player using HTML5's Media API
2//
3// Ian Devlin (c) 2012
4// http://iandevlin.com
5// http://twitter.com/iandevlin
6//
7// This was written as part of an article for the February 2013 edition of .net magazine (http://netmagazine.com/)
8
9// Wait for the DOM to be loaded before initialising the media player
10document.addEventListener("DOMContentLoaded", function() { initialiseMediaPlayer(); }, false);
11
12// Variables to store handles to various required elements
13var mediaPlayer;
14var playPauseBtn;
15var muteBtn;
16var progressBar;
17
18var mediaStartPlayTime;
19var mediaStartPauseTime;
20var mediaPlayedNotes;
21
22function initialiseMediaPlayer() {
23
24 mediaStartPlayTime = null;
25 mediaStartPauseTime = null;
26 mediaPlayedNotes = {};
27
28 // Get a handle to the player
29 mediaPlayer = document.getElementById('video');
30
31 // Get handles to each of the buttons and required elements
32 playPauseBtn = document.getElementById('play-pause-button');
33 muteBtn = document.getElementById('mute-button');
34 progressBar = document.getElementById('progress-bar');
35
36 // Hide the browser's default controls
37 mediaPlayer.controls = false;
38
39 // Add a listener for the timeupdate event so we can update the progress bar
40 mediaPlayer.addEventListener('timeupdate', updateProgressBar, false);
41
42 // Add a listener so the video can be advanced if the progress bar is clicked on
43 progressBar.addEventListener("change", progressBarChanged);
44
45
46 // Pause the video when the slider handle is being dragged
47 progressBar.addEventListener("mousedown", function() {
48 mediaPlayer.pause();
49 });
50
51 // Play the video when the slider handle is dropped
52 progressBar.addEventListener("mouseup", function() {
53 mediaPlayer.play();
54 });
55
56 // Add a listener for the play and pause events so the buttons state can be updated
57 mediaPlayer.addEventListener('play', function() {
58 // Change the button to be a pause button
59 changeButtonType(playPauseBtn, 'pause');
60 }, false);
61 mediaPlayer.addEventListener('pause', function() {
62 // Change the button to be a play button
63 changeButtonType(playPauseBtn, 'play');
64 }, false);
65
66 // need to work on this one more...how to know it's muted?
67 mediaPlayer.addEventListener('volumechange', function(e) {
68 // Update the button to be mute/unmute
69 if (mediaPlayer.muted) changeButtonType(muteBtn, 'unmute');
70 else changeButtonType(muteBtn, 'mute');
71 }, false);
72 mediaPlayer.addEventListener('ended', function() { this.pause(); }, false);
73}
74
75function togglePlayPause() {
76 // If the mediaPlayer is currently paused or has ended
77 if (mediaPlayer.paused || mediaPlayer.ended) {
78 if (mediaStartPlayTime == null) {
79 mediaStartPlayTime = Date.now();
80 //console.log("*** mediaStartPlayTime=" + mediaStartPlayTime);
81 mediaPlayedNotes[String(mediaStartPlayTime)] = [];
82
83 }
84 else {
85 var mediaPauseBuildup = mediaStartPauseTime - Date.now();
86
87 // adjust the start play time to the pause is effectively ignored
88 mediaStartPlayTime += mediaPauseBuildup;
89 mediaStartPauseTime = null;
90
91 }
92
93 // Change the button to be a pause button
94 changeButtonType(playPauseBtn, 'pause');
95 // Play the media
96 mediaPlayer.play();
97 }
98 // Otherwise it must currently be playing
99 else {
100 mediaPauseTime = Date.now();
101
102 // Change the button to be a play button
103 changeButtonType(playPauseBtn, 'play');
104 // Pause the media
105 mediaPlayer.pause();
106
107 console.log("*** mediaPlayedNotes = " + JSON.stringify(mediaPlayedNotes));
108 }
109}
110
111// Stop the current media from playing, and return it to the start position
112function stopPlayer() {
113 mediaPlayer.pause();
114 mediaPlayer.currentTime = 0;
115}
116
117// Changes the volume on the media player
118function changeVolume(direction) {
119 if (direction === '+') mediaPlayer.volume += mediaPlayer.volume == 1 ? 0 : 0.1;
120 else mediaPlayer.volume -= (mediaPlayer.volume == 0 ? 0 : 0.1);
121 mediaPlayer.volume = parseFloat(mediaPlayer.volume).toFixed(1);
122}
123
124// Toggles the media player's mute and unmute status
125function toggleMute() {
126 if (mediaPlayer.muted) {
127 // Change the cutton to be a mute button
128 changeButtonType(muteBtn, 'mute');
129 // Unmute the media player
130 mediaPlayer.muted = false;
131 }
132 else {
133 // Change the button to be an unmute button
134 changeButtonType(muteBtn, 'unmute');
135 // Mute the media player
136 mediaPlayer.muted = true;
137 }
138}
139
140// Replays the media currently loaded in the player
141function replayMedia() {
142 resetPlayer();
143 mediaPlayer.play();
144}
145
146// Update the progress bar
147function updateProgressBar() {
148 // Work out how much of the media has played via the duration and currentTime parameters
149 var percentage = (100 / mediaPlayer.duration) * mediaPlayer.currentTime;
150 // Update the progress bar's value
151 progressBar.value = percentage;
152 // Update the progress bar's text (for browsers that don't support the progress element)
153 progressBar.innerHTML = Math.floor(percentage) + '% played';
154}
155
156function progressBarChanged() {
157 //console.log("**** progress bar changed!")
158 // Calculate the new time
159 var time = mediaPlayer.duration * (progressBar.value / 100);
160
161 // Update the video time
162 mediaPlayer.currentTime = time;
163}
164
165// Updates a button's title, innerHTML and CSS class to a certain value
166function changeButtonType(btn, value) {
167 btn.title = value;
168 btn.innerHTML = value;
169 btn.className = value;
170}
171
172// Loads a video item into the media player
173function loadVideo() {
174 for (var i = 0; i < arguments.length; i++) {
175 var file = arguments[i].split('.');
176 var ext = file[file.length - 1];
177 // Check if this media can be played
178 if (canPlayVideo(ext)) {
179 // Reset the player, change the source file and load it
180 resetPlayer();
181 mediaPlayer.src = arguments[i];
182 mediaPlayer.load();
183 break;
184 }
185 }
186}
187
188// Checks if the browser can play this particular type of file or not
189function canPlayVideo(ext) {
190 var ableToPlay = mediaPlayer.canPlayType('video/' + ext);
191 if (ableToPlay == '') return false;
192 else return true;
193}
194
195// Resets the media player
196function resetPlayer() {
197 mediaStartPlayTime = null;
198 mediaStartPauseTime = null;
199
200 // Reset the progress bar to 0
201 progressBar.value = 0;
202 // Move the media back to the start
203 mediaPlayer.currentTime = 0;
204 // Ensure that the play pause button is set as 'play'
205 changeButtonType(playPauseBtn, 'play');
206}
Note: See TracBrowser for help on using the repository browser.