views:

150

answers:

3

Hello,

I assume there might be a HTML5 or some JS that can be used to play sound?

Before you mark this as duplicate, this question is old, so I believe outdated:

http://stackoverflow.com/questions/1694631/play-sound-in-iphone-web-app-with-javascript

+1  A: 

Maybe you could use a JS event, send a event to your UIWebView delegate and then play a sound with in objective-c ?

Best solution I think ^^

For a solution in HTML5 I have no idea. You could take a look at http://stackoverflow.com/questions/1933969/sound-effects-in-javascript-html5

But I'm not sure this solution would work on all device. It depends if you need to play the sound "often" or not.

Vinzius
The link included may be what you are after.
William
A: 

If noticed some issues in Chrome but otherwise seems to work in other major browsers.

HTML:

<audio id="sound_example" title="Sample" autobuffer>
  <source src="sample1.wav" type="audio/x-wav">
  <source src="sample2.ogg" type="application/ogg">
  <source src="sample3.mp3" type="audio/mpeg">
</audio>

Javascript:

var playThis = document.getElementById("sound_example");
if (!playThis.paused) {
    playThis.pause();
    playThis.currentTime = 0.0;
}
tmpAudio.play();

Obviously you'll need to provide your own wav, ogg, or mp3 to try this yourself. The check for it being paused is there so if the condition is met, it will reset before playing it again.

If you'd like to skip the audio tag altogether you can go with this:

var sound_example = new Audio("sample3.mp3");
sound_example.play();

There are some minor pros and cons to both approaches but if you're needs are straight-forward then either should suffice.

michael