Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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)