Repository navigation
Expand file tree
/
Copy pathPlayer.js
More file actions
executable file
·910 lines (776 loc) · 29.6 KB
/
Copy pathPlayer.js
File metadata and controls
executable file
·910 lines (776 loc) · 29.6 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
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
'use strict';
// We have four types of the playing sources
// These are for player to know which source type is playing
var TYPE_MIX = 'mix';
var TYPE_LIST = 'list';
var TYPE_SINGLE = 'single';
var TYPE_BLOB = 'blob';
// Repeat option for player
var REPEAT_OFF = 0;
var REPEAT_LIST = 1;
var REPEAT_SONG = 2;
// AVRCP spec defined the statuses in capitalized and to be simple,
// our player just use them instead of defining new constant strings.
var PLAYSTATUS_STOPPED = 'STOPPED';
var PLAYSTATUS_PLAYING = 'PLAYING';
var PLAYSTATUS_PAUSED = 'PAUSED';
var PLAYSTATUS_FWD_SEEK = 'FWD_SEEK';
var PLAYSTATUS_REV_SEEK = 'REV_SEEK';
var PLAYSTATUS_ERROR = 'ERROR';
// We get headphoneschange event when the headphones is plugged or unplugged
// A related Bug 809106 in Bugzilla
var acm = navigator.mozAudioChannelManager;
if (acm) {
acm.addEventListener('headphoneschange', function onheadphoneschange() {
if (!acm.headphones && PlayerView.playStatus === PLAYSTATUS_PLAYING) {
PlayerView.pause();
}
});
}
window.addEventListener('visibilitychange', function() {
if (document.hidden) {
PlayerView.audio.removeEventListener('timeupdate', PlayerView);
} else {
PlayerView.audio.addEventListener('timeupdate', PlayerView);
}
});
// View of Player
var PlayerView = {
get view() {
delete this._view;
return this._view = document.getElementById('views-player');
},
get audio() {
delete this._audio;
return this._audio = document.getElementById('player-audio');
},
get playStatus() {
return this._playStatus;
},
set playStatus(val) {
this._playStatus = val;
},
get dataSource() {
return this._dataSource;
},
set dataSource(source) {
this._dataSource = source;
if (this.sourceType) {
if (this.sourceType === TYPE_MIX || this.sourceType === TYPE_LIST) {
// Shuffle button will be disabled if an album only contains one song
this.shuffleButton.disabled = (this._dataSource.length < 2);
// Also, show or hide the Now Playing button depending on
// whether content is queued
TitleBar.playerIcon.hidden = (this._dataSource.length < 1);
} else {
// These buttons aren't necessary when playing a blob or a single track
this.shuffleButton.disabled = true;
this.repeatButton.disabled = true;
this.previousControl.disabled = true;
this.nextControl.disabled = true;
}
}
},
init: function pv_init() {
this.artist = document.getElementById('player-cover-artist');
this.album = document.getElementById('player-cover-album');
this.timeoutID;
this.cover = document.getElementById('player-cover');
this.coverImage = document.getElementById('player-cover-image');
this.offscreenImage = new Image();
this.shareButton = document.getElementById('player-cover-share');
this.repeatButton = document.getElementById('player-album-repeat');
this.shuffleButton = document.getElementById('player-album-shuffle');
this.ratings = document.getElementById('player-album-rating').children;
this.seekRegion = document.getElementById('player-seek-bar');
this.seekBar = document.getElementById('player-seek-bar-progress');
this.seekIndicator = document.getElementById('player-seek-bar-indicator');
this.seekElapsed = document.getElementById('player-seek-elapsed');
this.seekRemaining = document.getElementById('player-seek-remaining');
this.playControl = document.getElementById('player-controls-play');
this.previousControl = document.getElementById('player-controls-previous');
this.nextControl = document.getElementById('player-controls-next');
this.isTouching = false;
this.playStatus = PLAYSTATUS_STOPPED;
this.pausedPosition = null;
this.dataSource = [];
this.playingBlob = null;
this.currentIndex = 0;
this.setSeekBar(0, 0, 0); // Set 0 to default seek position
this.intervalID = null;
this.isContextmenu = false;
this.view.addEventListener('click', this);
this.view.addEventListener('contextmenu', this);
// Seeking audio too frequently causes the Desktop build hangs
// A related Bug 739094 in Bugzilla
this.seekRegion.addEventListener('touchstart', this);
this.seekRegion.addEventListener('touchmove', this);
this.seekRegion.addEventListener('touchend', this);
this.previousControl.addEventListener('touchend', this);
this.nextControl.addEventListener('touchend', this);
this.audio.addEventListener('play', this);
this.audio.addEventListener('pause', this);
this.audio.addEventListener('playing', this);
this.audio.addEventListener('durationchange', this);
this.audio.addEventListener('timeupdate', this);
this.audio.addEventListener('ended', this);
// A timer we use to work around
// https://bugzilla.mozilla.org/show_bug.cgi?id=783512
this.endedTimer = null;
},
clean: function pv_clean() {
// Cancel a pending enumeration before start a new one
if (typeof playerHandle !== 'undefined' && playerHandle)
musicdb.cancelEnumeration(playerHandle);
this.dataSource = [];
this.playingBlob = null;
},
setSourceType: function pv_setSourceType(type) {
this.sourceType = type;
},
// We only use the DBInfo for playing all songs.
setDBInfo: function pv_setDBInfo(info) {
this.DBInfo = info;
this.dataSource.length = info.count;
},
// This function is for the animation on the album art (cover).
// The info (album, artist) will initially show up when a song being played,
// if users does not tap the album art (cover) again,
// then it will be disappeared after 5 seconds
// however, if a user taps before 5 seconds ends,
// then the timeout will be cleared to keep the info on screen.
showInfo: function pv_showInfo() {
this.cover.classList.add('slideOut');
if (this.timeoutID)
window.clearTimeout(this.timeoutID);
this.timeoutID = window.setTimeout(
function pv_hideInfo() {
this.cover.classList.remove('slideOut');
}.bind(this),
5000
);
},
setInfo: function pv_setInfo(fileinfo) {
var metadata = fileinfo.metadata;
// Handle the title bar and the share button when the player is not launched
// by open activity.
if (typeof ModeManager !== 'undefined') {
ModeManager.playerTitle = metadata.title;
ModeManager.updateTitle();
// If it is a locked music file, or if we are handling a Pick activity
// then we should not give the user the option of sharing the file.
if (metadata.locked || pendingPick) {
this.shareButton.classList.add('hidden');
this.artist.classList.add('hidden-cover-share');
this.album.classList.add('hidden-cover-share');
}
else {
this.shareButton.classList.remove('hidden');
this.artist.classList.remove('hidden-cover-share');
this.album.classList.remove('hidden-cover-share');
}
} else {
var titleBar = document.getElementById('title-text');
titleBar.textContent = metadata.title || unknownTitle;
titleBar.dataset.l10nId = metadata.title ? '' : unknownTitleL10nId;
}
this.artist.textContent = metadata.artist || unknownArtist;
this.artist.dataset.l10nId = metadata.artist ? '' : unknownArtistL10nId;
this.album.textContent = metadata.album || unknownAlbum;
this.album.dataset.l10nId = metadata.album ? '' : unknownAlbumL10nId;
this.setCoverImage(fileinfo);
},
setCoverImage: function pv_setCoverImage(fileinfo) {
// Reset the image to be ready for fade-in
this.offscreenImage.src = '';
this.coverImage.classList.remove('fadeIn');
getThumbnailURL(fileinfo, function(url) {
url = url || generateDefaultThumbnailURL(fileinfo.metadata);
this.offscreenImage.addEventListener('load', pv_showImage.bind(this));
this.offscreenImage.src = url;
}.bind(this));
function pv_showImage(evt) {
evt.target.removeEventListener('load', pv_showImage);
var url = 'url(' + this.offscreenImage.src + ')';
this.coverImage.style.backgroundImage = url;
this.coverImage.classList.add('fadeIn');
};
},
setOptions: function pv_setOptions(settings) {
var repeatOption = (settings && settings.repeat) ?
settings.repeat : REPEAT_OFF;
var shuffleOption = (settings && settings.shuffle) ?
settings.shuffle : false;
this.setRepeat(repeatOption);
this.setShuffle(shuffleOption);
},
setRepeat: function pv_setRepeat(value) {
var repeatClasses = ['repeat-off', 'repeat-list', 'repeat-song'];
// Remove all repeat classes before applying a new one
repeatClasses.forEach(function pv_resetRepeat(targetClass) {
this.repeatButton.classList.remove(targetClass);
}.bind(this));
this.repeatOption = value;
this.repeatButton.classList.add(repeatClasses[this.repeatOption]);
},
setShuffle: function pv_setShuffle(value, index) {
this.shuffleOption = value;
if (this.shuffleOption) {
this.shuffleButton.classList.add('shuffle-on');
// if index exists, that means player is playing a list,
// so shuffle that list with the index
// or just create one with a random number
if (arguments.length > 1) {
this.shuffleList(this.currentIndex);
} else {
this.shuffleList();
}
} else {
this.shuffleButton.classList.remove('shuffle-on');
}
},
setRatings: function pv_setRatings(rated) {
for (var i = 0; i < 5; i++) {
var rating = this.ratings[i];
if (i < rated) {
rating.classList.add('star-on');
} else {
rating.classList.remove('star-on');
}
}
},
shuffleList: function slv_shuffleList(index) {
if (this.dataSource.length === 0)
return;
this.shuffleIndex = 0;
this.shuffledList = [];
for (var i = 0; i < this.dataSource.length; i++)
this.shuffledList.push(i);
// If with an index, that means the index is the currectIndex
// so it doesn't need to be shuffled
// It will be placed to the first element of shuffled list
// then we append the rest shuffled indexes to it
// to become a new shuffled list
if (arguments.length > 0) {
var currentItem = this.shuffledList.splice(index, 1);
slv_shuffle(this.shuffledList);
this.shuffledList = currentItem.concat(this.shuffledList);
} else {
slv_shuffle(this.shuffledList);
}
// shuffle the elements of array a in place
// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
function slv_shuffle(a) {
for (var i = a.length - 1; i >= 1; i--) {
var j = Math.floor(Math.random() * (i + 1));
if (j < i) {
var tmp = a[j];
a[j] = a[i];
a[i] = tmp;
}
}
}
},
getMetadata: function pv_getMetadata(blob, callback) {
parseAudioMetadata(blob, pv_gotMetadata, pv_metadataError.bind(this));
function pv_gotMetadata(metadata) {
callback(metadata);
}
function pv_metadataError(e) {
if (this.onerror)
this.onerror(e);
console.warn('parseAudioMetadata: error parsing metadata - ', e);
}
},
setAudioSrc: function pv_setAudioSrc(file) {
var url = URL.createObjectURL(file);
this.playingBlob = file;
// Reset src before we set a new source to the audio element
this.audio.removeAttribute('src');
this.audio.load();
// Add mozAudioChannelType to the player
this.audio.mozAudioChannelType = 'content';
this.audio.src = url;
this.audio.load();
this.audio.play();
// An object URL must be released by calling URL.revokeObjectURL()
// when we no longer need them
this.audio. { URL.revokeObjectURL(url); };
this.audio. {
if (this.onerror)
this.onerror(evt);
}).bind(this);
// when play a new song, reset the seekBar first
// this can prevent showing wrong duration
// due to b2g cannot get some mp3's duration
// and the seekBar can still show 00:00 to -00:00
this.setSeekBar(0, 0, 0);
if (this.endedTimer) {
clearTimeout(this.endedTimer);
this.endedTimer = null;
}
},
updateRemoteMetadata: function pv_updateRemoteMetadata() {
// If MusicComms does not exist or data source is empty, we don't have to
// update the metadata.
if (typeof MusicComms === 'undefined' || this.dataSource.length === 0)
return;
// Update the playing information to AVRCP devices
var fileinfo = this.dataSource[this.currentIndex];
var metadata = fileinfo.metadata;
// AVRCP expects the duration in ms, note that it's converted from s to ms.
var notifyMetadata = {
title: metadata.title || unknownTitle,
artist: metadata.artist || unknownArtist,
album: metadata.album || unknownAlbum,
duration: this.audio.duration * 1000,
mediaNumber: this.currentIndex + 1,
totalMediaCount: this.dataSource.length
};
// Grab the album art if this is a new song; otherwise, don't bother, since
// listeners should already have the album art. Note: if no .picture
// attribute is in the metadata, then listeners should reuse the previous
// picture. If .picture is null, something went wrong and listeners should
// probably use a blank picture (or their own placeholder).
if (this.audio.currentTime === 0) {
getAlbumArtBlob(fileinfo, function(err, blob) {
if (!err) {
if (blob)
notifyMetadata.picture = blob;
} else {
notifyMetadata.picture = null;
}
MusicComms.notifyMetadataChanged(notifyMetadata);
});
}
else {
MusicComms.notifyMetadataChanged(notifyMetadata);
}
},
updateRemotePlayStatus: function pv_updateRemotePlayStatus() {
// If MusicComms does not exist then no need to update the play status.
if (typeof MusicComms === 'undefined')
return;
var position = this.pausedPosition ?
this.pausedPosition : this.audio.currentTime;
var info = {
playStatus: this.playStatus,
duration: this.audio.duration * 1000,
position: position * 1000
};
// Before we resume the player, we need to keep the paused position
// because once the connected A2DP device receives different positions
// on AFTER paused and BEFORE playing, it will break the play/pause states
// that the A2DP device kept.
this.pausedPosition = (this.playStatus === PLAYSTATUS_PLAYING) ?
null : this.audio.currentTime;
// Notify the remote device that status is changed.
MusicComms.notifyStatusChanged(info);
},
// The song data might return from the existed dataSource
// or we will retrieve it directly from the MediaDB.
getSongData: function pv_getSongData(index, callback) {
var info = this.DBInfo;
var songData = this.dataSource[index];
if (songData) {
callback(songData);
} else {
// Cancel the ongoing enumeration so that it will not
// slow down the next enumeration if we start a new one.
ListView.cancelEnumeration();
var handle =
musicdb.advancedEnumerate(
info.key, info.range, info.direction, index, function(record) {
musicdb.cancelEnumeration(handle);
this.dataSource[index] = record;
callback(record);
}.bind(this)
);
}
},
/*
* Get a blob for the specified song, decrypting it if necessary,
* and pass it to the specified callback
*/
getFile: function pv_getFile(songData, callback) {
if (!songData.metadata.locked) {
musicdb.getFile(songData.name, callback);
return;
}
// If here, then this is a locked music file, so we have
// to decrypt it before playing it.
musicdb.getFile(songData.name, function(locked) {
ForwardLock.getKey(function(secret) {
ForwardLock.unlockBlob(secret, locked, function(unlocked) {
callback(unlocked);
}, null, function(msg) {
console.error(msg);
callback(null);
});
});
});
},
play: function pv_play(targetIndex) {
this.showInfo();
if (arguments.length > 0) {
this.getSongData(targetIndex, function(songData) {
this.currentIndex = targetIndex;
this.setInfo(songData);
// set ratings of the current song
this.setRatings(songData.metadata.rated);
// update the metadata of the current song
songData.metadata.played++;
musicdb.updateMetadata(songData.name, songData.metadata);
this.getFile(songData, function(file) {
this.setAudioSrc(file);
// When we need to preview an audio like in picker mode,
// we will not autoplay the picked song unless the user taps to play
// And we just call pause right after play.
if (this.sourceType === TYPE_SINGLE)
this.pause();
}.bind(this));
}.bind(this));
} else if (this.sourceType === TYPE_BLOB && !this.audio.src) {
// When we have to play a blob, we need to parse the metadata
this.getMetadata(this.dataSource, function(metadata) {
// Add the blob from the dataSource to the fileinfo
// because we want use the cover image which embedded in that blob
// so that we don't have to count on the musicdb
this.setInfo({metadata: metadata,
name: this.dataSource.name,
blob: this.dataSource});
this.setAudioSrc(this.dataSource);
}.bind(this));
} else {
// If we reach here, the player is paused so resume it
this.audio.play();
}
},
pause: function pv_pause() {
this.audio.pause();
},
stop: function pv_stop() {
this.pause();
this.audio.removeAttribute('src');
this.audio.load();
this.clean();
// Player in open activity does not have ModeManager.
if (typeof ModeManager !== 'undefined') {
ModeManager.playerTitle = null;
// To leave player mode and set the correct title to the TitleBar
// we have to decide which mode we should back to when the player stops
if (ModeManager.currentMode === MODE_PLAYER) {
ModeManager.pop();
} else {
ModeManager.updateTitle();
}
}
this.playStatus = PLAYSTATUS_STOPPED;
this.updateRemotePlayStatus();
},
next: function pv_next(isAutomatic) {
if (this.sourceType === TYPE_BLOB || this.sourceType === TYPE_SINGLE) {
// When the player ends, reassign src it if the dataSource is a blob
this.setAudioSrc(this.playingBlob);
this.pause();
return;
}
// We only repeat a song automatically. (when the song is ended)
// If users click skip forward, player will go on to next one
if (this.repeatOption === REPEAT_SONG && isAutomatic) {
this.play(this.currentIndex);
return;
}
var playingIndex = (this.shuffleOption) ?
this.shuffleIndex : this.currentIndex;
// If it's a last song and repeat list is OFF, ignore it.
// but if repeat list is ON, player will restart from the first song
if (playingIndex >= this.dataSource.length - 1) {
if (this.repeatOption === REPEAT_LIST) {
if (this.shuffleOption) {
// After finished one round of shuffled list,
// re-shuffle again and start from the first song of shuffled list
this.shuffleList(this.shuffledList[0]);
} else {
this.currentIndex = 0;
}
} else {
// When reaches the end, stop and back to the previous mode
this.stop();
return;
}
} else {
if (this.shuffleOption) {
this.shuffleIndex++;
} else {
this.currentIndex++;
}
}
var realIndex = (this.shuffleOption) ?
this.shuffledList[this.shuffleIndex] : this.currentIndex;
this.play(realIndex);
},
previous: function pv_previous() {
// If a song starts more than 3 (seconds),
// when users click skip backward, it will restart the current song
// otherwise just skip to the previous song
if (this.audio.currentTime > 3) {
this.play(this.currentIndex);
return;
}
var playingIndex = (this.shuffleOption) ?
this.shuffleIndex : this.currentIndex;
// If it's a first song and repeat list is ON, go to the last one
// or just restart from the beginning when repeat list is OFF
if (playingIndex <= 0) {
var newIndex = (this.repeatOption === REPEAT_LIST) ?
this.dataSource.length - 1 : 0;
if (this.shuffleOption) {
this.shuffleIndex = newIndex;
} else {
this.currentIndex = newIndex;
}
} else {
if (this.shuffleOption) {
this.shuffleIndex--;
} else {
this.currentIndex--;
}
}
var realIndex = (this.shuffleOption) ?
this.shuffledList[this.shuffleIndex] : this.currentIndex;
this.play(realIndex);
},
startFastSeeking: function pv_startFastSeeking(direction) {
// direction can be 1 or -1, 1 means forward and -1 means rewind.
this.isTouching = true;
var offset = direction * 2;
this.playStatus = direction ? PLAYSTATUS_FWD_SEEK : PLAYSTATUS_REV_SEEK;
this.updateRemotePlayStatus();
this.intervalID = window.setInterval(function() {
this.seekAudio(this.audio.currentTime + offset);
}.bind(this), 15);
},
stopFastSeeking: function pv_stopFastSeeking() {
this.isTouching = false;
if (this.intervalID)
window.clearInterval(this.intervalID);
// After we cancel the fast seeking, an 'playing' will be fired,
// so that we don't have to update the remote play status here.
},
updateSeekBar: function pv_updateSeekBar() {
// Don't update the seekbar when the user is seeking.
if (this.isTouching)
return;
// If ModeManager is undefined, then the music app is launched by the open
// activity. Otherwise, only seek the audio when the mode is PLAYER because
// updating the UI will slow down the other pages, such as the scrolling in
// ListView.
if (typeof ModeManager === 'undefined' ||
ModeManager.currentMode === MODE_PLAYER &&
this.playStatus === PLAYSTATUS_PLAYING) {
this.seekAudio();
}
},
seekAudio: function pv_seekAudio(seekTime) {
if (seekTime !== undefined)
this.audio.currentTime = seekTime;
var startTime = this.audio.startTime;
var endTime =
(this.audio.duration && this.audio.duration != 'Infinity') ?
this.audio.duration :
this.audio.buffered.end(this.audio.buffered.length - 1);
var currentTime = this.audio.currentTime;
this.setSeekBar(startTime, endTime, currentTime);
},
setSeekBar: function pv_setSeekBar(startTime, endTime, currentTime) {
this.seekBar.min = startTime;
this.seekBar.max = endTime;
this.seekBar.value = currentTime;
// if endTime is 0, that's a reset of seekBar
var ratio = (endTime != 0) ? (currentTime / endTime) : 0;
// The width of the seek indicator must be also considered
// so we divide the width of seek indicator by 2 to find the center point
var x = (ratio * this.seekBar.offsetWidth -
this.seekIndicator.offsetWidth / 2) + 'px';
this.seekIndicator.style.transform = 'translateX(' + x + ')';
this.seekElapsed.textContent = formatTime(currentTime);
var remainingTime = endTime - currentTime;
// Check if there is remaining time to show, avoiding to display "-00:00"
// while song is loading (Bug 833710)
this.seekRemaining.textContent =
(remainingTime > 0) ? '-' + formatTime(remainingTime) : '---:--';
},
share: function pv_shareFile() {
// We try to fix Bug 814323 by using
// current workaround of bluetooth transfer
// so we will pass both filenames and filepaths
// The filepaths can be removed after Bug 811615 is fixed
var songData = this.dataSource[this.currentIndex];
if (songData.metadata.locked)
return;
musicdb.getFile(songData.name, function(file) {
var filename = songData.name,
name = filename.substring(filename.lastIndexOf('/') + 1),
type = file.type;
// And we just want the first component of the type "audio" or "video".
type = type.substring(0, type.indexOf('/')) + '/*';
var a = new MozActivity({
name: 'share',
data: {
type: type,
number: 1,
blobs: [file],
filenames: [name],
filepaths: [filename]
}
});
a. {
console.warn('share activity error:', a.error.name);
};
});
},
handleEvent: function pv_handleEvent(evt) {
var target = evt.target;
if (!target)
return;
switch (evt.type) {
case 'click':
switch (target.id) {
case 'player-cover':
case 'player-cover-image':
this.showInfo();
break;
case 'player-controls-play':
if (this.playStatus === PLAYSTATUS_PLAYING)
this.pause();
else
this.play();
break;
case 'player-album-repeat':
this.showInfo();
var newValue = ++this.repeatOption % 3;
// Store the option when it's triggered by users
asyncStorage.setItem(SETTINGS_OPTION_KEY, {
repeat: newValue,
shuffle: this.shuffleOption
});
this.setRepeat(newValue);
break;
case 'player-album-shuffle':
this.showInfo();
var newValue = !this.shuffleOption;
// Store the option when it's triggered by users
asyncStorage.setItem(SETTINGS_OPTION_KEY, {
repeat: this.repeatOption,
shuffle: newValue
});
this.setShuffle(newValue, this.currentIndex);
break;
case 'player-cover-share':
this.share();
break;
}
if (target.dataset.rating) {
this.showInfo();
var songData = this.dataSource[this.currentIndex];
var targetRating = parseInt(target.dataset.rating);
var newRating = (targetRating === songData.metadata.rated) ?
targetRating - 1 : targetRating;
songData.metadata.rated = newRating;
musicdb.updateMetadata(songData.name, songData.metadata);
this.setRatings(newRating);
}
break;
case 'play':
this.playControl.classList.remove('is-pause');
break;
case 'pause':
this.playControl.classList.add('is-pause');
this.playStatus = PLAYSTATUS_PAUSED;
this.updateRemotePlayStatus();
break;
case 'playing':
// The playing event fires when the audio is ready to start.
this.playStatus = PLAYSTATUS_PLAYING;
this.updateRemotePlayStatus();
break;
case 'touchstart':
case 'touchmove':
if (evt.type === 'touchstart') {
this.isTouching = true;
this.seekIndicator.classList.add('highlight');
}
if (this.isTouching && this.audio.duration > 0) {
// target is the seek bar
var touch = evt.touches[0];
var x = (touch.clientX - target.offsetLeft) / target.offsetWidth;
if (x < 0)
x = 0;
if (x > 1)
x = 1;
this.seekTime = x * this.seekBar.max;
this.setSeekBar(this.audio.startTime,
this.audio.duration, this.seekTime);
}
break;
case 'touchend':
// If isContextmenu is true then the event is trigger by the long press
// of the previous or next buttons, so stop the fast seeking.
// Otherwise, check the target id then do the corresponding actions.
if (this.isContextmenu) {
this.isContextmenu = false;
this.stopFastSeeking();
} else if (target.id === 'player-seek-bar') {
this.seekIndicator.classList.remove('highlight');
if (this.audio.duration > 0 && this.isTouching) {
this.seekAudio(this.seekTime);
this.seekTime = 0;
}
this.isTouching = false;
} else if (target.id === 'player-controls-previous') {
this.previous();
} else if (target.id === 'player-controls-next') {
this.next();
}
break;
case 'contextmenu':
this.isContextmenu = true;
if (target.id === 'player-controls-next')
this.startFastSeeking(1);
if (target.id === 'player-controls-previous')
this.startFastSeeking(-1);
break;
case 'durationchange':
case 'timeupdate':
this.updateSeekBar();
// Update the metadata when the new track is really loaded
// when it just started to play, or the duration will be 0 then it will
// break the duration that the connected A2DP has.
if (evt.type === 'durationchange' || this.audio.currentTime === 0)
this.updateRemoteMetadata();
// Since we don't always get reliable 'ended' events, see if
// we've reached the end this way.
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=783512
// If we're within 1 second of the end of the song, register
// a timeout to skip to the next song one second after the song ends
if (this.audio.currentTime >= this.audio.duration - 1 &&
this.endedTimer == null) {
var timeToNext = (this.audio.duration - this.audio.currentTime + 1);
this.endedTimer = setTimeout(function() {
this.next(true);
}.bind(this),
timeToNext * 1000);
}
break;
case 'ended':
// Because of the workaround above, we have to ignore real ended
// events if we already have a timer set to emulate them
if (!this.endedTimer)
this.next(true);
break;
default:
return;
}
}
};