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
151
152
153
154
|
(function($) {
window.audioshortcode = {
/**
* Prep the audio player once the page is ready, add listeners, etc
*/
prep: function( player_id, files, titles, volume, loop ) {
// check if the player has already been prepped, no-op if it has
var container = $( '#wp-as-' + player_id + '-container' );
if ( container.hasClass( 'wp-as-prepped' ) ) {
return;
}
container.addClass( 'wp-as-prepped' );
// browser doesn't support HTML5 audio, no-op
if ( ! document.createElement('audio').canPlayType ) {
return;
}
// if the browser removed the script, no-op
player = $( '#wp-as-' + player_id ).get(0);
if ( typeof player === 'undefined' ) {
return;
}
this[player_id] = [];
this[player_id].i = 0;
this[player_id].files = files;
this[player_id].titles = titles;
player.volume = volume;
var type_map = {
'mp3': 'mpeg',
'wav': 'wav',
'ogg': 'ogg',
'oga': 'ogg',
'm4a': 'mp4',
'aac': 'mp4',
'webm': 'webm'
};
// strip out all the files that can't be played
for ( var i = this[player_id].files.length-1; i >= 0; i-- ) {
var extension = this[player_id].files[i].split( '.' ).pop();
var type = 'audio/' + type_map[extension];
if ( ! player.canPlayType( type ) ) {
this.remove_track( player_id, i );
}
}
// bail if there are no more good files
if ( 0 === this[player_id].files.length ) {
return;
}
player.src = this[player_id].files[0];
// show the controls if there are still 2+ files remaining
if ( 1 < this[player_id].files.length ) {
$( '#wp-as-' + player_id + '-controls' ).show();
}
player.addEventListener( 'error', function() {
audioshortcode.remove_track( player_id, audioshortcode[player_id].i );
if ( 0 < audioshortcode[player_id].files.length ) {
audioshortcode[player_id].i--;
audioshortcode.next_track( player_id, false, loop );
}
}, false );
player.addEventListener( 'ended', function() {
audioshortcode.next_track( player_id, false, loop );
}, false );
player.addEventListener( 'play', function() {
var i = audioshortcode[player_id].i;
var titles = audioshortcode[player_id].titles;
$( '#wp-as-' + player_id + '-playing' ).text( ' ' + titles[i] );
}, false );
player.addEventListener( 'pause', function() {
$( '#wp-as-' + player_id + '-playing' ).text( '' );
}, false );
},
/**
* Remove the track and update the player/controls if needed
*/
remove_track: function( player_id, index ) {
this[player_id].files.splice( index, 1 );
this[player_id].titles.splice( index, 1 );
// get rid of player/controls if they can't be played
if ( 0 === this[player_id].files.length ) {
$( '#wp-as-' + player_id + '-container' ).html( $( '#wp-as-' + player_id + '-nope' ).html() );
$( '#wp-as-' + player_id + '-controls' ).html( '' );
} else if ( 1 == this[player_id].files.length ) {
$( '#wp-as-' + player_id + '-controls' ).html( '' );
}
},
/**
* Change the src of the player, load the file, then play it
*/
start_track: function( player_id, file ) {
var player = $( '#wp-as-' + player_id ).get(0);
player.src = file;
player.load();
player.play();
},
/**
* Play the previous track
*/
prev_track: function( player_id ) {
var player = $( '#wp-as-' + player_id ).get(0);
var files = this[player_id].files;
if ( player.paused || 0 === this[player_id].i ) {
return;
}
player.pause();
if ( 0 < this[player_id].i ) {
this[player_id].i--;
this.start_track( player_id, files[this[player_id].i] );
}
},
/**
* Play the next track
*/
next_track: function( player_id, fromClick, loop ) {
var player = $( '#wp-as-' + player_id ).get(0);
var files = this[player_id].files;
if ( fromClick && ( player.paused || files.length-1 == this[player_id].i ) ) {
return;
}
player.pause();
if ( files.length-1 > this[player_id].i ) {
this[player_id].i++;
this.start_track( player_id, files[this[player_id].i] );
} else if ( loop ) {
this[player_id].i = 0;
this.start_track( player_id, 0 );
} else {
this[player_id].i = 0;
player.src = files[0];
$( '#wp-as-' + player_id + '-playing' ).text( '' );
}
}
};
})(jQuery);
|