问题
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
回答1:
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.
回答2:
Let's look at this scenario :

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;
}
回答3:
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];
来源:https://stackoverflow.com/questions/10323756/youtube-dismissal-event-ios