如何在UIWebView中暂停媒体播放 [英] How to Pause Media Playback in UIWebView
问题描述
我目前正在使用支持iPad上多个选项卡的浏览器.问题是iOS不允许同时播放多个音频/视频的选项卡,尝试这样做会导致出现问题,例如所有音频停止播放而无法返回.
I am currently working on a browser that supports multiple tabs on the iPad. Problem is iOS doesn't allow for more than one tab to be playing audio/video at the same time, trying this causes issues to arise such as all the audio to stop and not come back.
我注意到Google Chrome浏览器实际上停止了处于非活动状态的标签中的媒体.我想知道他们如何去做.我知道可以通过[webView loadRequest:NSURLRequestWithString(@"about:blank")];
语句完全停止媒体,但是这会使页面显示空白屏幕.我只想暂停媒体.
I have noticed that Google Chrome's browser actually stops the media in the tab that goes inactive. I am wondering how they go about it. I know it is possible to completely stop the media via a [webView loadRequest:NSURLRequestWithString(@"about:blank")];
statement but that causes the page to display a blank screen. I would like to merely pause the media.
我可以执行一些JavaScript来停止UIWebView中的媒体吗?如果是这样,它会是什么样子(我以前从未接触过javascript)?
Is there some javascript I can execute that will stop the media in the UIWebView? If so, what would it look like (I have never touched javascript before)?
提前谢谢!
推荐答案
由于您可以执行[webView stringByEvaluatingJavaScriptFromString:]
,因此这里有一些简单的方法.
Since you can do [webView stringByEvaluatingJavaScriptFromString:]
there is are she simple methods.
例如,您可以执行以下操作:
For example you could do this:
[webview stringByEvaluatingJavaScriptFromString: @"document.querySelector('video').pause();"]
当然,这只会停止播放第一个视频,因此在iPad上,脚本需要稍微复杂一些,并遍历页面上的所有视频.
Of course this would only stop the first video, so on iPad the script needs to be a little bit more sophisticated and loop through all the videos on the page.
var videos = document.querySelectorAll("video");
for (var i = videos.length - 1; i >= 0; i--){
videos[i].pause();
};
这篇关于如何在UIWebView中暂停媒体播放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!