Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Notification when a video starts or stops in UIWebView

Hello i am new to objective - c

I'm having a problem with the UIWebView and MPMoviePlayerController: My UIWebView has a movie inside the html (it's a local html file), I'm using html5 and a video tag for the video.

I want a notification when video starts or stops in UIWebView....

I have tried using MPMoviePlayerPlaybackDidFinishNotification, but it doesnt fire ...

I have also tried to make the my main UIViewController's view a view of my own, and intercept -didAddSubview: and -willRemoveSubview:. but with no sucess...

Does any body know how to get notification from uiwebview??

like image 803
Krunal Doshi Avatar asked Nov 24 '11 06:11

Krunal Doshi


2 Answers

You can observe @"MPAVControllerPlaybackStateChangedNotification" (use nil for the object). This notification isn't documented so I don't know if the App Store will approve your app.

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

The notification has the key MPAVControllerNewStateParameter in its userInfo. The value seems to be 0 before playback starts, 1 when it is paused, 2 when it is playing, and 3 (momentarily) when you are dragging the playback slider.

- (void)playbackStateDidChange:(NSNotification *)note
{
    NSLog(@"note.name=%@ state=%d", note.name, [[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue]);
}
like image 158
rob mayoff Avatar answered Nov 14 '22 22:11

rob mayoff


I searched alot about this..Here is the solution that I have found for getting the playback end notification call. Tested code on iOS6.0 and above. All thanks to @Morten.

In viewDidLoad add observer

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

Then simply add following javascript code webViewDidFinishLoad delegate 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;
}

You will get playbackDid End call in the above selected and can do whatever is your requirement. Happy Coding !!

like image 41
Sourabh Bhardwaj Avatar answered Nov 14 '22 22:11

Sourabh Bhardwaj