Autoplay audio with iOS 5 Mobile Safari, any workarounds left?

给你一囗甜甜゛ 提交于 2020-01-14 04:16:11

问题


We all know Apple doesn't allow autoplay on html5 audio tags with Mobile Safari. The workaround for iOS 4 was to use an iframe with an mp3 src. Apple seems to have patched this on iOS 5.

<html>
<body>
iframe mp3 autoplay test
<iframe src='iframe.mp3' width='0px' height='0px' scrolling='no'></iframe>
</body>
</html>

This works on iOS 4.3.1, but not on 5.0.1.

Any workarounds left, or has Apple finally patched all the autoplay loopholes?


回答1:


The answer might be inside of this post here, of nothing else seems to work for you. Play sound file in iOS and Android




回答2:


Actually Apple have disabled the auto play feature for mobile safari. "In Safari on iPhone OS (for all devices, including iPad), where the user may be on a cellular network and be charged per data unit, autobuffering 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() method is triggered by user action. In other words, a user-initiated Play button works, but an onLoad play event does not."

It can be done by first loading the video and then faking the user click event as described in the following link Have a look at this link for a work around,

http://roblaplaca.com/blog/2010/04/14/ipad-and-iphone-html5-video-autoplay/

view source code of this page

http://www.roblaplaca.com/examples/html5AutoPlay/




回答3:


This uses jQuery and the point that most users will touch the screen after loading a page.

By using .one the sound "autostarts" only the first time one touches the screen..

<audio id="soundX" src="your source here" ></audio>

<script type="text/javascript">
$(document).ready(function() {
var soundX = document.getElementById('soundX');
$( "body" ).one( "touchstart", function() {
  console.log( "This will be displayed only once." );
  soundX.play(); 
});
});
</script>


来源:https://stackoverflow.com/questions/8276202/autoplay-audio-with-ios-5-mobile-safari-any-workarounds-left

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!