Como receber NSNotifications da reprodução de vídeo do YouTube incorporada no UIWebView
Não recebi notificações porMPMoviePlayerController
. O que estou fazendo de errado
Uso a seguir lógica.
Estou começando a reproduzir vídeo do youtube emUIWebView
. UIWebView
chama um padrãoMPMoviePlayerController
. Eu não controloMPMoviePlayerController
porque não instanciamosMPMoviePlayerController
.
Eu corro o clipe do youtube com reprodução automática (atraso de 1 segundo):
[self performSelector:@selector(touchInView:) withObject:b afterDelay:1];
Meu código é:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[self embedYouTube];
}
- (void)loadStateDidChange:(NSNotification*)notification
{
NSLog(@"________loadStateDidChange");
}
- (void)playbackDidFinish:(NSNotification*)notification
{
NSLog(@"________DidExitFullscreenNotification");
}
- (void)embedYouTube
{
CGRect frame = CGRectMake(25, 89, 161, 121);
NSString *urlString = [NSString stringWithString:@"http://www.youtube.com/watch?v=sh29Pm1Rrc0"];
NSString *embedHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
videoView.delegate = self;
for (id subview in videoView.subviews)
if ([[subview class] isSubclassOfClass: [UIScrollView class]])
((UIScrollView *)subview).bounces = NO;
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
}
- (void)webViewDidFinishLoad:(UIWebView *)_webView
{
UIButton *b = [self findButtonInView:_webView];
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(touchInView:) object:b];
[self performSelector:@selector(touchInView:) withObject:b afterDelay:1];
}
- (UIButton *)findButtonInView:(UIView *)view
{
UIButton *button = nil;
if ([view isMemberOfClass:[UIButton class]]) {
return (UIButton *)view;
}
if (view.subviews && [view.subviews count] > 0)
{
for (UIView *subview in view.subviews)
{
button = [self findButtonInView:subview];
if (button) return button;
}
}
return button;
}
- (void)touchInView:(UIButton*)b
{
[b sendActionsForControlEvents:UIControlEventTouchUpInside];
}
ATUALIZAR Estou criando um aplicativo que reproduz o vídeo do youtube. Você pode executar a lista de reprodução e verá o primeiro vídeo. Quando o primeiro vídeo termina, o segundo começa a ser reproduzido automaticamente e assim por diante.
Preciso dar suporte ao iOS 4.1 e acim
UPDATE2: @ H2CO3 Estou tentando usar seu esquema de URL, mas não funciona. O método delegado não chamou o evento de saída. Eu adicionei meu URL html ao log. Isto é
<html><head> <body style="margin:0">
<script>function endMovie()
{document.location.href="somefakeurlscheme://video-ended";}
</script> <embed id="yt" src="http://www.youtube.com/watch?v=sh29Pm1Rrc0"
onended="endMovie()" type="application/x-shockwave-flash"
width="161" height="121"></embed>
</body></html>
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([[[request URL] absoluteString] hasPrefix:@"somefakeurlscheme://video-ended"])
{
[self someMethodSupposedToDetectVideoEndedEvent];
return NO; // prevent really loading the URL
}
return YES; // else load the URL as desired
}
UPDATE3 @ Até agora, não consigo capturar UIMoviePlayerControllerDidExitFullscreenNotification, mas encontrei MPAVControllerItemPlaybackDidEndNotification. MPAVControllerItemPlaybackDidEndNotification aparece quando a reprodução do vídeo termina.
Mas eu não entendo como faço para receber notificações onDone?