Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Michele Smiraglia
Michele Smiraglia
6,509 Points

Overlapping audio...

Hi all, I have 4 audio tags (songs) in mine html code, everyone with an anchor element for javascript function to start audio. Well, when i click another song before stop the current song, they overlap, because the first song didn't stop... How can do this? Stop the first audio when i choose other? Please Help xD!

3 Answers

Colin Bell
Colin Bell
29,679 Points

If you post your code or a codepen of your code, it would be easier to help out.

But a quick general suggestion would be be something like this:

$('.play-button').on('click', function() {
  //stop all from playing
  //play $(this) one
})
Michele Smiraglia
Michele Smiraglia
6,509 Points
var tracks = [document.getElementById('theChill'),
              document.getElementById('coolDown'),
              document.getElementById('gameDrop'),
              document.getElementById('upStorms')];

function ply(tracks) {

  tracks.play();

}

function stp(tracks) {
  tracks.pause();
  tracks.currentTime = 0;
}
Michele Smiraglia
Michele Smiraglia
6,509 Points

Colin Bell this is the function to play the tracks and stop it. This is the site: (http://fieldofbeats.com)