|
5 | 5 | * @constructor |
6 | 6 | */ |
7 | 7 | (() => { |
| 8 | + let APlayers = []; |
| 9 | + |
8 | 10 | class APlayer { |
9 | 11 | constructor (option) { |
10 | 12 |
|
|
19 | 21 | element: document.getElementsByClassName('aplayer')[0], |
20 | 22 | narrow: false, |
21 | 23 | autoplay: false, |
| 24 | + mutex: true, |
22 | 25 | showlrc: false, |
23 | 26 | theme: '#b7daff' |
24 | 27 | }; |
|
366 | 369 | } |
367 | 370 |
|
368 | 371 | this.setMusic(0); |
| 372 | + |
| 373 | + APlayers.push(this); |
369 | 374 | }; |
370 | 375 |
|
371 | 376 | /** |
|
498 | 503 | * Play music |
499 | 504 | */ |
500 | 505 | 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); |
515 | 525 | } |
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 | + } |
518 | 534 | }; |
519 | 535 |
|
520 | 536 | /** |
521 | 537 | * Pause music |
522 | 538 | */ |
523 | 539 | 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 | + } |
532 | 550 | }; |
533 | 551 | } |
534 | 552 |
|
|
0 commit comments