So erhalten Sie NSNotifications von der eingebetteten YouTube-Videowiedergabe von UIWebView

Habe ich nicht erhalten keine Benachrichtigungen für MPMoviePlayerController. Was mache ich falsch?

Ich verwende folgende Logik.

Bin ich Anfang zu spielen Sie youtube-video in UIWebView. UIWebView ruft eine standard -MPMoviePlayerController. Ich keine Kontrolle MPMoviePlayerController weil ich nicht instanziieren MPMoviePlayerController.

Mir laufen die youtube-Clips mit autoplay (1 Sekunde Verzögerung):

[self performSelector:@selector(touchInView:) withObject:b afterDelay:1];

Mein code ist:

- (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];
}

UPDATE: ich bin momentan dabei, application, Spiele, youtube-video. Sie können führen Sie Wiedergabelisten und sehen Sie erste video. Beim ersten video beendet, das zweite video beginnt automatisch Abspielen und so weiter.

Muss ich eine ios-Version 4.1 und höher.

UPDATE2: @H2CO3 ich versuche, Ihr url-Schema, aber es nicht funktioniert. Delegate-Methode nicht aufgerufen, auf Ereignis exit. Ich habe meine html-url anmelden.
Es ist:

<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
@Till, ich cann ' T gefangen UIMoviePlayerControllerDidExitFullscreennotification, aber ich fand MPAVControllerItemPlaybackDidEndnotification. MPAVControllerItemPlaybackDidEndnotification wird angezeigt, wenn video-Wiedergabe beendet ist.

Aber ich verstehe nicht, wie fange ich onDone Benachrichtigungen?

InformationsquelleAutor der Frage Voloda2 | 2011-12-15

Schreibe einen Kommentar