Skip to content

Commit 92d5930

Browse files
committed
Change state of play button from event handler
When the browser blocks autoplay, the 'Play' button will show instead of the 'Pause' button.
1 parent 22ce21c commit 92d5930

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

js/AudioPlayer.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,15 @@ var AudioPlayer = (function() {
171171
audio.addEventListener('error', error, false);
172172
audio.addEventListener('timeupdate', update, false);
173173
audio.addEventListener('ended', doEnd, false);
174+
audio.addEventListener('play', () => {
175+
playBtn.classList.add('playing');
176+
}, false)
177+
audio.addEventListener('pause', () => {
178+
playBtn.classList.remove('playing');
179+
}, false)
174180

175181
if(settings.autoPlay) {
176182
audio.play();
177-
playBtn.classList.add('playing');
178183
plLi[index].classList.add('pl-current');
179184
}
180185
}
@@ -314,7 +319,6 @@ var AudioPlayer = (function() {
314319
body: 'Now playing',
315320
tag: 'music-player'
316321
});
317-
playBtn.classList.add('playing');
318322
plActive();
319323
}
320324

@@ -340,7 +344,6 @@ var AudioPlayer = (function() {
340344
durTime.innerHTML = '--';
341345
progressBar.style.width = 0;
342346
preloadBar.style.width = 0;
343-
playBtn.classList.remove('playing');
344347
pl.innerHTML = '<div class="pl-empty">PlayList is empty</div>';
345348
}
346349

@@ -428,7 +431,6 @@ var AudioPlayer = (function() {
428431
if(!repeating) {
429432
audio.pause();
430433
plActive();
431-
playBtn.classList.remove('playing');
432434
return;
433435
}
434436
else {

0 commit comments

Comments
 (0)