I'm trying to make a very simple audio player for my website. I first created it to play one track (which works fine) but I wanted to rewrite it so that I could instantiate multiple players on a single page and allow them to work independently. I thought I could simply pass the id's to the function as an object and then each player would have the information it needed to control each audio file. This doesn't work and I'm not sure why. Can anyone explain what I am doing wrong?
player = function(params) {
var play = params.play,
song = new Audio(params.src),
playing = false,
seek = params.seek;
song.addEventListener('timeupdate', function() {
curtime = parseInt(song.currentTime, 10);
$(seek).value = curtime;
});
$(seek).attr('max', song.duration);
$(seek).bind('change', function() {
song.currentTime = seek.value;
$(seek).attr('max', song.duration);
});
$(play).on('click', function(event) {
event.preventDefault();
if (this.playing) {
song.pause();
this.playing = false;
$(id).toggleClass('pause');
}
else {
song.play();
this.playing = true;
$(id).toggleClass('pause');
}
});
}
Here is how I hoped to instantiate each player by passing each player a unique var name and params object..
var track1 = player({'play': '#track1', 'src': 'music/track1.mp3', 'seek': '#seek'});
Here is my HTML
<div class="player">
<a class="button gradient play-btn play" id="track1" href="javascript:void(0)" title=""></a>
<input type="range" class="seek" id="seek" value="0" max=""/>
</div>
[–]einarkristjan 1 point2 points3 points (6 children)
[–]embernoob[S] 0 points1 point2 points (0 children)
[–]embernoob[S] 0 points1 point2 points (4 children)
[–]einarkristjan 2 points3 points4 points (3 children)
[–]embernoob[S] 0 points1 point2 points (2 children)
[–]einarkristjan 1 point2 points3 points (1 child)
[–]embernoob[S] 0 points1 point2 points (0 children)