-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmedia-player.js
executable file
·128 lines (110 loc) · 4.22 KB
/
media-player.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Sample Media Player using HTML5's Media API
//
// Ian Devlin (c) 2012
// http://iandevlin.com
// http://twitter.com/iandevlin
//
// This was written as part of an article for the February 2013 edition of .net magazine (http://netmagazine.com/)
// Wait for the DOM to be loaded before initialising the media player
document.addEventListener("DOMContentLoaded", function() { initialiseMediaPlayer(); }, false);
// Variables to store handles to various required elements
var mediaPlayer;
var playPauseBtn;
var muteBtn;
var progressBar;
function initialiseMediaPlayer() {
// Get a handle to the player
mediaPlayer = document.getElementById('video_player');
// Get handles to each of the buttons and required elements
playPauseBtn = document.getElementById('play-pause-button');
muteBtn = document.getElementById('mute-button');
progressBar = document.getElementById('progress-bar');
// Hide the browser's default controls
mediaPlayer.controls = false;
// Add a listener for the timeupdate event so we can update the progress bar
mediaPlayer.addEventListener('timeupdate', updateProgressBar, false);
// Add a listener for the play and pause events so the buttons state can be updated
mediaPlayer.addEventListener('play', function() {
// Change the button to be a pause button
changeButtonType(playPauseBtn, 'pause');
}, false);
mediaPlayer.addEventListener('pause', function() {
// Change the button to be a play button
changeButtonType(playPauseBtn, 'play');
}, false);
// need to work on this one more...how to know it's muted?
mediaPlayer.addEventListener('volumechange', function(e) {
// Update the button to be mute/unmute
if (mediaPlayer.muted) changeButtonType(muteBtn, 'unmute');
else changeButtonType(muteBtn, 'mute');
}, false);
mediaPlayer.addEventListener('ended', function() { this.pause(); }, false);
}
function togglePlayPause() {
// If the mediaPlayer is currently paused or has ended
if (mediaPlayer.paused || mediaPlayer.ended) {
// Change the button to be a pause button
changeButtonType(playPauseBtn, 'pause');
// Play the media
mediaPlayer.play();
}
// Otherwise it must currently be playing
else {
// Change the button to be a play button
changeButtonType(playPauseBtn, 'play');
// Pause the media
mediaPlayer.pause();
}
}
// Changes the volume on the media player
function changeVolume(direction) {
if (direction === '+') mediaPlayer.volume += mediaPlayer.volume == 1 ? 0 : 0.1;
else mediaPlayer.volume -= (mediaPlayer.volume == 0 ? 0 : 0.1);
mediaPlayer.volume = parseFloat(mediaPlayer.volume).toFixed(1);
}
// Update the progress bar
function updateProgressBar() {
// Work out how much of the media has played via the duration and currentTime parameters
var percentage = (100 / mediaPlayer.duration) * mediaPlayer.currentTime;
// Update the progress bar's value
progressBar.value = percentage;
// Update the progress bar's text (for browsers that don't support the progress element)
progressBar.innerHTML = percentage + '% played';
progressBar.addEventListener("click", seek);
}
function seek(e) {
var percent = e.offsetX / this.offsetWidth;
mediaPlayer.currentTime = percent * mediaPlayer.duration;
e.target.value = Math.floor(percent / 100);
e.target.innerHTML = progressBar.value + '% played';
}
// Updates a button's title, innerHTML and CSS class to a certain value
function changeButtonType(btn, value) {
if (value === "pause") {
btn.src = "images/pause.png";
}
else if (value == "play") {
btn.src = "images/play.png";
}
}
// Loads a video item into the media player
function loadVideo() {
for (var i = 0; i < arguments.length; i++) {
var file = arguments[i].split('.');
var ext = file[file.length - 1];
// Check if this media can be played
if (canPlayVideo(ext)) {
// Reset the player, change the source file and load it
resetPlayer();
mediaPlayer.src = arguments[i];
mediaPlayer.load();
break;
}
}
}
// Checks if the browser can play this particular type of file or not
function canPlayVideo(ext) {
var ableToPlay = mediaPlayer.canPlayType('video/' + ext);
if (ableToPlay == '') return false;
else return true;
}