-
Notifications
You must be signed in to change notification settings - Fork 4
/
cordova-mediaplayer.js
804 lines (607 loc) · 27 KB
/
cordova-mediaplayer.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
var audioElement = null; // Cordova Media element or HTML <audio> element
var audioPlaybackProgressCheckInterval = null; // interval that watches audioElement progress/position
var prevTimeLeftInFile; // helper var to determine when audio finishes
var useCordovaMediaElement = false; // boolean to determine type of element audioElement is
var currentMediaPath; // ...
var currentMediaPaused = undefined; // ...
var currentMediaAlreadyLoaded = false; // ...
var currentMediaPlaying = false; // ...
var delayedAudioPlaybackTimeout; // ...
var delayedVideoPlaybackInterval;
var cmpHeight, cmpWidth;
var termLoading = "Loading";
// unused for now:
// var videoFileExt = "m4v"; // iOS
// if(isMobile.Android()) videoFileExt = "mp4";
/* ===================== AUDIO SPECIFIC STUFF ===================== */
/* --------------------------------------- */
function playAudio(src) {
// alert('playAudio [' + src + ']...');
report('TEST','playAudio(' + src + ')...');
try{
prevTimeLeftInFile = 0;
// [useCordovaMediaElement]
// I have found that iOS does better playing audio via a
// HTML <audio> element vs the Cordova Media element - so
// I ONLY use Cordova Media for Android (which does well with it).
// But set this boolean however you'd like.
if(isMobile.Android()) useCordovaMediaElement = true;
// create/init [audioElement]
if(useCordovaMediaElement){
// (Cordova MEDIA element)
audioElement = new Media(src, onSuccess, onError);
}else{
// (HTML5 Audio)
audioElement = document.getElementById("app_audio");
}
audioElement.play();
}catch(e){
report('ERROR','!! ERROR: audio.js playAudio() [' + e.message + ']');
}
// Update audioElement position every second
if (audioPlaybackProgressCheckInterval == null) {
audioPlaybackProgressCheckInterval = setInterval(function() {
try{
if(currentMediaAlreadyLoaded && currentMediaPaused) return;
var position;
if(useCordovaMediaElement){
position = audioElement.getCurrentPosition(cordovaMediaGetCurrentPositionSuccess, cordovaMediaGetCurrentPositionError);
}else{
report('TEST','HTML <audio>....processing [duration] {' + audioElement.duration + ' (' + parseInt(audioElement.duration) + ')} <' + audioElement.readyState + '>');
if(parseInt(audioElement.duration)){
report('TEST','audioPlaybackProgressCheckInterval(pos:' + audioElement.currentTime + ', dur:' + audioElement.duration + ')...');
processAudioPosition(audioElement.currentTime,audioElement.duration);
}else{
report('TEST','audioPlaybackProgressCheckInterval waiting on duration...');
}
}
}catch(e){
report('ERROR','!! ERROR: audio.js playAudio().audioPlaybackProgressCheckInterval [' + e.message + ']');
}
}, 1000);
}
}
/* --------------------------------------- */
function cordovaMediaGetCurrentPositionSuccess(position) {
try{
report('TEST','CORDOVA <media>....processing [position] {' + position + '}');
processAudioPosition(position);
}catch(e){
report('ERROR','!! ERROR: audio.js cordovaMediaGetCurrentPositionSuccess() [' + e.message + ']');
}
}
/* --------------------------------------- */
function cordovaMediaGetCurrentPositionError(e) {
console.log("Error getting pos=" + e);
refreshMediaPositionLabel("00:00"); //Error: " + e);
}
/* --------------------------------------- */
function processAudioPosition(position,duration){
if(!duration) duration = audioElement.getDuration();
report('TEST','[CMP].processAudioPosition(pos:' + parseInt(position) + ',dur:' + duration + ')...');
try{
if (parseInt(position) > 0){ //-1) {
if(currentMediaAlreadyLoaded && currentMediaPaused) return;
_mediaPlaybackHasStarted();
var timeLeft = duration - position;
refreshMediaPositionLabel(Utility.formatTime(timeLeft)); // + " sec");
// trigger stopped method when reaching end of file
// since there is no STOPPED or COMPLETE property at the moment
if((prevTimeLeftInFile >= 1) && (timeLeft <= 1)){
report('TEST','calling _mediaPlaybackHasBeenStopped() because we think we\'ve reached the end of the file ..');
_mediaPlaybackHasBeenStopped();
}
prevTimeLeftInFile = timeLeft;
}
}catch(e){
report('ERROR','!! ERROR: audio.js processAudioPosition(pos:' + position + ',dur:' + duration + ') [' + e.message + ']');
}
}
/* --------------------------------------- */
function pauseAudio() {
if(typeof(mediaPlaybackHasBeenPaused) == 'function') mediaPlaybackHasBeenPaused();
if ((audioElement) && ($('#app_audio').length)){
audioElement.pause();
}
}
/* --------------------------------------- */
function resumeAudio() {
if (audioElement) {
audioElement.play();
}
if(typeof(_mediaPlaybackHasStarted) == 'function') _mediaPlaybackHasStarted();
}
/* --------------------------------------- */
function stopAudio() {
if ($('#app_audio').length){
if (audioElement) {
if(useCordovaMediaElement){
audioElement.stop();
}else{
audioElement.pause();
}
}
}
clearTimeoutVar(delayedAudioPlaybackTimeout);
clearIntervalVar(audioPlaybackProgressCheckInterval);
audioPlaybackProgressCheckInterval = null;
_mediaPlaybackHasBeenStopped();
}
/* --------------------------------------- */
function onSuccess() {
console.log("AUDIO.JS: playAudio():Audio Success");
}
/* --------------------------------------- */
function onError(error) {
console.log('AUDIO.JS ERROR: code: ' + error.code + '\nmessage: ' + error.message + '\n');
}
/* --------------------------------------- */
function refreshMediaPositionLabel(labelString) {
$('#cmp_status_or_position').html(labelString);
console.log('Media Label Change [' + labelString + ']');
}
/* ----------------------------------------------------------- */
function initMediaPlayerForAudio(audioTitle,audioPath,audioThumbPath,audioDesc){
report('TEST','[CMP].initMediaPlayerForAudio(audioPath:' + audioPath + ')..');
try{
_closeMediaPlayer();
$('#cordova_mediaplayer')
.removeClass('playing disabled');
// VARS
var audioTitle = audioTitle;
if(!audioDesc) audioDesc = "No audio description provided.";
var audioDesc = audioDesc;
$('#cordova_mediaplayer').removeClass('video').addClass('audio');
$('#cmp_header h1').html(audioTitle);
$('#cordova_mediaplayer #cmp_desc').html(audioDesc);
$('#media_details').show();
$('#app_audio').remove();
var _audioTagHTML = '<audio id="app_audio" ' +
' audio_path="' + audioPath + '" ' +// '?cachedID=' + cacheVersionID + '" ' +
' src="' + audioPath + '" ' + //'?cachedID=' + cacheVersionID + '" ' +
' preload="auto" class="jp-audio" type="audio/mp3"/>';
$('#cmp_media_container').append(_audioTagHTML);
initMediaLoading('AUDIO',audioPath,audioThumbPath);
}catch(e){ catchError('initMediaPlayerForAudio()',e); }
}
/* ----------------------------------------------------------- */
function initMediaPlayerForVideo(videoTitle,videoPath,videoThumbPath,videoDesc){
report('TEST','[CMP].initMediaPlayerForVideo(videoPath:' + videoPath + ')..');
try{
_closeMediaPlayer();
// REFRESH UI
$('#cordova_mediaplayer,#media_playpause').removeClass('playing disabled');
$('#audio_item,#media_loading_msg').hide();
// VARS
var videoTitle = videoTitle;
if(!videoDesc) videoDesc = "No video description provided.";
var videoDesc = videoDesc;
$('#cordova_mediaplayer').removeClass('audio').addClass('video');
$('#cmp_header h1').html(videoTitle);
$('#cordova_mediaplayer #cmp_desc').html(videoDesc);
$('#media_details').show();
// Remove and Re-Append <video> tag each time
var _videoTagHTML = '<video id="app_video" type="video/mp4" video_path="' + videoPath + '"/>'; //class="media_controls"
$('#app_video').remove();
$('#cmp_media_container').append(_videoTagHTML);
if(isMobile.Android()){
// ANDROID: provide the src on the <video> tag
$('#app_video')
.attr('src',videoPath);
}else{
// iOS:
// do NOT provide the src on the <video> tag
// Instead add <source> node with the src
$('#app_video source').remove(); // remove previous <source> nodes if exist
$('#app_video')
.attr('webkit-playsinline','')
.attr('controls','controls')
.append('<source src="' + videoPath + '" type="video/mp4"/>');
}
initMediaLoading('VIDEO',videoPath,videoThumbPath);
}catch(e){ catchError('initMediaPlayerForVideo()',e); }
}
/* ----------------------------------------------------------- /
_prepUIElementsForMediaPlayback
/ ----------------------------------------------------------- */
function _prepUIElementsForMediaPlayback(){
report('TEST','[CMP]._prepUIElementsForMediaPlayback()..');
try{
// $('#app_media_thumb').css('backgroundImage','url(' + currentMediaThumbPath + ')');
var _appMediaThumbHeight = Math.min(Math.round($('#app_media_thumb').height()*90),480);
var _appMediaThumbWidth = Math.round(window.innerWidth*.90); //$('#app_media_thumb').width();
// DETAILS BOX HEIGHT
// $("#cordova_mediaplayer")// ;;_elements")
// .css("height",_appMediaThumbHeight+"px")
// .css("width",_appMediaThumbWidth+"px");
// update the click/touch event on video play/pause button
$('#media_playpause')
.unbind(clickOrTouchEvent)
.bind(clickOrTouchEvent,function(e){
toggleMediaPlayback();
e.stopPropagation();
});
// Then remove loading class we're done!
$('#cordova_mediaplayer').removeClass('loading');
if(typeof(prepUIElementsForMediaPlayback) == 'function') prepUIElementsForMediaPlayback();
}catch(e){ catchError('_prepUIElementsForMediaPlayback()',e); }
}
/* ----------------------------------------------------------- /
toggleMediaPlayback
/ ----------------------------------------------------------- */
function toggleMediaPlayback(forceMode){
try{
if(!isConnectedToInternet()){ // alert('getConnectionType() [' + getConnectionType() + '] | isConnectedToInternet() [' + isConnectedToInternet() + ']');
doAlert('Video playback requires an internet connection. Please connect this device to a WiFi or a 3G/4G network and try again.','Internet Connection Required');
return false;
}
var _mediaMode = "VIDEO";
if($('#app_audio').length) _mediaMode = "AUDIO";
switch(_mediaMode){
// ===== VIDEO ====================================
case 'VIDEO':
report('TEST','[CMP]. toggleMediaPlayback(VIDEO)..');
var video = document.getElementById("app_video");
// var _paused = ((isMediaPlayerPaused(video)) || (forceMode == 'PAUSED'));
if(currentMediaPaused){ //_paused){
report('TEST','\t ... VIDEO WAS paused ... trying to PLAY ');
initMediaPlayerBuffering('START');
//if(!isMobile.Android()){
var _videoPath = $('#app_video').attr('video_path');
var _currentVideoPath = video.getAttribute("src");
if(_videoPath != currentMediaPath){ //} _currentVideoPath != _videoPath){
video.setAttribute("src", _videoPath);
}
//}
playVideo();
}else{
report('TEST','\t ... VIDEO was NOT paused ... trying to PAUSE ');
currentMediaPlaying = true;
currentMediaPaused = true;
$('#cordova_mediaplayer').removeClass('playing').addClass('paused');
video.pause();
}
break;
// ===== AUDIO ==============================
case 'AUDIO':
report('TEST','[CMP].toggleMediaPlayback(AUDIO)..'); // report('TEST','** AUDIO: paused=' + _paused + ' | currentMediaPaused:' + currentMediaPaused + ' | currentMediaPlaying:' + currentMediaPlaying + '|');
if(currentMediaPaused){
report('TEST','\t ... AUDIO WAS paused ... trying to PLAY ');
if(!currentMediaAlreadyLoaded){
stopAudio();
initMediaPlayerBuffering('START');
clearTimeoutVar(delayedAudioPlaybackTimeout);
delayedAudioPlaybackTimeout = window.setTimeout(function(){
playAudio(currentMediaPath);
},800);
}else{
resumeAudio();
}
}else{
report('TEST','\t ... AUDIO was NOT paused ... trying to PAUSE ');
pauseAudio();
}
break;
}
}catch(e){ catchError('toggleMediaPlayback()',e); }
}
/* ----------------------------------------------------------- /
_mediaPlaybackHasBeenStopped
/ ----------------------------------------------------------- */
function _mediaPlaybackHasBeenStopped(){
report('TEST','[CMP]._mediaPlaybackHasBeenStopped()..');
try{
if(typeof(currentMediaAlreadyLoaded) == 'undefined'){
// alert('setting it!');
currentMediaAlreadyLoaded = false;
}
if(
(currentMediaAlreadyLoaded) ||
(currentMediaPlaying)
){
// only run if one of above vars arent flagged false yet
currentMediaAlreadyLoaded = false;
currentMediaPlaying = false;
currentMediaPaused = true;
initMediaPlayerBuffering('END');
}
$('#cordova_mediaplayer')
.removeClass('playing')
.addClass('paused');
clearAllMediaTimersAndIntervals();
if(typeof(mediaPlaybackHasBeenStopped) == 'function') mediaPlaybackHasBeenStopped();
}catch(e){ catchError('_mediaPlaybackHasBeenStopped()',e); }
}
/* ----------------------------------------------------------- /
_mediaPlaybackHasStarted
/ ----------------------------------------------------------- */
function _mediaPlaybackHasStarted(){
report('TEST','[CMP]._mediaPlaybackHasStarted().. [currentMediaAlreadyLoaded:' + currentMediaAlreadyLoaded + ' | currentMediaPlaying:' + currentMediaPlaying + ']');
try{
//only run if one of main vars is false
if(!currentMediaAlreadyLoaded || !currentMediaPlaying){
report('TEST','!!!!! _mediaPlaybackHasStarted().. SETTING currentMediaAlreadyLoaded to TRUE!');
currentMediaAlreadyLoaded = true;
currentMediaPlaying = true;
currentMediaPaused = false;
initMediaPlayerBuffering('END');
if(!cordovaIsLoaded){
// css flag so we know when we're not in device playback mode
$('#cordova_mediaplayer').addClass('playing_non_device_mode');
}else{
// reset play/pause button on devices
$('#cordova_mediaplayer').removeClass('playing').addClass('paused');
currentMediaAlreadyLoaded = false;
currentMediaPlaying = false;
currentMediaPaused = false;
}
$('#cordova_mediaplayer').removeClass('paused').addClass('playing');
}
if(typeof(mediaPlaybackHasStarted) == 'function') mediaPlaybackHasStarted();
}catch(e){ catchError('_mediaPlaybackHasStarted()',e); }
}
/* ----------------------------------------------------------- /
mediaPlaybackHasBeenPaused
/ ----------------------------------------------------------- */
function mediaPlaybackHasBeenPaused(){
report('TEST','[CMP].mediaPlaybackHasBeenPaused()..');
try{
currentMediaPlaying = false;
currentMediaPaused = true;
$('#cordova_mediaplayer')
.removeClass('playing loading buffering')
.addClass('paused');
}catch(e){ catchError('mediaPlaybackHasBeenPaused()',e); }
}
/* ----------------------------------------------------------- */
function clearAllMediaTimersAndIntervals(){
report('TEST','[CMP].clearAllMediaTimersAndIntervals()..');
try{
clearIntervalVar(audioPlaybackProgressCheckInterval);
clearTimeoutVar(delayedAudioPlaybackTimeout);
clearIntervalVar(delayedVideoPlaybackInterval);
}catch(e){ catchError('clearAllMediaTimersAndIntervals()',e); }
}
function stopVideo(){
//PWreenableAutoLock();
currentMediaPlaying = false;
try{
$('#app_video').remove();
}catch(e){ catchError('stopVideo()',e); }
}
/* -------------------------------------- */
function _closeMediaPlayer(){
report('TEST','_closeMediaPlayer()...');
stopAudio();
stopVideo();
clearAllMediaTimersAndIntervals();
currentMediaAlreadyLoaded = false;
currentMediaPlaying = false;
currentMediaPaused = false;
mediaPlayerClosed = true;
initMediaPlayerBuffering('END');
// PWreenableAutoLock();
// DOM stuff
$('#app_video,#app_audio').remove();
$('#cordova_mediaplayer')
.removeClass('playing paused video audio buffering playing_non_device_mode');
$('#cmp_status_or_position').html("");
$("body").removeClass('media_playback_mode');
$('#cmp_header h1').html("");
$('#cmp_desc').html("");
if(typeof(closeMediaPlayer) == 'function') closeMediaPlayer();
}
/* ----------------------------------------------------------- */
function initMediaGallery(){
report('TEST','[CMP].initMediaGallery()..');
try{
initOrUpdatePodcastList();
initOrUpdateVideoList();
initMediaGalleryEvents();
initMediaGalleryScrolling();
toggleMediaList("SHOW");
}catch(e){ catchError('initMediaGallery()',e); }
}
/* ----------------------------------------------------------- */
function setupCordovaMediaPlayer(playerWidth,playerHeight,defaultTitle, defaultText,defaultThumb){
report('TEST','\nCordova Media Player initializing ... [CMP]\n');
report('TEST','[CMP].setupCordovaMediaPlayer(playerWidth:' + playerWidth + ',playerHeight:' + playerHeight + ')..');
try{
_defaultText = '';
_defaultTitle = '';
if(defaultText) _defaultText = defaultText;
if(defaultTitle) _defaultTitle = defaultTitle;
if($('#cordova_mediaplayer')){
cmpHeight = 270;
cmpWidth = 480;
if(playerHeight) cmpHeight = playerHeight;
if(playerWidth) cmpWidth = playerWidth;
var mediaPlayerHTML = "";
mediaPlayerHTML += '<div id="cmp_loading_spinner" class="processing_spinner rotate"></div>' +
'<div id="cmp_header">' +
' <div class="label_new"></div>' +
' <h1>' + _defaultTitle + '</h1>' +
'</div>' +
'<div id="cmp_media_container">' +
' <div id="app_media_thumb"><img/></div>' +
' <div id="cmp_status_or_position"></div>' +
' <div id="cmp_controls" class="media_controls ">' +
' <div id="media_playpause" class="media_button image_icon playpause_button"></div>' +
' </div>' +
' <div id="cmp_media_elements">' +
' </div>' +
'</div>' +
'<div id="cmp_details">' +
' <div id="cmp_desc">' + _defaultText + '</div>' +
'</div>';
$('#cordova_mediaplayer')
.html(mediaPlayerHTML)
.css({
'width':cmpWidth + 'px'//,
//'height':cmpHeight + 'px'
});
var headerHeight = $('#cmp_header').height();
var availableHeight = cmpHeight-headerHeight;
var controlsHeight = $('#media_playpause').height();
var spinnerHeight = $('#cmp_loading_spinner').height();
// alert('a: ' + headerHeight + ', b:' + availableHeight + ', c:' + controlsHeight);
// base media thumb sizing
$('#cmp_media_container').css('height',availableHeight + 'px');
$('#app_media_thumb').css({
'width':cmpWidth + 'px',
'height':availableHeight + 'px'
});
// status label position
//$('#cmp_status_or_position').css('top',(headerHeight+10) + 'px');
// control buttons position
$('#cmp_controls').css('top',(availableHeight-(controlsHeight+20)) + 'px');
$('#cmp_media_elements').css('top',(availableHeight + 10) + 'px');
$('#cmp_loading_spinner').css('top', ((availableHeight/2) + (spinnerHeight/2)) + 'px');
if(defaultThumb) refreshMediaThumbnail(defaultThumb);
// DEVICE MODE
// if we're on an actual device, this class will always hide the
// <video> element since we never actually play it inline in this example
// project. Note if inline playback is desired this class can be overridden via CSS
// such as body.tablet_mode #cordova_mediaplayer.device_mode video { display:block !important; }
if(cordovaIsLoaded){
$('#cordova_mediaplayer').addClass('device_mode');
}
}
}catch(e){ catchError('setupCordovaMediaPlayer()',e); }
}
/* ----------------------------------------------------------- */
function pauseAllMedia(){
report('TEST','[CMP].pauseAllMedia()..');
try{
if(currentMediaPlaying){
toggleMediaPlayback('PAUSE');
}
}catch(e){ catchError('pauseAllMedia()',e); }
}
/* ----------------------------------------------------------- /
isMediaPlayerPaused
/ ----------------------------------------------------------- */
function isMediaPlayerPaused(audioOrVideoItem){
var p = false;
var _mediaElementIsPaused = false;
try{
_mediaElementIsPaused = (audioOrVideoItem.paused);
}catch(e){ catchError('isMediaPlayerPaused()',e); }
try{
p = _mediaElementIsPaused;
}catch(e){ catchError('isMediaPlayerPaused()',e); }
report('TEST','[CMP].isMediaPlayerPaused()..paused:' + p + '? [_mediaElementIsPaused:' + _mediaElementIsPaused + '], [currentMediaPlaying:' + currentMediaPlaying + '], [currentMediaPaused:' + currentMediaPaused + ']');
return p;
}
/* ----------------------------------------------------------- /
initMediaLoading
/ ----------------------------------------------------------- */
function initMediaLoading(videoOrPodcast,mediaPath,mediaThumbPath){
report('TEST','[CMP].initMediaLoading(videoOrPodcast:' + videoOrPodcast + ', mediaPath:' + mediaPath + ', mediaThumbPath:' + mediaThumbPath + ')..');
try{
$('#cordova_mediaplayer').addClass('loading');
$('#cordova_mediaplayer div.label_new').hide();
currentMediaPlaying = false;
currentMediaPaused = true;
currentMediaAlreadyLoaded = false;
currentMediaPath = fixLocalFilePathsForAndroid(mediaPath);
currentMediaThumbPath = mediaThumbPath;
// MEDIA THUMB
$('#app_media_thumb').css('background-image','none');
$('#cordova_mediaplayer').addClass('loading');
refreshMediaThumbnail(currentMediaThumbPath,_prepUIElementsForMediaPlayback);
}catch(e){ catchError('initMediaLoading()',e); }
}
function refreshMediaThumbnail(thumbPath,postThumbLoadCallback){
try{
$('<img/>').attr('src', thumbPath).load(function() {
$(this).remove();
$('#app_media_thumb').css({'background-image':'url(' + thumbPath + ')'});
// run callback after loading finished if desired
if(postThumbLoadCallback) postThumbLoadCallback();
});
}catch(e){ catchError('refreshMediaThumbnail()',e); }
}
function initMediaPlayerBuffering(startOrEnd){
report('TEST','[CMP].initMediaPlayerBuffering(' + startOrEnd + ')...');
try{
$('#media_playpause').empty();
switch(startOrEnd){
case 'START':
refreshMediaPositionLabel(termLoading + " ...");
$('#cordova_mediaplayer').addClass('buffering');
$('#media_playpause')
.append('<div class="processing_spinner rotate">');
break;
case 'END':
default:
$('#cordova_mediaplayer .media_button .processing_spinner').remove();
$('#cordova_mediaplayer').removeClass('buffering');
clearIntervalVar(delayedVideoPlaybackInterval);
clearTimeoutVar(delayedAudioPlaybackTimeout);
break;
}
}catch(e){ catchError('initMediaPlayerBuffering()',e); }
}
function androidLocalVideoPlaybackMethodRequired(){
if(!isMobile.Android()) return false;
var isRequired = false;
try{
isRequired = ((isMobile.Android()));
}catch(e){
catchError('androidLocalVideoPlaybackMethodRequired()',e);
}
report('TEST','[CMP].androidLocalVideoPlaybackMethodRequired() [' + isRequired + ']');
return isRequired;
}
/* ----------------------------------------------------------- /
playVideo
/ ----------------------------------------------------------- */
function playVideo(){
try{
// stop existing video?
report('TEST','[CMP].playVideo() [currentMediaPath:' + currentMediaPath + ']');
var video = document.getElementById("app_video");
initMediaPlayerBuffering('START');
clearIntervalVar(delayedVideoPlaybackInterval);
if(androidLocalVideoPlaybackMethodRequired()){
window.plugins.videoPlayer.play(currentMediaPath);
initMediaPlayerBuffering('END');
return true;
}else{
// OTHERWISE CONTINUE
video.play();
video.pause();
}
delayedVideoPlaybackInterval = window.setInterval(function(){
var seek = 0;
try{
if(video.seekable) seek = video.seekable.end(0);
}catch(e){
// we tried
}
report('TEST','playVideo() [CMP].video play interval: readyState=[' + video.readyState + '] seek=[' + seek + '] androidLocalVideoPlaybackMethodRequired=[' + androidLocalVideoPlaybackMethodRequired() + ']');
var videoReadyForPlayback = (
(parseInt(video.readyState) >= 3) ||
((seek > 30) && (parseInt(seek) != 6000)) ||
(androidLocalVideoPlaybackMethodRequired())
);
if(videoReadyForPlayback){
report('TEST','\t... READY FOR PLAYBACK(?) [playVideo() [CMP].video.play()]');
_mediaPlaybackHasStarted();
// FINALLY start video playback!
if(androidLocalVideoPlaybackMethodRequired()){
// video already playing via Android video plugin
}else{
// jump back to start of video (ios sometimes caches/saved last played video's currentTIme)
video.currentTime=0;
video.play();
}
}else{
report('TEST','\t... playVideo() [CMP].buffering...');
// keep waiting...
}
},2000);
}catch(e){ catchError('playVideo()',e); }
}
console.log('[cordova-mediaplayer.js] LOADED...');