-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmediaelement-timecode.js
More file actions
103 lines (70 loc) · 3.1 KB
/
Copy pathmediaelement-timecode.js
File metadata and controls
103 lines (70 loc) · 3.1 KB
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
/* ========================================================================
This script enables creation of timecode links that work with the
mediaelement HTML5 media player plugin.
Assuming you're already using mediaelement on your web page, and you
have a media element such as:
<video width="640" height="400" id="player" controls="controls" preload="auto">
<source src="http://www.example.com/media/video.mp4" type="video/mp4">
</video>
This script will enable timecode links in the following formats:
1) Internal link example (for use on the same page as your video):
<a href="#" class="timecode-link" data-mediaelement="player" data-seconds="165.30">
Jump to 0:02:45:30
</a>
2) External link example:
http://www.example.come/video-page.html?mediaelement=player&seconds=165.30
To use it, include this script at the bottom of your page, after
mediaelement.
======================================================================== */
/* === Handle external links with timecode parameters. === */
// Set up blank variables to store timecode link details.
var urlMediaDivId = '';
var urlSeconds = '';
// Get the query string.
var queryString = $(location).attr('href').split('?')[1];
// If the query string has timecode details:
if ( typeof queryString != 'undefined' && queryString.indexOf('mediaelement=') !== -1 ) {
// Get an array of query string parameters, and loop through them:
var parameters = queryString.split('&');
var pieces = new Array();
$.each(parameters, function (index, parameter) {
// Set the timecode link details.
pieces = parameter.split('=');
if ( pieces[0] == 'mediaelement' )
urlMediaDivId = pieces[1];
if ( pieces[0] == 'seconds' )
urlSeconds = pieces[1];
});
// Define the success callback invoked when mediaelement completes setup.
$('#' + urlMediaDivId).mediaelementplayer({
success: function(player, domObject) {
// Once the video metadata has loaded, set the player to the specified time.
$(domObject).on('loadedmetadata', function() {
player.setCurrentTime(urlSeconds);
// Optionally, start playing automatically.
// player.play();
});
}
});
}
/* === Enable all remaining players on the page. === */
$('audio, video').mediaelementplayer({});
/* === Enable all the internal anchor-style timecode links. === */
// Set up new blank variables for the link parameters.
var anchorMediaDivId = '';
var anchorSeconds = '';
// Loop through all timecode links on the current page.
$('a.timecode-link').each(function () {
// Add a handler for clicks on the timecode link.
$(this).click(function() {
// Get the link's parameters from its data-* attributes.
anchorMediaDivId = $(this).data('mediaelement');
anchorSeconds = $(this).data('seconds');
// Set the player to the specified time.
$('#' + anchorMediaDivId)[0].player.setCurrentTime(anchorSeconds);
// Optionally, start playing automatically.
// $('#' + anchorMediaDivId)[0].player.play();
// Prevent the browser from handling the click as a regular link.
return false;
});
});