How to Pause Media Playback in UIWebView

随声附和 提交于 2019-12-23 18:19:48

问题


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:


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();
};


来源:https://stackoverflow.com/questions/11640135/how-to-pause-media-playback-in-uiwebview

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