Receive notification when a video starts or stops in UIWebView

Hi, I'm new to the lens - c

I have a problem with UIWebViewand MPMoviePlayerController: mine UIWebViewhas a movie inside html (this is a local html file), I use html5 and a video tag for the video.

I need a notification when a video starts or stops at UIWebView....

I tried to use MPMoviePlayerPlaybackDidFinishNotification, but it does not start ...

I also tried to make my main UIViewControllerview view of my own and intercept -didAddSubview:and -willRemoveSubview:. but without success ...

Does anyone know how to get a notification from UIWebView??

+5
source share
2 answers

You can observe @"MPAVControllerPlaybackStateChangedNotification"(use nilfor an object). This notification is not documented, so I don’t know if the App Store will approve your application.

[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(playbackStateDidChange:)
    name:@"MPAVControllerPlaybackStateChangedNotification"
    object:nil];

The notification has a key MPAVControllerNewStateParameterin its own userInfo. The value will be 0 before playback, 1 when paused, 2 during playback, and 3 (instantly) when dragging the playback slider.

- (void)playbackStateDidChange:(NSNotification *)note
{
    NSLog(@"note.name=%@ state=%d", note.name, [[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue]);
}
+19
source

I searched alot about this. Here is the solution I found to receive a call notification of completion of playback. Verified code on iOS6.0 and higher . All thanks @Morten .

In the viewDidLoad add an observer

[[NSNotificationCenter defaultCenter] addObserver:self
 selector:@selector(playbackDidEnd:)
name:@"MPAVControllerItemPlaybackDidEndNotification"//@"MPAVControllerPlaybackStateChangedNotification"
object:nil];

Then just add the following javascript webViewDidFinishLoaddelegate code as below

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //http://stackoverflow.com/a/12504918/860488
    [videoView stringByEvaluatingJavaScriptFromString:@"\
                                    var intervalId = setInterval(function() { \
                                        var vph5 = document.getElementById(\"video-player\");\
                                        if (vph5) {\
                                            vph5.playVideo();\
                                            clearInterval(intervalId);\
                                        } \
                                    }, 100);"];
}

- (void)playbackDidEnd:(NSNotification *)note
{
//do your stuff here
    [videoView removeFromSuperview];
    videoView.delegate = nil;
    videoView = nil;
}

, . !

+1

All Articles