2

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").play();',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'.

JiminP
  • 2,136
  • 19
  • 26

4 Answers4

3

Autoplay of video/audio files on iPhones and iPads is not allowed. This is a decision taken by Apple.

Ian Devlin
  • 18,534
  • 6
  • 55
  • 73
1

Try this

 <audio src='music.m4a' autoplay="autoplay" id='asdf'></audio>
tildy
  • 979
  • 10
  • 20
  • 1
    No... it doesn't work on iPhone (even though it works on computer) – JiminP Jul 29 '11 at 12:05
  • 1
    You are right. i have found a same question here. http://stackoverflow.com/questions/3009888/autoplay-audio-files-on-an-ipad-with-html5 . .load() method before the .play() method on the audio or video tag should work. – tildy Jul 29 '11 at 12:14
  • Or check this: http://roblaplaca.com/blog/2010/04/14/ipad-and-iphone-html5-video-autoplay/ – tildy Jul 29 '11 at 12:19
  • 1
    Conclusion : In iPhone OS 4.2.1 or higher, there's no way to start audio or video automatically... :( – JiminP Jul 29 '11 at 12:43
0

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

Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
0

You would need to wait until the audio has loaded enough to begin playing it.

Documentation.

alex
  • 479,566
  • 201
  • 878
  • 984
  • That's why I tried `setTimeout('document.getElementById("asdf").play();',10000);`. It doesn't work even after I added `preload='auto'`. Pressing 'dd' starts the music immediately. – JiminP Jul 29 '11 at 12:08