javascript 在 iOS5 中自动播放 HTML5 音频/视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8044447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Autoplay HTML5 audio/video in iOS5
提问by Mark Kahn
I have an HTML5 web-app that has sound effects. I'm trying to get these effects working in iOS5 and can't for the life of me.
我有一个具有音效的 HTML5 网络应用程序。我试图让这些效果在 iOS5 中工作,但我终其一生都做不到。
Wondering if anyone has any work-arounds to get JS control of an HTML5 audio/video control in iOS5.
想知道是否有人有任何解决方法来获得 iOS5 中 HTML5 音频/视频控件的 JS 控制。
Or even a way to control multiple audio files with one click. As it stands right now, if I have 10 sound effects, I'd need 10 user clicks to get control of all of them, which is absurd!
甚至可以通过单击来控制多个音频文件。就目前而言,如果我有 10 个音效,我需要用户点击 10 次才能控制所有音效,这太荒谬了!
回答by andrewh
Absurb, but you have to see it from iPhone or any mobile phone's point of view. It is a mobile phone going over a cellular network with bandwidth limitations, which many people know about from the recent Sprint commercial. They do not want users going over their bandwidth limit because some site is sending their phone a large amount of data without them taking action themselves.
荒谬,但你必须从 iPhone 或任何手机的角度来看待它。它是一种移动电话,通过蜂窝网络进行带宽限制,许多人从最近的 Sprint 广告中了解到这一点。他们不希望用户超过他们的带宽限制,因为某些站点在他们自己没有采取行动的情况下向他们的手机发送了大量数据。
The following is an excerpt from the official Safari Developer Librarywith more details.
以下是官方Safari 开发者库的摘录,其中包含更多详细信息。
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()">
通过蜂窝网络下载的用户控制
在 iOS 上的 Safari(适用于所有设备,包括 iPad)中,用户可能使用蜂窝网络并按数据单位收费,预加载和自动播放被禁用。在用户启动之前不会加载任何数据。这意味着 JavaScript play() 和 load() 方法在用户启动播放之前也处于非活动状态,除非 play() 或 load() 方法由用户操作触发。换句话说,用户启动的播放按钮有效,但 onLoad="play()" 事件无效。
这是播放电影:
<input type="button" value="Play" onClick="document.myMovie.play()">
这在 iOS 上没有任何作用:
<body onLoad="document.myMovie.play()">
回答by Jim True
Due to Apple, they have restricted the auto-play features to prevent cell data charges. In xcode4 i added a workaround though. In your "webViewDidFinishLoad" Send a javascript call to auto play the video and it works. I tried this in the html file with regular javascript but it did not work. Doing it through webViewDidFinishLoad did the trick though. In this example i want to auto play the video on my index.html page. I have a javascript function on that page called startVideo().
由于Apple,他们限制了自动播放功能以防止手机数据费用。在 xcode4 中,我添加了一个解决方法。在您的“webViewDidFinishLoad”中发送一个 javascript 调用来自动播放视频并且它可以工作。我在带有常规 javascript 的 html 文件中尝试了这个,但它没有用。虽然通过 webViewDidFinishLoad 做到了这一点。在这个例子中,我想在我的 index.html 页面上自动播放视频。我在该页面上有一个名为 startVideo() 的 javascript 函数。
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSURLRequest* request = [webView request];
NSString *page = [request.URL lastPathComponent];
if ([page isEqualToString:@"index.html"]){
NSString *js = @"startVideo();";
[myWebMain stringByEvaluatingJavaScriptFromString:js];
}
}
And here's my javascript function:
这是我的 javascript 函数:
<script>
function startVideo(){
var pElement3 = document.getElementById('myVideo');
pElement3.play();
}
</script>
And here's the html in case you're new to html video
如果您不熟悉 html 视频,这里是 html
<video id="myVideo" poster="index_poster.png" width="1024" height="768" xcontrols autoplay="autoplay">
<source src="flow.m4v" type="video/mp4"/>
browser not supports the video
</video>
回答by ryuutatsuo
Have you tried something like this??
你试过这样的吗??
function clickedOnce(){
$('audio').each(function(){
this.play();
});
}