6

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??

Manse
  • 37,765
  • 10
  • 83
  • 108
Krunal Doshi
  • 73
  • 1
  • 3

2 Answers2

19

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]);
}
rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • awesome advice - i often told people that a web view movie player could not be observed for notifications - gee, guess I was wrong then. – Till Nov 24 '11 at 21:03
  • It doesn't actually know which movie player it's observing. Hopefully you only have one to observe. :) – rob mayoff Nov 24 '11 at 22:47
  • how to change the video from playing to paused, just from the notification? – lakshmen Jul 01 '13 at 04:54
  • Are there any alternate solutions like above? – Susim Samanta Dec 04 '14 at 07:22
  • @Susim Did u find any alternative for this? – Jasmeet Singh Feb 06 '15 at 09:25
  • @JasmeetSingh no I remembered so far – Susim Samanta Feb 19 '15 at 15:27
  • @Susim In iOS 8 I found AVSystemController_NowPlayingAppIsPlayingDidChangeNotification which is another private notification. It has a AVSystemController_NowPlayingAppIsPlayingNotificationParameter which is 0 when stopped or 1 when playing. – Ashley Mar 15 '15 at 23:41
  • This approach is working for me in iOS 8: http://stackoverflow.com/a/6869408/462162. Instead of calling a function when the user clicks a button as shown in that answer, you can call a function from the JavaScript onplay and onpause events. – arlomedia Jul 20 '15 at 19:55
  • @arlomedia Hi, could you please give more details about your answer? – Demonedge Oct 25 '15 at 04:36
1

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 !!

Sourabh Bhardwaj
  • 2,524
  • 1
  • 17
  • 19