/
converter.js
150 lines (137 loc) · 4.29 KB
/
converter.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Player creation index
var jpCount = 1;
// Replace all HTML5 video and audio tags in given selector with jPlayer equivalents
function jpUpdate(target) {
var audios = $(target).find('audio');
audios.each(function(i){
if (!ie8_lower) {
$(this).get(0).pause();
}
initAudio($(this));
});
var videos = $(target).find('video');
videos.each(function(i){
if (!ie8_lower) {
$(this).get(0).pause();
}
initVideo($(this));
});
}
// Insert a new audio jPlayer and controller with given ID into insert selectors. Return player element
function genAudio(id, insertControls, insertPlayer) {
var player = genPlayer(id);
if (insertControls) {
var controls = genControls('audio');
$(controls).appendTo(insertControls);
}
return $(player).appendTo(insertPlayer);
}
// Insert a new video jPlayer and controller with given ID into insert selectors. Return player element
function genVideo(id, insertControls, insertPlayer) {
var player = genPlayer(id);
var controls = genControls('video');
var returner = $(player).appendTo(insertPlayer);
$(controls).appendTo(insertControls);
return returner;
}
function genControls(playerType) {
var jpClass = "";
switch (playerType) {
case 'audio':
jpClass = 'jp-audio';
break;
case 'video':
jpClass = 'jp-video';
break
}
var controls = '<div id="jp_container_' + jpCount + '" class="' + jpClass + '">';
controls += '<div class="jp-type-single">';
controls += '<div class="jp-gui jp-interface">';
controls += '<ul class="jp-controls">';
controls += '<li><a href="javascript:;" class="jp-play" tabindex="1">play</a></li>';
controls += '<li><a href="javascript:;" class="jp-pause" tabindex="1">pause</a></li>';
controls += '<li><a href="javascript:;" class="jp-stop" tabindex="1">stop</a></li>';
controls += '</ul>';
controls += '<div class="jp-progress">';
controls += '<div class="jp-seek-bar">';
controls += '<div class="jp-play-bar"></div>';
controls += '</div>';
controls += '</div>';
controls += '<div class="jp-time-holder">';
controls += '<div class="jp-current-time"></div>';
controls += '<div class="jp-duration"></div>';
controls += '</div>';
controls += '</div>';
controls += '<div class="jp-no-solution">';
controls += '<span>Update Required</span>';
controls += 'To play the media you will need to either update your browser to a recent version or update your <a href="http://get.adobe.com/flashplayer/" target="_blank">Flash plugin</a>. Sorry for the inconvenience.';
controls += '</div>';
controls += '</div>';
controls += '</div>';
jpCount++;
return controls;
}
function genPlayer(id) {
return '<div id="' + id + '" class="jp-jplayer"></div>';
}
function initAudio(replaceTar) {
// Extract audio file
var srcs = [];
var files = {};
$(replaceTar).find('source').each(function(i){
var thisrc = $(this).attr('src');
var ext = thisrc.substring( thisrc.lastIndexOf(".") + 1 );
srcs.push(ext);
files[ext] = thisrc;
});
var replacer = $('<div class="jp-rep audio-rep"></div>');
var wrapper = replaceTar.replaceWith(replacer);
var repAudio = genAudio('audio_' + jpCount, replacer, replacer);
repAudio.jPlayer({
ready: function (event) {
$(this).jPlayer("setMedia", files);
},
swfPath: "scripts/jPlayer",
cssSelectorAncestor: '#jp_container_' + (jpCount - 1),
wmode: "window"
});
pauseOthers(repAudio);
}
function initVideo(replaceTar) {
// Extract video file
var srcs = [];
var files = {};
$(replaceTar).find('source').each(function(i){
var thisrc = $(this).attr('src');
var ext = thisrc.substring( thisrc.lastIndexOf(".") + 1 );
switch (ext) {
case 'ogg':
ext = 'ogv';
break;
case 'mp4':
ext = 'm4v';
break;
}
srcs.push(ext);
files[ext] = thisrc;
});
var replacer = $('<div class="jp-rep video-rep"></div>');
var wrapper = replaceTar.replaceWith(replacer);
var repVideo = genVideo('video_' + jpCount, replacer, replacer);
repVideo.jPlayer({
ready: function (event) {
$(this).jPlayer("setMedia", files);
$(this).jPlayer("pause", 0.01);
},
swfPath: "scripts/jPlayer",
cssSelectorAncestor: '#jp_container_' + (jpCount - 1),
supplied: srcs.toString(),
size: {
width: '420px',
height: '315px',
cssClass: "jp-video-rep"
},
wmode: 'window'
});
pauseOthers(repVideo);
}