HTML5 Audio tag on Safari has a delay

后端 未结 9 646
無奈伤痛
無奈伤痛 2020-11-27 18:50

I\'m trying to accomplish a simple doodle-like behaviour, where a mp3/ogg sound rings on click, using the html tag. It is supposed to work under Firefox, Safari and Safari

9条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 19:08

    Apple decided (to save money on celluar) to not pre-load and HTML elements.

    From the Safari Developer Library:

    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:

    This does nothing on iOS:


    I don't think you can bypass this restriction, but you might be able to.

    Remember: Google is your best friend.


    Update: After some experimenting, I found a way to play the with JavaScript:

    var vid = document.createElement("iframe");
    vid.setAttribute('src', "http://yoursite.com/yourvideooraudio.mp4"); // replace with actual source
    vid.setAttribute('width', '1px');
    vid.setAttribute('height', '1px');
    vid.setAttribute('scrolling', 'no');
    vid.style.border = "0px";
    document.body.appendChild(vid);
    

    Note: I only tried with .


    Update 2: jsFiddle here. Seems to work.

提交回复
热议问题