-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaap.js
1480 lines (1371 loc) · 46.5 KB
/
aap.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Javascript for Accessible Audio Player (AAP)
http://www.terrillthompson.com/music/aap
Author: Terrill Thompson
Version: 2.1.3
Last update: September 10, 2011
Uses Yahoo! Media Player as fallback. API docs are here:
http://mediaplayer.yahoo.com/api/
*/
////////////////////////////////////////
//
// user-defined global variables
//
////////////////////////////////////////
// useDebug - set to true to display event log, otherwise set to false
var useDebug = false;
// useYahoo - set to true to always use the fallback Yahoo Media Player (RECOMMENDED FOR TESTING ONLY)
var useYahoo = false;
////////////////////////////////////////
//
// end user-defined global variables
//
////////////////////////////////////////
if (useDebug) {
var debug;
var log;
var numEvents = 0;
}
var player; //will be programatically set either to "html5" or "yahoo"
//id's of various components
var audioId;
var playlistId;
var nowPlayingId;
var controllerId;
var statusBarId;
var audio;
var controller;
var loading = false;
var playpause;
var seekBar;
var seekBack;
var seekForward;
var seekInterval = 15; //number of seconds to seek forward or back
var timer;
var elapsedTimeContainer;
var elapsedTime;
var duration;
var durationContainer;
var pauseTime = 0;
var mute;
var volumeUp;
var volumeDown;
var hasSlider;
var numSongs;
var songIndex = 0;
var prevSongIndex = 0;
var songId;
var songTitle;
//var prevSongId;
var prevSongId;
var prevSongTitle;
var playlist;
var nowPlayingDiv;
var statusBar;
var userClickedPlayPause = false;
var userClickedLink = false;
var autoplaying = false;
var playButtonImage = 'images/audio_play.gif';
var pauseButtonImage = 'images/audio_pause.gif';
var volumeButtonImage = 'images/audio_volume.gif';
var muteButtonImage = 'images/audio_mute.gif';
//vars used by Yahoo
var thisMediaObj;
function aap_init() {
var audioId = 'aap-audio'; //id of the <audio> element
var playlistId = 'aap-playlist'; //id of the playlist <ul>
var nowPlayingId = 'aap-now-playing'; //id of the Now Playing <div>
var controllerId = 'aap-controller'; //id of the <div> where controls will be written
var statusBarId = 'aap-status-bar'; //id of the <div> where status messages are written
var debugId = 'aap-debug'; //id of the <div> where debug messages are written
if (useDebug) setupDebug(debugId);
audio = document.getElementById(audioId);
if (audio) {
controller = document.getElementById(controllerId);
nowPlayingDiv = document.getElementById(nowPlayingId);
playlist = document.getElementById(playlistId);
statusBar = document.getElementById(statusBarId);
numSongs = countSongs(playlist);
if (audio.canPlayType) { //this browser suports HTML5 audio
// check canPlayType for all audio sources
var sources = audio.getElementsByTagName('source');
var canPlaySourceType = false;
for (var i=0; i<sources.length; i++) {
var audioSource = sources[i];
var sourceType = audioSource.getAttribute('type');
if (audio.canPlayType(sourceType)) canPlaySourceType = true;
}
if ((!canPlaySourceType) && (sources.length == 1) && (sourceType == 'audio/mpeg')) {
//the only file type provided is an MP3, and this browser can't play it in HTML5 audio player
player = 'yahoo';
YAHOO.MediaPlayer.onAPIReady.subscribe(yahooInit);
}
else if ((numSongs > 1) && (isUserAgent('firefox/3') || isUserAgent('Firefox/2') || isUserAgent('Firefox/1'))) {
//This is Firefox 3 or earlier. It can play the current file type in HTML5 but it chokes on playlists
//See here: https://developer.mozilla.org/forums/viewtopic.php?f=4&t=48
//Therefore, need to use Yahoo if there is more than one track in playlist
player = 'yahoo';
YAHOO.MediaPlayer.onAPIReady.subscribe(yahooInit);
}
else if (useYahoo) {
player = 'yahoo';
// if forcing browser to use yahoo player, be sure html5 isn't set to autoplay
// otherwise, it will play, and so will Yahoo!
if (audio.getAttribute('autoplay') != null) {
audio.removeAttribute('autoplay');
}
YAHOO.MediaPlayer.onAPIReady.subscribe(yahooInit);
}
else {
player = 'html5';
if (audio.getAttribute('autoplay') != null) {
autoplaying = true;
}
}
}
else { //this browser does not support HTML5 audio at all
player = 'yahoo';
YAHOO.MediaPlayer.onAPIReady.subscribe(yahooInit);
}
if (useDebug) logit('Using player: ' + player);
addButtons();
addEventListeners();
if (player == 'html5') {
// most browsers at this point attempt to load the <audio> media source
// if successful, the "canplay" and "canplaythrough" events are triggered
// other browsers (ahem) just sit there waiting for further instructions...
if (isUserAgent('chrome') || isUserAgent('opera')) {
playAudio(); //this doesn't play the audio - it just loads the selected track
}
}
}
else {
//there is no audio tag on this page. Do nothing.
}
}
function isUserAgent(which) {
var userAgent = navigator.userAgent.toLowerCase();
if (userAgent.indexOf(which)!=-1) return true;
else return false;
}
function yahooInit() {
//get and set default values
YAHOO.MediaPlayer.setVolume(volume);
// Add listeners for Yahoo events
YAHOO.MediaPlayer.onMediaUpdate.subscribe(onMediaUpdateHandler);
YAHOO.MediaPlayer.onPlaylistUpdate.subscribe(onPlaylistUpdateHandler);
YAHOO.MediaPlayer.onTrackStart.subscribe(onTrackStartHandler);
YAHOO.MediaPlayer.onTrackPause.subscribe(onTrackPauseHandler);
YAHOO.MediaPlayer.onProgress.subscribe(onProgressHandler);
YAHOO.MediaPlayer.onTrackComplete.subscribe(onTrackCompleteHandler);
//since parse was false initially, need to load media from playlist now
YAHOO.MediaPlayer.addTracks(playlist,null,true);
}
function addEventListeners() {
//handle clicks on playlist (HTML5 only - Yahoo playlist handled elsewhere)
if (player == 'html5') {
if (playlist) {
if (playlist.addEventListener) {
playlist.addEventListener('click',function (e) {
if (e.preventDefault) e.preventDefault();
else e.returnValue = false; //??
userClickedLink = true;
if (useDebug) {
logit('<strong>You clicked a title in the playlist</strong>');
}
songIndex = getSongIndex(e);
if (numSongs == 1) playAudio();
else if (numSongs > 1) {
swapSource(e.target);
updatePlaylist(songIndex);
}
}, false);
}
else if (playlist.attachEvent) {
playlist.attachEvent('onclick',function (e) {
e.preventDefault();
userClickedLink = true;
if (useDebug) {
logit('<strong>You clicked a title in the playlist</strong>');
}
songIndex = getSongIndex(e);
if (numSongs == 1) playAudio();
else if (numSongs > 1) {
swapSource(e.target);
updatePlaylist(songIndex);
}
});
}
}
}
if (playpause) {
//handle clicks on play/pause button (HTML5 + Yahoo)
if (playpause.addEventListener) {
playpause.addEventListener('click',function (e) {
userClickedPlayPause = true;
playAudio();
}, false);
}
else if (playpause.attachEvent) {
playpause.attachEvent('onclick',function (e) {
userClickedPlayPause = true;
playAudio();
});
}
}
if (seekBar) {
//handle seekBar onchange event (user slides or clicks seekBar)
//(HTML5 + Yahoo) however no known browser that is using Yahoo
//supports seekBar slider
if (seekBar.addEventListener) {
seekBar.addEventListener('change',function (e) {
seekAudio(seekBar);
}, false);
}
else if (seekBar.attachEvent) {
seekBar.attachEvent('onclick',function (e) {
seekAudio(seekBar);
});
}
}
if (seekBack) {
//handle clicks on seekBack button (HTML5 + Yahoo)
if (seekBack.addEventListener) {
seekBack.addEventListener('click',function (e) {
seekAudio(seekBack);
}, false);
}
else if (seekBack.attachEvent) {
seekBack.attachEvent('onclick',function (e) {
seekAudio(seekBack);
});
}
}
if (seekForward) {
//handle clicks on seekForward button (HTML5 + Yahoo)
if (seekForward.addEventListener) {
seekForward.addEventListener('click',function (e) {
seekAudio(seekForward);
}, false);
}
else if (seekForward.attachEvent) {
seekForward.attachEvent('onclick',function (e) {
seekAudio(seekForward);
});
}
}
if (mute) {
//handle clicks on mute button (HTML5 + Yahoo)
if (mute.addEventListener) {
mute.addEventListener('click',function (e) {
toggleMute();
}, false);
}
else if (mute.attachEvent) {
mute.attachEvent('onclick',function (e) {
toggleMute();
});
}
}
if (volumeUp) {
//handle clicks on volume Up button (HTML5 + Yahoo)
if (volumeUp.addEventListener) {
volumeUp.addEventListener('click',function (e) {
updateVolume('up');
}, false);
}
else if (volumeUp.attachEvent) {
volumeUp.attachEvent('onclick',function (e) {
updateVolume('up');
});
}
}
if (volumeDown) {
//handle clicks on volumeDown button (HTML5 + Yahoo)
if (volumeDown.addEventListener) {
volumeDown.addEventListener('click',function (e) {
updateVolume('down');
}, false);
}
else if (volumeDown.attachEvent) {
volumeDown.attachEvent('onclick',function (e) {
updateVolume('down');
});
}
}
//add event listeners for most media events documented here:
//https://developer.mozilla.org/En/Using_audio_and_video_in_Firefox
if (player == 'html5' && audio.addEventListener) {
audio.addEventListener('abort',function () {
if (useDebug) logit('abort');
}, false);
audio.addEventListener('canplay',function () {
if (useDebug) logit('canplay');
}, false);
audio.addEventListener('canplaythrough',function () {
if (useDebug) logit('canplaythrough');
playAudio();
}, false);
audio.addEventListener('canshowcurrentframe',function () {
if (useDebug) logit('canshowcurrentframe');
}, false);
audio.addEventListener('dataunavailable',function () {
if (useDebug) logit('dataunavailable');
}, false);
audio.addEventListener('durationchange',function () {
if (useDebug) logit('durationchange');
//duration of audio has changed (probably from unknown to known value).
//Update seekbar with new value
setupSeekControls();
}, false);
audio.addEventListener('emptied',function () {
if (useDebug) logit('emptied');
}, false);
audio.addEventListener('empty',function () {
if (useDebug) logit('empty');
}, false);
audio.addEventListener('ended',function () {
if (useDebug) logit('ended');
statusBar.innerHTML = 'End of track';
//although user didn't technically click anything to trigger play event,
//it's almost as if they did, so...
userClickedPlayPause = true; //??
//play the next song when the current one ends
if (numSongs > 1) playNext();
else {
//reset slider and/or start time to 0
if (seekBar.type !== 'text') {
seekBar.value = 0;
}
showTime(0,elapsedTimeContainer,hasSlider);
//reset play button
playpause.setAttribute('title','Play');
playpause.style.backgroundImage='url(' + playButtonImage + ')';
}
}, false);
// Note: As of 7-25-11, error events are not being triggered.
// The HTML5 spec may have evolved since AAP 1.0. This section needs to be updated.
// Resource: http://dev.w3.org/html5/spec/Overview.html#media-element
audio.addEventListener('error',function () {
statusBar.innerHTML = 'Error';
var errorCode = audio.error.code;
var networkState = audio.networkState;
if (errorCode == 1) var errorMsg = 'Waiting'; //actually, aborted I think
else if (errorCode == 2) var errorMsg = 'Network error';
else if (errorCode == 3) var errorMsg = 'Media decoding error';
else if (errorCode == 4) {
//4 = media source not supported
//Firefix 3.x returns this if it tries to load a file
//from a source that has been changed dynamically (e.g., via swapSource())
//To determine whether this is Firefox 3.x or an actual media source problem,
//need to also evaluate netWorkState
//Firefox 3.x returns a bogus netWorkState value (4), not in the HTML5 spec
if (networkState == 4) {
var errorMsg = 'Firefox 3.x File Load Error! ';
}
else {
//if it's not Firefox 3.x, then it must really be a media source problem
var errorMsg = 'Error reading media source';
}
}
else var errorMsg = 'Unknown error: ' + errorCode;
statusBar.innerHTML = errorMsg;
if (useDebug) logit(errorMsg);
}, false);
audio.addEventListener('loadeddata',function () {
if (useDebug) logit('loadeddata');
//meta data includes duration
duration = audio.duration;
if (duration > 0) {
showTime(duration,durationContainer,hasSlider);
seekBar.setAttribute('min',0);
seekBar.setAttribute('max',duration);
}
}, false);
audio.addEventListener('loadedmetadata',function () {
if (useDebug) logit('loadedmetadata');
}, false);
audio.addEventListener('loadstart',function () {
statusBar.innerHTML = 'Loading';
if (useDebug) logit('loadstart');
}, false);
audio.addEventListener('mozaudioavailable',function () {
if (useDebug) logit('mozaudioavailable');
}, false);
audio.addEventListener('pause',function () {
if (useDebug) logit('pause');
}, false);
audio.addEventListener('play',function () {
if (useDebug) logit('play');
//good time to be sure the pause button is showing
playpause.setAttribute('title','Play');
}, false);
audio.addEventListener('ratechange',function () {
if (useDebug) logit('ratechange');
}, false);
audio.addEventListener('seeked',function () {
if (useDebug) logit('seeked');
}, false);
audio.addEventListener('seeking',function () {
if (useDebug) logit('seeking');
}, false);
audio.addEventListener('suspend',function () {
if (useDebug) logit('suspend');
}, false);
audio.addEventListener('timeupdate',function () {
//the current time on the media has been updated
//not added to event log - it happens too often
updateSeekBar();
}, false);
audio.addEventListener('volumechange',function () {
//not added to event log - already logged via volume functions
if (useDebug) logit('volumechange event registered');
}, false);
audio.addEventListener('waiting',function () {
if (useDebug) logit('waiting');
statusBar.innerHTML = 'Waiting';
}, false);
}
}
function addButtons() {
// add HTML buttons to #controller
playpause = document.createElement('input');
playpause.setAttribute('type','button');
playpause.setAttribute('id','aap-playpause');
playpause.setAttribute('value','');
playpause.setAttribute('title','Play');
playpause.setAttribute('accesskey','P');
controller.appendChild(playpause);
// Don't display a slider in browsers that tell you they can handle it but really can't
// Safari on iOS acknowledges seekBar.type = 'range', but displays it as a text input, not a slider
// Chrome crashes if user moves the slider too rapidly
if (!(isUserAgent('iphone') || isUserAgent('ipad') || isUserAgent('chrome'))) {
seekBar = document.createElement('input');
seekBar.setAttribute('type','range');
seekBar.setAttribute('id','aap-seekBar');
seekBar.setAttribute('value','0'); //???
seekBar.setAttribute('step','any');
controller.appendChild(seekBar);
hasSlider = true;
}
if (hasSlider) {
// Check to see if browser can really support this feature
// If browser says seekBar is type="text", we know it can't (e.g., Firefox can't)
if (seekBar.type == 'text') {
controller.removeChild(seekBar);
hasSlider = false;
}
}
//Now add rewind and fast forward buttons
//These will be hidden from users who have sliders, but visible to users who don't
//We still want them, even if hidden, so users can benefit from their accesskeys
seekBack = document.createElement('input');
seekBack.setAttribute('type','button');
seekBack.setAttribute('id','aap-seekBack');
seekBack.setAttribute('value','');
seekBack.setAttribute('title','Rewind ' + seekInterval + ' seconds');
seekBack.setAttribute('accesskey','R');
controller.appendChild(seekBack);
seekForward = document.createElement('input');
seekForward.setAttribute('type','button');
seekForward.setAttribute('id','aap-seekForward');
seekForward.setAttribute('value','');
seekForward.setAttribute('title','Forward ' + seekInterval + ' seconds');
seekForward.setAttribute('accesskey','F');
controller.appendChild(seekForward);
// initially, seekBar, seekBack, & seekForward should be disabled
// they will be enabled once the duration of the media file is known
toggleSeekControls('off');
if (hasSlider == true) {
//Note: all major browsers support accesskey on elements hidden with visibility:hidden
seekBack.style.visibility='hidden';
seekForward.style.visibility='hidden';
}
timer = document.createElement('span');
timer.setAttribute('id','aap-timer');
elapsedTimeContainer = document.createElement('span');
elapsedTimeContainer.setAttribute('id','aap-elapsedTime');
var startTime = document.createTextNode('0:00');
elapsedTimeContainer.appendChild(startTime);
durationContainer = document.createElement('span');
durationContainer.setAttribute('id','aap-duration');
timer.appendChild(elapsedTimeContainer);
timer.appendChild(durationContainer);
controller.appendChild(timer);
if (!(isUserAgent('iphone') || isUserAgent('ipad'))) {
//iphones and ipads don't support HTML5 audio volume control
//so don't display volume-related buttons
mute = document.createElement('input');
mute.setAttribute('type','button');
mute.setAttribute('id','aap-mute');
mute.setAttribute('value','');
mute.setAttribute('title','Mute');
mute.setAttribute('accesskey','M');
controller.appendChild(mute);
volumeUp = document.createElement('input');
volumeUp.setAttribute('type','button');
volumeUp.setAttribute('id','aap-volumeUp');
volumeUp.setAttribute('value','');
volumeUp.setAttribute('title','Volume Up');
volumeUp.setAttribute('accesskey','U');
controller.appendChild(volumeUp);
volumeDown = document.createElement('input');
volumeDown.setAttribute('type','button');
volumeDown.setAttribute('id','aap-volumeDown');
volumeDown.setAttribute('value','');
volumeDown.setAttribute('title','Volume Down');
volumeDown.setAttribute('accesskey','D');
controller.appendChild(volumeDown);
}
audio.volume = volume;
setupSeekControls();
}
function showTime(time,elem,hasSlider) {
//time must be passed to this function in seconds
var minutes = Math.floor(time/60);
var seconds = Math.floor(time % 60);
if (seconds < 10) seconds = '0' + seconds;
var output = minutes + ':' + seconds;
if (elem == elapsedTimeContainer) elem.innerHTML = output;
else {
if (output == '0:00') {
//don't show 0:00 as duration - just empty out the div
elem.innerHTML = '';
}
else {
elem.innerHTML = ' / ' + output;
}
}
}
function playAudio() {
if (player == 'html5') {
if (autoplaying) {
statusBar.innerHTML = 'Playing';
playpause.setAttribute('title','Pause');
playpause.style.backgroundImage='url(' + pauseButtonImage + ')';
autoplaying = false; //reset. This var is only true when page is first loaded
}
else if (userClickedPlayPause) {
if (audio.paused || audio.ended) {
//audio is paused. play it.
audio.play();
statusBar.innerHTML = 'Playing';
playpause.setAttribute('title','Pause');
playpause.style.backgroundImage='url(' + pauseButtonImage + ')';
}
else {
//audio is playing. pause it.
audio.pause();
statusBar.innerHTML = 'Paused';
playpause.setAttribute('title','Play');
playpause.style.backgroundImage='url(' + playButtonImage + ')';
}
userClickedPlayPause = false; //reset
}
else if (userClickedLink) {
if (audio.paused || audio.ended) {
// User clicked on a link, so they probably expect the media to play
audio.play();
statusBar.innerHTML = 'Playing';
playpause.setAttribute('title','Pause');
playpause.style.backgroundImage='url(' + pauseButtonImage + ')';
}
else {
// audio is playing, and will continue playing after new media is loaded
// that's done elsewhere
}
}
else {
// not sure how this function was called. Do nothing.
}
if (typeof songTitle == 'undefined') {
//this will only be true if songIndex == 0, and user has clicked Play button
updatePlaylist(0);
}
loading=false;
}
else { //player is yahoo
playerState = YAHOO.MediaPlayer.getPlayerState();
//values: STOPPED: 0, PAUSED: 1, PLAYING: 2,BUFFERING: 5, ENDED: 7
if (playerState == 2) { //playing
YAHOO.MediaPlayer.pause();
statusBar.innerHTML = 'Paused';
playpause.setAttribute('title','Play');
playpause.style.backgroundImage='url(' + playButtonImage + ')';
}
else {
if (playerState == 0) statusBar.innerHTML = 'Stopped';
else if (playerState == 1) statusBar.innerHTML = 'Paused';
else if (playerState == 5) statusBar.innerHTML = 'Buffering';
else if (playerState == 7) statusBar.innerHTML = 'Ended';
YAHOO.MediaPlayer.play();
statusBar.innerHTML = 'Playing';
playpause.setAttribute('title','Pause');
playpause.style.backgroundImage='url(' + pauseButtonImage + ')';
}
}
}
function playAudioOld() {
if (player == 'html5') {
if (autoplaying) {
statusBar.innerHTML = 'Playing';
playpause.setAttribute('title','Pause');
playpause.style.backgroundImage='url(' + pauseButtonImage + ')';
autoplaying = false; //reset. This var is only true when page is first loaded
}
else if (audio.paused || audio.ended) {
//Safari 5.x (both Win & Mac) has audio.paused=true even if it hasn't played yet
//Don't play unless user initiated the play event
if (userClickedPlayPause) {
audio.play();
statusBar.innerHTML = 'Playing';
playpause.setAttribute('title','Pause');
playpause.style.backgroundImage='url(' + pauseButtonImage + ')';
userClickedPlayPause = false; //reset
autoplaying = false; //another reset. This is only true when page is first loaded
}
}
else if (userClickedPlayPause) {
//similar to above, only pause if user requested it
//this should eliminate browsers pausing after other events
//e.g., Chrome 7.x pausing after seeked
// and Firefox 3.x mysterious pause after 3 seconds bug
audio.pause();
statusBar.innerHTML = 'Paused';
playpause.setAttribute('title','Play');
playpause.style.backgroundImage='url(' + playButtonImage + ')';
userClickedPlayPause = false; //reset
}
else {
// if this function was called without clicking on the play/pause button, just ignore it!
// ???
}
if (typeof songTitle == 'undefined') {
//this will only be true if songIndex == 0, and user has clicked Play button
updatePlaylist(0);
}
loading=false;
}
else { //player is yahoo
playerState = YAHOO.MediaPlayer.getPlayerState();
//values: STOPPED: 0, PAUSED: 1, PLAYING: 2,BUFFERING: 5, ENDED: 7
if (playerState == 2) { //playing
YAHOO.MediaPlayer.pause();
statusBar.innerHTML = 'Paused';
playpause.setAttribute('title','Play');
playpause.style.backgroundImage='url(' + playButtonImage + ')';
}
else {
if (playerState == 0) statusBar.innerHTML = 'Stopped';
else if (playerState == 1) statusBar.innerHTML = 'Paused';
else if (playerState == 5) statusBar.innerHTML = 'Buffering';
else if (playerState == 7) statusBar.innerHTML = 'Ended';
YAHOO.MediaPlayer.play();
statusBar.innerHTML = 'Playing';
playpause.setAttribute('title','Pause');
playpause.style.backgroundImage='url(' + pauseButtonImage + ')';
}
}
}
function playNext() {
//called when previous track has ended
if (songIndex == (numSongs - 1)) { //this is the lastsong
//loop around to start of playlist
songIndex = 0;
}
else songIndex++;
var songObj = getTrack(songIndex);
swapSource(songObj);
audio.load(); //track will play automatically after canplaythrough event is triggered
updatePlaylist(songIndex);
}
function playPrevious() {
//never called, but might be if we add a "Previous Track" button
if (songIndex == 0) { //this is the first song
//loop around to end of playlist
songIndex = numSongs-1;
}
else songIndex--;
var songObj = getTrack(songIndex);
swapSource(songObj);
audio.load(); //track will play automatically after canplaythrough event is triggered
updatePlaylist(songIndex);
}
function getTrack(songIndex) {
//returns anchor object corresponding with position songIndex in playlist
var children = playlist.childNodes;
var count = 0;
for (var i=0; i < children.length; i++) {
if (children[i].nodeName == 'LI') {
if (count == songIndex) {
//this is the track
if (children[i].childNodes[0].nodeName == 'A') {
return children[i].childNodes[0];
}
else if (children[i].childNodes[0].childNodes[0].nodeName == 'A') {
return children[i].childNodes[0].childNodes[0];
}
}
count++;
}
}
return false;
}
function updatePlaylist(songIndex) {
//updates playlist (and NowPlayingDiv) so current playing track is identified
//also updates global var songTitle
//Yahoo adds the following code to each item in playlist. Need to work around that.
var yahooCode = '<em class="ymp-skin"></em>';
var children = playlist.childNodes;
var count = 0;
for (var i=0; i < children.length; i++) {
if (children[i].nodeName == 'LI') {
if (count == songIndex) { //this is the song
children[i].className='focus';
if (children[i].childNodes[0].nodeName == 'A') {
songTitle = children[i].childNodes[0].innerHTML;
var titleLang = children[i].childNodes[0].getAttribute('lang');
var anchorDepth = 1;
}
else {
//Yahoo player has wrapped the <a> in a <span>. <a> is one level deeper
songTitle = children[i].childNodes[0].childNodes[0].innerHTML;
var titleLang = children[i].childNodes[0].childNodes[0].getAttribute('lang');
var anchorDepth = 2;
}
//clean up global var songTitle if needed (remove * and/or Yahoo code)
var starPos = songTitle.indexOf(' *');
if (starPos != -1) {
//this title already has a *, so remove it + plus everything that follows
songTitle = songTitle.substr(0,starPos);
}
else {
//this title does not yet have a star. Add one to the HTML in the playlist
if (songTitle.indexOf(yahooCode) != -1) {
//this title has yahooCode, so be sure to restore it after the *
if (anchorDepth == 1) {
children[i].childNodes[0].innerHTML = songTitle + ' *' + yahooCode;
}
else if (anchorDepth == 2) {
children[i].childNodes[0].childNodes[0].innerHTML = songTitle + ' *' + yahooCode;
}
}
else {
//no yahooCode, so just add a *
if (anchorDepth == 1) {
children[i].childNodes[0].innerHTML = songTitle + ' *';
}
else if (anchorDepth == 2) {
children[i].childNodes[0].childNodes[0].innerHTML = songTitle + ' *';
}
}
}
if (typeof titleLang != 'undefined') {
var npTitle = '<span lang="' + titleLang + '">' + songTitle + '</span>';
}
else {
var npTitle = songTitle;
}
nowPlayingDiv.innerHTML = '<span>Selected track:</span><br/>' + npTitle;
}
else if (count == prevSongIndex) { //this was the previous song
//remove * from innerHTML (if there is one)
if (typeof prevSongTitle != 'undefined') {
if (player == 'yahoo') {
var originalTitle = prevSongTitle + ' <em class="ymp-skin"></em>';
if (children[i].childNodes[0].nodeName == 'A') {
children[i].childNodes[0].innerHTML = originalTitle;
}
else {
//Yahoo player has wrapped the <a> in a <span>. <a> is one level deeper
children[i].childNodes[0].childNodes[0].innerHTML = originalTitle;
}
}
else {
if (children[i].childNodes[0].nodeName == 'A') {
children[i].childNodes[0].innerHTML = prevSongTitle;
}
else {
//Yahoo player has wrapped the <a> in a <span>. <a> is one level deeper
children[i].childNodes[0].childNodes[0].innerHTML = prevSongTitle;
}
}
//remove .focus class for <li>
if (children[i].getAttribute('class')){
children[i].removeAttribute('class');
}
else { //if this is IE7
children[i].removeAttribute('className');
}
}
}
count++;
}
}
//prevSongId = songId; //not needed in AAP
prevSongIndex = songIndex;
prevSongTitle = songTitle;
}
function countSongs(playlist) {
var children = playlist.childNodes;
var count = 0;
var finished = false;
for (var i=0; i < children.length && finished == false; i++) {
if (children[i].nodeName == 'LI') count++;
}
return count;
}
function getSongIndex(e) {
//returns songIndex, and changes value of global var songTitle
var eTarget = e.target; //should be a link
if (eTarget.nodeName == 'A') {
var eUrl = eTarget.getAttribute('href');
var children = playlist.childNodes;
var count = 0;
for (var i=0; i < children.length; i++) {
if (children[i].nodeName == 'LI') {
var thisSongUrl = children[i].childNodes[0].getAttribute('href');
if (thisSongUrl == eUrl) { //this is the song
songTitle = children[i].childNodes[0].innerHTML;
return count;
}
count++;
}
}
}
}
function getSongId(songIndex) {
//returns a link object from playlist
var children = playlist.childNodes;
var count = 0;
for (var i=0; i < children.length; i++) {
if (children[i].nodeName == 'LI') {
if (count == songIndex) {
// this is the track
if (player == 'html5') {
return children[i].childNodes[0].getAttribute('id');
}
else {
// Yahoo may have wrapped the <a> it inside a <span>
// (or maybe not)
var grandChildren = children[i].childNodes;
if (grandChildren.length > 0) {
for ( var j=0; j < grandChildren.length; j++ ) {
if (grandChildren[j].nodeName == 'A') {
return grandChildren[j].getAttribute('id');
}
else {
var greatGrandChildren = grandChildren[j].childNodes;
if (greatGrandChildren.length > 0) {
for ( var k=0; k < greatGrandChildren.length; k++ ) {
if (greatGrandChildren[k].nodeName == 'A') {
return greatGrandChildren[k].getAttribute('id');
}
}
}
}
}
}
}
}
count++;
}
}
return false;
}
function getSongIdFromTitle(targetTitle) {
// this function returns the id of a link with a matching title
var children = playlist.childNodes;
for (var i=0; i < children.length; i++) {
if (children[i].nodeName == 'LI') {
if (children[i].childNodes[0].nodeName == 'A') {
var thisTitle = children[i].childNodes[0].innerHTML;
// compare targetTitle with same-length # of characters from thisTitle
// since thisTitle may have " *" or injected Yahoo markup on the end
if (thisTitle.substr(0,targetTitle.length) == targetTitle) {
return children[i].childNodes[0].getAttribute('id');
}
}
else {
//check one layer deeper - if this is Yahoo player, link is wrapped in a <span>
if (children[i].childNodes[0].childNodes[0].nodeName == 'A') {
var thisTitle = children[i].childNodes[0].childNodes[0].innerHTML;
if (thisTitle.substr(0,targetTitle.length) == targetTitle) {
return children[i].childNodes[0].childNodes[0].getAttribute('id');
}
}
}
}
}
}
function getSongIndexFromYahooSongId(yahooSongId) {
// Yahoo Song Id is a bit of a hack
// It's an internal id string and doesn't exist in the DOM, although it's included as part of a longer class name
// that's been added to a class attribute on an anchor tag in the playlist
// If we can find that anchor, we can get the id of that anchor
var currentIndex = 0;
var children = playlist.childNodes;
for (var i=0; i < children.length; i++) {
if (children[i].nodeName == 'LI') {
var thisClass = children[i].childNodes[0].className;
if (thisClass.indexOf(yahooSongId) != -1) {
return currentIndex;
}
else {
currentIndex++;
}
}
}
return false;
}