I'm looking for a way to tweak this code so that it works with multiple players on the same page and adds a fade effect when paused / played:
var playing = false;
playpause.addEventListener('click', function () {
if (!playing) {
document.getElementById('player').play();
this.src = 'pause.png';
} else {
document.getElementById('player').pause();
this.src = 'play.png';
}
playing = !playing;
});
This unbearable fragment is what I use now to make it work with different tracks (using different identifiers) ....
var playing = false;
playpause01.addEventListener('click', function () {
if (!playing) {
document.getElementById('player01').play();
this.src = 'pause.png';
} else {
document.getElementById('player01').pause();
this.src = 'play.png';
}
playing = !playing;
});
playpause04.addEventListener('click', function () {
if (!playing) {
document.getElementById('player04').play();
this.src = 'pause.png';
} else {
document.getElementById('player04').pause();
this.src = 'play.png';
}
playing = !playing;
});
playpause13.addEventListener('click', function () {
if (!playing) {
document.getElementById('player13').play();
this.src = 'pause.png';
} else {
document.getElementById('player13').pause();
this.src = 'play.png';
}
playing = !playing;
});
Here's the "working" JSFiddle with HTML and JS:
http://jsfiddle.net/multiformeingegno/jdk0b5L4/
So, how can I rewrite this to mix tracks in the same function, stop playing one track if another is selected, and add a fade effect?
source
share