Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play mp3 file with javascript onClick

I am playing mp3 file just javascript onClick.

Bellow is my code:

//Music File 1
<audio id="id1" src="01.mp3"></audio>
<button onClick="document.getElementById("id1").play()">Play</button>
<button onClick="document.getElementById("id1").pause()">Stop</button>

//Music File 2
<audio id="id2" src="02.mp3"></audio>
<button onClick="document.getElementById("id2").play()">Play</button>
<button onClick="document.getElementById("id2").pause()">Stop</button>

My question is, I am playing music 01.mp3 and I want it stop play if I press play button at music 02.mp3.

Can any one give me a better solution than this? Totally, I want to way to play music like www.mp3skull.com, other stop play if pressed other file to play.

Regards, Virak

like image 256
SopheakVirak Avatar asked Jan 16 '23 13:01

SopheakVirak


1 Answers

Aside from the tag errors (presuming it's just a typo in this writeup), if you want a nicer Javascript audio player, check out http://jplayer.org/ - it's an open source, free player API.

One of the methods available for the player, .pauseOthers() halts all players on the page other than the one you ran the method on - useful in your situation.

like image 76
Death Avatar answered Jan 26 '23 00:01

Death