Skip to content

Commit f013a39

Browse files
committed
add option mutex (pause other players when started)
1 parent d34525e commit f013a39

5 files changed

Lines changed: 46 additions & 27 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ ap.init();
6363
narrow: false, // Optional, narrow style
6464
autoplay: true, // Optional, autoplay song(s), not supported by mobile browsers
6565
showlrc: false, // Optional, show lrc
66+
mutex: true, // Optional, pause other players when this player playing
6667
theme: '#e6d0b2', // Optional, theme color, default: #b7daff
6768
music: { // Required, music info
6869
title: 'Preparation', // Required, music title

dist/APlayer.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/APlayer.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "aplayer",
3-
"version": "1.3.7",
3+
"version": "1.3.8",
44
"description": "Wow, such a beautiful html5 music player",
55
"main": "dist/APlayer.min.js",
66
"scripts": {

src/APlayer.js

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
* @constructor
66
*/
77
(() => {
8+
let APlayers = [];
9+
810
class APlayer {
911
constructor (option) {
1012

@@ -19,6 +21,7 @@
1921
element: document.getElementsByClassName('aplayer')[0],
2022
narrow: false,
2123
autoplay: false,
24+
mutex: true,
2225
showlrc: false,
2326
theme: '#b7daff'
2427
};
@@ -366,6 +369,8 @@
366369
}
367370

368371
this.setMusic(0);
372+
373+
APlayers.push(this);
369374
};
370375

371376
/**
@@ -498,37 +503,50 @@
498503
* Play music
499504
*/
500505
play () {
501-
this.button.classList.remove('aplayer-play');
502-
this.button.classList.add('aplayer-pause');
503-
this.button.innerHTML = '';
504-
setTimeout(() => {
505-
this.button.innerHTML = '<i class="demo-icon aplayer-icon-pause"></i>';
506-
}, 100);
507-
this.audio.play();
508-
if (this.playedTime) {
509-
clearInterval(this.playedTime);
510-
}
511-
this.playedTime = setInterval(() => {
512-
this.updateBar('played', this.audio.currentTime / this.audio.duration, 'width');
513-
if (this.option.showlrc) {
514-
this.updateLrc();
506+
if (this.audio.paused) {
507+
this.button.classList.remove('aplayer-play');
508+
this.button.classList.add('aplayer-pause');
509+
this.button.innerHTML = '';
510+
setTimeout(() => {
511+
this.button.innerHTML = '<i class="demo-icon aplayer-icon-pause"></i>';
512+
}, 100);
513+
514+
// pause other players (Thanks @Aprikyblue)
515+
if (this.option.mutex) {
516+
for (let i = 0; i < APlayers.length; i++) {
517+
if (this != APlayers[i]) {
518+
APlayers[i].pause();
519+
}
520+
}
521+
}
522+
this.audio.play();
523+
if (this.playedTime) {
524+
clearInterval(this.playedTime);
515525
}
516-
this.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = this.secondToTime(this.audio.currentTime);
517-
}, 100);
526+
this.playedTime = setInterval(() => {
527+
this.updateBar('played', this.audio.currentTime / this.audio.duration, 'width');
528+
if (this.option.showlrc) {
529+
this.updateLrc();
530+
}
531+
this.element.getElementsByClassName('aplayer-ptime')[0].innerHTML = this.secondToTime(this.audio.currentTime);
532+
}, 100);
533+
}
518534
};
519535

520536
/**
521537
* Pause music
522538
*/
523539
pause () {
524-
this.button.classList.remove('aplayer-pause');
525-
this.button.classList.add('aplayer-play');
526-
this.button.innerHTML = '';
527-
setTimeout(() => {
528-
this.button.innerHTML = '<i class="demo-icon aplayer-icon-play"></i>';
529-
}, 100);
530-
this.audio.pause();
531-
clearInterval(this.playedTime);
540+
if (!this.audio.paused) {
541+
this.button.classList.remove('aplayer-pause');
542+
this.button.classList.add('aplayer-play');
543+
this.button.innerHTML = '';
544+
setTimeout(() => {
545+
this.button.innerHTML = '<i class="demo-icon aplayer-icon-play"></i>';
546+
}, 100);
547+
this.audio.pause();
548+
clearInterval(this.playedTime);
549+
}
532550
};
533551
}
534552

0 commit comments

Comments
 (0)