5

Good day,

Through the use of an UIWebview I have now a working method to show a youtube video within my app (using the tag, finding the play button within the webview and firing the touch event on that).

Works like a charm. The video pops up and plays. However I would like to recieve an event when the video ends or the user clicks the done button.

On the internet I have found that there is an event: MPAVControllerItemPlaybackDidEndNotification where you can listen to. However this one does not get called.

After some further research I found that for Youtube Videos embedded through UIWebView another notification was called ( UIMoviePlayerControllerDidExitFullscreenNotification ). Unfortunately that one does not work either anymore. ( found it here )

Does anyone have any idea how I can do some action after the video is done playing or has been dismissed?

Thanks

Community
  • 1
  • 1
Matthijn
  • 3,126
  • 9
  • 46
  • 69

3 Answers3

5

Use the UIMoviePlayerControllerWillExitFullscreenNotification for getting notified once the user tapped on the DONE button. The UIMoviePlayerControllerDidExitFullscreenNotification seems indeed to be omitted on iOS6.

Mind that ...Did... vs. ...Will... difference!

For more on that subject, once again check my updated answer within that posting you referenced in your question.

Till
  • 27,559
  • 13
  • 88
  • 122
0

Let's look at this scenario :

enter image description here

In your view, you have a button. When it's clicked, you want to play the video directly. In order, to do so, you open the webview as a modal view of your view :

[self presentModalViewController:videoWebView animated:NO];

For your webview, you should use Youtube API to integrate and autoplay the video. See the proposed working example here : https://stackoverflow.com/a/15538968

You'll see that the video is launched in a modal view of your webview view. One way to detect when the video is dismissed (when the "done" button has been clicked) is to use the viewDidAppear on your webview view class. In this method you will then dismiss the webview view too but...when this view is launched at first, you don't want to dismiss it. You can add a boolean property to avoid that.

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    if (_videoLaunched) {
        [self dismissModalViewControllerAnimated:YES];
    }
}

In the viewDidLoad method, set this property to NO and in the webViewDidFinishLoad method (delegate method of webview) set it to YES.

I think it answers one part of your question. Concerning the detection of the end of the video you have to modify you YT_Player.html file to listen to state changes.

ytPlayer = new YT.Player('media_area', {height: '100%', width: '100%', videoId: 'SbPFDcspRBA',
                                 events: {'onReady': onPlayerReady, 'onStateChange': onPlayerStateChange}

function onPlayerStateChange(e) {
        var result = JSON.parse(event.data);
        if (result.info == 0) { // Video end
            window.location = "videomessage://end";
        }
    }
                                 });

You will then catch the event in your webview view and dismiss it like this :

-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSURL *url = request.URL;
    if ([[url scheme] isEqualToString:@"videomessage"]) {
        [self dismissModalViewControllerAnimated:YES];
        return YES;
    }
    return YES;
}
Community
  • 1
  • 1
Yiseli
  • 23
  • 1
  • 5
-1

What you need here is something like this:

- (void)playerWillExitFullscreen:(NSNotification *)notification
{
//do something...
}

[[NSNotificationCenter defaultCenter] addObserver:self
                                     selector:@selector(playerWillExitFullscreen:) 
name:@"MPMoviePlayerWillExitFullscreenNotification" object:nil];
defactodeity
  • 714
  • 1
  • 8
  • 21
  • Did you try with iOS 6? Tried this before, notification is not firing here. – fabb Sep 25 '12 at 19:02
  • MPMoviePlayerWillExitFullscreenNotification is working for me on iOS 6. Can you check if it's working for you? – defactodeity Sep 25 '12 at 19:09
  • Unfortunately, it does not work for me. Maybe it has to do with the fact I use a Youtube video within a UIWebView? – fabb Sep 26 '12 at 09:11
  • @fabb that certainly is correct, this answer is not helping when using Youtube video's within a UIWebView - the author of this answer must have missed that part in the original question. – Till Oct 01 '12 at 20:15