We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I'm exactly copying an example from the IBM site on how to have a button play sound on my iphone under javascript control. But I cant figure out what I'm doing wrong.
The following works fine on a desktop for both "audio" and "audio2" sounds. But on an iphone it only works for sound "audio" but not for sound "audio2". The only difference between these two sounds that I can see is that "audio" is a song that lasts a minute and audio2 is a sound that lasts 0.25 seconds.
<body>
// a short audio file (0.24 seconds long).
<audio id="audio2">
<source src="audio2/zbing.m4a" type="audio/mpeg" />
</audio>
// this is the original example from the IBM jsfiddle site
<audio id="audio">
<source src='http://dl.dropbox.com/u/1538714/article_resources/song.m4a' type="audio/mpeg" />
</audio>
<button id="button">Play1</button>
<script type="text/javascript">
var button = document.getElementById('button');
var audio1 = document.getElementById("audio2"); // works for "audio" but not "audio2" on iphones.
var onClick = function() {
audio1.play(); // audio will load and then play
};
button.addEventListener('click', onClick, false); // ties button press to audio play.
</script>
</body>
Ultimately I want my game to have lots of short sounds so I'm not wedded to the above method if you have a better one. the reason for this convoluted process is to figure out how to work around the iphone's need to have user input before it will load/play sounds. To work that out, I was following the example for this trick to have the user intiate a sound load by a button press.
PS: the anchor tag that appears in the src= listing above is not really there. this web forum inserts that automagically!
Answers
??
I suggest using the Audio API if you dare to take the long way. Here is a good tutorial that helpmed me tremendously http://www.html5rocks.com/en/tutorials/webaudio/intro/
As far as I know, the audio element in html5 is still a bit buggy. Looping sounds seamlessly is a problem, something you may eventualy need for a game. As far as I know there still is no bulletproof solution you can use without having a fallback to flash.
But your bug with short sounds not getting played is very intresting^^ If you don't mind telling what you found out about it in the meantime, I would appreciate it :)