Link to home
Start Free TrialLog in
Avatar of debbieau1
debbieau1Flag for United States of America

asked on

Play sound on DIV click

I want to play audio, when a <div> is clicked.

I will have multiple .mp3s.   I need to ensure if one is clicked, then others will not play, or repeat.

It needs to run on an ipad
ASKER CERTIFIED SOLUTION
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
as in th example provided above you can simply do like this

<script language="javascript" type="text/javascript">
 function playSound(soundfile) {
 document.getElementById("playsound").innerHTML= "<embed src=\""+soundfile+"\" hidden=\"true\" autostart=\"true\" loop=\"false\" />";
 }
 </script>

<div id="playsound" onclick="playSound('abc.mp3');" style="border:1px solid red;"></div>

you can always refer you path within the innerhtml tag

I gave a border to Div just to check if this really works as expected or not!

i have placed my abc.mpe in same folder

Avatar of debbieau1

ASKER

Unfortunately several of these solutions do not work on the ipad.  I had tried most previously.  The desktop isn't hard, particularly if using a flash solution, but the ipad has real latency issues and I am having problems when using multiple audio, that it will sometimes replay previous audio.

If you have any sample scripts using several audios that do work on the ipad without repeating, it would be great to get.
For Ipad, you HTML5 audio tag. Check this example
http://www.jezra.net/code_examples/html5_audio/

Thanks very much for your perseverance.  I have managed to get audio playing on the ipad.  I think I may have an answer for the latency, but the biggest issue is still with having several audio files, that it gives unpredictable results.

Sometimes I click on one audio, then another, but a previous one plays, instead of the one just clicked.

I was trying to find a sample that I can test on the ipad, that plays several audios.  I need to make sure if I click one that it only plays that one.
can u use some server side lanaguage also

with that you can set the play the one and when other is clicked, the one should be stopped
I'm using php, but this really needs to be handled client side.  Thanks anyway
did you checked the HTML5 audio tag on ipad?
Yep did that.  Have run audio successfully on the ipad, it's just when using multiple audio it has been a problem.
did you used the play and pause apis of audio tag?
If yes, please share that code snippet
I'm going to have play more with that player when I get a chance

Thanks.