I found that:
<!DOCTYPE html>
<html>
<head>
<script src='jQuery.js'></script>
<script>
$(document).ready(function(){
document.getElementById("asdf").play();
});
</script>
</head>
<body>
<audio src='music.m4a' id='asdf'></audio>
</body>
</html>
or
<!DOCTYPE html>
<html>
<head>
<script>
setTimeout('document.getElementById("asdf").开发者_如何学Cplay();',10000);
</script>
</head>
<body>
<audio src='music.m4a' id='asdf'></audio>
</body>
</html>
will not play the music on iPhone (of course above two works in normal computer), but writing
<!DOCTYPE html>
<html>
<body>
<audio src='music.m4a' id='asdf'></audio>
<a href='javascript:document.getElementById("asdf").play()'>dd</a>
</body>
</html>
and click on 'dd' will play the music.
My question is : how to automatically play the music right after the page is loaded (and 'ready' to play the music) in iPhone?
PS : I added setInterval('if($("#asdf").attr("readyState")) console.log(1);');
for check whether the sound is loaded, and I found that readyState
is changed right after I pressed 'dd'.
Autoplay of video/audio files on iPhones and iPads is not allowed. This is a decision taken by Apple.
Try this
<audio src='music.m4a' autoplay="autoplay" id='asdf'></audio>
HTML audio tag has a autoplay attribute which specifies whether or not to start playing the audio as soon as the object has loaded.
Check here
You would need to wait until the audio has loaded enough to begin playing it.
Documentation.
精彩评论