DEV Community

Cover image for Help with audio/.js

Help with audio/.js

Hi, this is my first post. I'm a newbie, following several online free courses, tutorials, etc. Sometimes I stumble upon a code and try to figure out what it does.
Right now I'm stuck on a code. I want it to make it so when someone clicks somewhere else, the audio file will stop, go to 0, and then play the new audio. So far, this code makes me click on audios, but it will override or mash up the songs, so you end up listening to two songs at the same time or more, depending on your clicking.
Can anyone help me, please? I've been trying to do it for more than a week now, and still no luck.
Thank you in advance.
Paulina.
PS: Sorry my english. Also, since it's my first post, don't know exactly how to copy the code down here, so you have it up the title and also a link here. Sorry
That's the code

Top comments (4)

Collapse
 
bitdweller profile image
Pedro Pimenta

Hi there!

To begin with and for the next time, it's best if you put the actual code on a working (or failing, or semi-working, etc...) example, something like JSBin or Codepen.io, this way we can try it, change it and share our fork with you again so you can see the changes.

On to the actual question, I think the problem is you're only making audios play with audio.play(), you're never telling audios to stop playing. The only similiar thing is you're removing the class playing from the playing element, but that's only CSS and in this case has no actual effect on the audio element.

You'll need to do something like audio.pause() or audio.stop(), I'm not sure, but you'll definitely need some JS to stop the audio.

Hope it nudges you in the good direction :)

Collapse
 
paukaradagian profile image
π“Ÿπ“ͺ𝓾𝓡𝓲𝓷π“ͺβ€οΈπ“šπ“ͺ𝓻π“ͺ𝓭π“ͺ𝓰𝓲π“ͺ𝓷

Thank you for taking the time to answer me.
This is the link to codepen.
codepen.io/PauKa/pen/eYJwKxa

Collapse
 
adityac0620 profile image
Aditya Chandran

Can you send a codepen link or something to your app so that I could test it?

Collapse
 
paukaradagian profile image
π“Ÿπ“ͺ𝓾𝓡𝓲𝓷π“ͺβ€οΈπ“šπ“ͺ𝓻π“ͺ𝓭π“ͺ𝓰𝓲π“ͺ𝓷

Thank you for answering me!
this is the link.
codepen.io/PauKa/pen/eYJwKxa