issues playing soundclips with HTML5 Audio on iPhone
I have a few mp3 soundclips to play at certain events on my HTML5 app. There's no tag, the script creates Audio objects at the beginning (one for each mp3) and loads the files. When I have to play a sound, I simply call play() on one of these objects.
This works fine in a Chrome desktop, but is very inconsistent in my iPod touch, eventually some sounds stop playing and I even get error alerts.
Here's a small script I set up to see the problem, it's hosted at soundtest.staticloud.com including the 3 audi开发者_运维百科o files so you can check it out on an iPhone/whatever.
var snd = [];
window.onload = function() {
for(var i = 0; i < 3; i++) {
snd[i] = new Audio("snd" + i + ".mp3");
snd[i].load();
}
}
function sound(n) {
snd[n-1].play();
}
Am I doing something wrong?
I had a ton of audio problems in iDevices...what you have to do is blank out the last played clip before starting the next. Also, I just had a single audio object, and would change the source, so my solution looked more like this:
<html>
<body>
<audio></audio>
etc
</body>
<script type="text/javascript">
var sounds=[];
window.onload = function() {
for(var i = 0; i < 3; i++) {
sounds[i] = "snd" + i + ".mp3";
}
}
function sound(n){
snd=document.getElementsByTagName("audio")[0];
snd.pause(); //-----------------PAUSE THE LAST (WHICH MAY BE LONG DONE)
snd.src=''; //-----------------BLANK SRC
snd.src=sounds[n-1]; //---------REPLACE WITH NEW SOUND
snd.load();
snd.play();
}
</script>
</html>
精彩评论