How to prevent UIWebView video from receiving remote control events

I use UIWebView in an iOS app to play videos on YouTube, but to provide my own experience, I used playback controls using UIKit. Thus, UIWebView is used only for displaying video.

I also implemented -remoteControlReceivedWithEvent: to enable control using the Control Center buttons and the controller on the headphones. But it looks like UIWebView is automatically handling remote control events from the headphones. This is a problem because when you switch the play / pause mode, my code pauses the video and then UIWebView switches it again to play the video.

Is there any way to prevent this?

A related issue is that UIWebView trying to set the "Now Playing" information to MPNowPlayingInfoCenter , which is also executed by my code.

+5
source share
2 answers

I ran into the same issue with my application that plays audio using AVAudioPlayer . This application displays the current audio information in MPNowPlayingInfoCenter during playback. My requirement was to show html5 video ads (with sound) inside webview on top of my player.

At first I used only UIWebView , because I needed to support iOS7, but I met a lot of problems, one of them was MPNowPlayingInfoCenter , which displays the URL of the advertising video instead of the current native audio playback. I tried several solutions like swizzling method on UIWebView without any success.

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

Hope this helps.

+3
source

I came across this question, trying to solve a slightly opposite problem: I added a UIWebView to the application, and the remote control events did not work when the UIWeb view was displayed, but it worked elsewhere in the application. In my case, the solution was to add these 3 methods that are present on all other view controllers in my application to the controller of my new UIWebView interface:

 - (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 the UIWebView from managing remote control events by doing one of two things: 1. Turn on the logic in remoteControlReceivedWithEvent to ignore remote control events that you do not want to handle. 2. Ask your UIWebView to resign as the first responder by calling resignFirstResponder in the controller's viewDidAppear method.

0
source

Source: https://habr.com/ru/post/1207701/


All Articles