2013-10-02 104 views
0

我的應用只有肖像,但是,我想讓用戶在通過UIWebview觀看全屏視頻時旋轉到風景。我做了一些研究,發現我要補充我的類作爲這些通知觀察員:UIMoviePlayerControllerWillExitFullscreenNotification沒有收到

UIMoviePlayerControllerDidEnterFullscreenNotification UIMoviePlayerControllerWillExitFullscreenNotification

我添加和刪除類作爲像這樣的觀察:

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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidEnterFullScreen:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullScreen:) name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil]; 
} 

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

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; 

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerWillExitFullscreenNotification" object:nil]; 
} 

- (void)moviePlayerDidEnterFullScreen:(NSNotification *)notification 
{ 
    self.videoPlayingFullScreen = YES; 
} 

- (void)moviePlayerWillExitFullScreen:(NSNotification *)notification 
{ 
    self.videoPlayingFullScreen = NO; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    if (self.videoPlayingFullScreen) 
    { 
     return UIInterfaceOrientationMaskAllButUpsideDown; 
    } 

    return UIInterfaceOrientationMaskPortrait; 
} 

我的問題是:我從來沒有收到「UIMoviePlayerControllerWillExitFullscreenNotification」。我無法使用UIMoviePlayerControllerDidExitFullscreenNotification,因爲如果用戶完成以橫向方向觀看全屏視頻並按下「完成」,則前一個視圖控制器在縱向時也會以橫向顯示。

是否有另一種方式來檢測用戶「做」進入全屏和「將」退出全屏?還是有什麼我失蹤?

編輯: 我的應用程序僅適用於iOS 7。

+1

這些通知沒有記錄,因此在使用時會有超級風險,因爲它們可能會在發佈新操作系統更新時破壞應用程序。例如,UIMoviePlayerControllerDidExitFullscreenNotification永遠不會在iOS6上發送。 – Till

+0

是否有文檔記錄的方式來檢測UIWebview何時進入並將全屏退出? – Jonathan

+1

按照[此答案](http://stackoverflow.com/a/8554077/91282)所述使用JavaScript事件。 – Till

回答

3

你沒有得到的UIMoviePlayerControllerWillExitFullscreenNotification回調的原因是因爲你刪除自己作爲viewWillDisappear:

+0

感謝您的回答。作爲觀察者,我應該使用哪種方法? – Jonathan

+0

我想'dealloc'。 –

+2

使用dealloc刪除通知時,請確保在init方法中附加它們,而不是在外觀回調中。 – Till

0

由於這些回調都是無證我使用JavaScript事件(如H2CO3建議here)的觀察員,以確定當視頻開始,結束或暫停。

順便說一下,我使用的是YouTube播放器。

首先,我設置UIWebview並將我的ViewController設置爲委託。

接下來,我將HTML文件加載到UIWebview中。

的Index.html

<html> 
    <body bgcolor=#8C1717 style="margin:0px;"> 
     <div id="ytplayer"></div> 
     <script type="text/javascript"> 
      var tag = document.createElement('script'); 
      tag.src = "https://www.youtube.com/player_api"; 

      var firstScriptTag = document.getElementsByTagName('script')[0]; 
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 

      var player; 
      function onYouTubeIframeAPIReady() 
      { 
       player = new YT.Player('ytplayer', 
             { 
             height: 'videoHeight', 
             width: 'videoWidth', 
             videoId: 'videoID', 
             playerVars: { 'showinfo':0, 'rel':0 }, 
             events: { 'onStateChange': onPlayerStateChange } 
             }); 
      } 

      function playerDidBeginPlaying() 
      { 
       document.location.href = "fake://video-began"; 
      } 

      function playerDidEndPlaying() 
      { 
       document.location.href = "fake://video-ended"; 
      } 

      var done = false; 
      function onPlayerStateChange(event) 
      { 
       if (event.data == YT.PlayerState.PLAYING && !done) 
       { 
        done = true; 
        playerDidBeginPlaying(); 
       } 
       else if (event.data == YT.PlayerState.ENDED) 
       { 
        playerDidEndPlaying(); 
       } 
       else if (event.data == YT.PlayerState.PAUSED) 
       { 
        playerDidEndPlaying(); 
       } 
      } 
     </script> 
    </body> 
</html> 

裏面的ViewController

NSError *error = NULL; 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]; 
    NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error]; 

    if (error) 
    { 
#ifdef DEBUG 
     NSLog(@"[YouTube Webview] Error: %@", [error description]); 
#endif 
    } 

    [self.webView loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]]; 

然後,我實現了方法webView:shouldStartLoadWithRequest:navigationType:當事件發生時收到通知。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType 
{ 
    if ([[[request URL] absoluteString] hasPrefix:@"fake://video-began"]) 
    { 
     self.videoPlayingFullScreen = YES; 

     return NO; 
    } 
    else if ([[[request URL] absoluteString] hasPrefix:@"fake://video-ended"]) 
    { 
     self.videoPlayingFullScreen = NO; 

     return NO; 
    } 

    return YES; 
}