title: How to receive NSNotifications from UIWebView embedded YouTube video playback
body: <p>I didn't received any notifications for <code>MPMoviePlayerController</code>. What am I doing wrong?</p>
  
  <p>I use following logic.</p>
  
  <p>I'm begining to play youtube video in <code>UIWebView</code>. <code>UIWebView</code> calls a standard <code>MPMoviePlayerController</code>. I don't control <code>MPMoviePlayerController</code> because I didn't instantiate <code>MPMoviePlayerController</code>.  </p>
  
  <p>I run youtube's clip with autoplay (1 second delay):</p>
  
  <pre><code>[self performSelector:@selector(touchInView:) withObject:b afterDelay:1];
  </code></pre>
  
  <p>My code is:</p>
  
  <pre><code>- (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 = @"&lt;html&gt;&lt;head&gt;\
      &lt;body style=\"margin:0\"&gt;\
      &lt;embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
      width=\"%0.0f\" height=\"%0.0f\"&gt;&lt;/embed&gt;\
      &lt;/body&gt;&lt;/html&gt;";
      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 &amp;&amp; [view.subviews count] &gt; 0) 
      {
          for (UIView *subview in view.subviews) 
          {
              button = [self findButtonInView:subview];
              if (button) return button;
          }
      }
      return button;
  }
  
  - (void)touchInView:(UIButton*)b
  {
      [b sendActionsForControlEvents:UIControlEventTouchUpInside];
  }
  </code></pre>
  
  <p><strong>UPDATE:</strong> I'm creating application that plays youtube's video. You can run playlist and you will see first video. When first video has ended, second video begins play automatically and so on. </p>
  
  <p>I need to support ios 4.1 and above.</p>
  
  <p><strong>UPDATE2:</strong> @H2CO3 I'm trying to use your url-scheme, but it don't works. Delegate method didn't called on exit event.  I added my html url to log.
  It is:</p>
  
  <pre><code>&lt;html&gt;&lt;head&gt;    &lt;body style="margin:0"&gt;    
  &lt;script&gt;function endMovie() 
  {document.location.href="somefakeurlscheme://video-ended";} 
   &lt;/script&gt;      &lt;embed id="yt" src="http://www.youtube.com/watch?v=sh29Pm1Rrc0"        
   onended="endMovie()" type="application/x-shockwave-flash"  
   width="161" height="121"&gt;&lt;/embed&gt;  
   &lt;/body&gt;&lt;/html&gt;
  
  - (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
  }
  </code></pre>
  
  <p><strong>UPDATE3</strong>
  @Till, I cann't caught UIMoviePlayerControllerDidExitFullscreenNotification, but I found MPAVControllerItemPlaybackDidEndNotification. MPAVControllerItemPlaybackDidEndNotification appears when playback video is ended. </p>
  
  <p>But I don't understand how do I catch onDone notifications? </p>