How to prevent UIWebView video from getting remote

2020-08-22 13:54发布

问题:

I am using UIWebView in an iOS app to play YouTube videos but to provide native experience, I've implemented playback controls using UIKit. So the UIWebView is only used to display video.

I've also implemented -remoteControlReceivedWithEvent: to allow control from Control Center and controller buttons on earphones. But it seems that UIWebView automatically handles remote control events from the earphones. This is a problem because when you toggle play/pause, my code would pause the video and then UIWebView will toggle it again to play the video.

Is there anyway to prevent this from happening?

Related issue is that UIWebView tries to set "Now Playing" information to MPNowPlayingInfoCenter which is also done by my code.

回答1:

I encountered same kind of issue with my app which playbacks audio using AVAudioPlayer. This app displays current audio info in MPNowPlayingInfoCenter during playback. My requirement was to display an html5 video advert (with audio) inside a webview on top my player.

First i used only UIWebView as i needed to support iOS7 but i met a lot of issues, one of them was MPNowPlayingInfoCenter that displays url of ad video in place of current native audio playback. I tried several solutions such as method swizzling on UIWebView without any success.

I found only one solution that works for me: use WKWebView by default instead of UIWebView web container. HTML5 video playback will not interact anymore with MPNowPlayingInfoCenter, i had to support also iOS7 so i created a wrapper class to switch between UIWebView (with still the issue) on iOS7 and WKWebView from iOS8 and more.

Hope this helps.



回答2:

I ran into this question while trying to solve somewhat the reverse problem: I added a UIWebView to an app and remote control events were not working when the UIWeb view was on display, but worked elsewhere in the app. In my case the solution was to add these 3 methods, which are present on all other view controllers in my app, to the controller of my new UIWebView:

- (void) viewDidAppear:(BOOL)animated  {
    [self becomeFirstResponder];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self resignFirstResponder];
}

-(void)remoteControlReceivedWithEvent:(UIEvent *)event {
    // Logic to handle remote control events
}

From this, I suspect that you can prevent your UIWebView from handling remote control events by doing one of two things: 1. Include logic in the remoteControlReceivedWithEvent to ignore the remote control events you don't want handled. 2. Have your UIWebView resign being the first responder by calling resignFirstResponder in the viewDidAppear method of the controller.



标签: ios uiwebview