0

I have this function:

function audioPlayer(band, album, track) {
    var audio = new Audio; //Class\
    audio.src = 'music/' + band + '/' + album + '/' + track + '.mp3';
    audio.play();
}

It will play the song that is clicked. But when another song is clicked I'll hear both songs play.

I have tried multiple suggestions already but none of them seem to work for me. I have tried:

I don't use the html5 <audio> tag only JavaScript / jQuery.

Is there a way to make the first song stop and than just play the other?

If you need more information please let me know. Thanks in advance

Community
  • 1
  • 1
SuperDJ
  • 7,108
  • 9
  • 38
  • 68
  • You need to keep track of a shared `audio` variable. When you call `audioPlayer(,,)`, you'll stop that variable, set the src, and then play it. Something to that effect – Ian Aug 02 '14 at 15:40

1 Answers1

2

You have to make your audio variable global, this will work:

var audio = new Audio;

function audioPlayer(band, album, track) {
    audio.src = 'music/' + band + '/' + album + '/' + track + '.mp3';
    audio.play();
}
Marco Bonelli
  • 55,971
  • 20
  • 106
  • 115