Oleg
Oleg

Reputation: 5

How can I autoplay media in Safari on an iPhone?

I'm trying to get an video file to autoplay in Safari on an iPhone. But this javascript not working on Safari on an iPhone, but on Android it's fine.

                         window.onload = function () {
                              var pElement = document.getElementById("myVideo");

                              setTimeout(function () {
                                  pElement.load();

                                  setTimeout(function () {
                                      pElement.play();
                                  }, 500);
                              }, 500);
                          };

Upvotes: 0

Views: 424

Answers (1)

Scott
Scott

Reputation: 285

User Control of Downloads Over Cellular Networks

In Safari on iOS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, preload and autoplay are disabled. No data is loaded until the user initiates it. This means the JavaScript play() and load() methods are also inactive until the user initiates playback, unless the play() or load() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad="play()" event does not.

This plays the movie:

<input type="button" value="Play" onClick="document.myMovie.play()">

This does nothing on iOS: <body onLoad="document.myMovie.play()">

Upvotes: 2

Related Questions