How to Pause Media Playback in UIWebView

2019-07-04 20:56发布

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.

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.

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)?

Thanks in advance!

1条回答
仙女界的扛把子
2楼-- · 2019-07-04 21:35

Since you can do [webView stringByEvaluatingJavaScriptFromString:] there is are she simple methods.

For example you could do this:

[webview stringByEvaluatingJavaScriptFromString: @"document.querySelector('video').pause();"]

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();
};
查看更多
登录 后发表回答