0

我的应用程序仅是纵向的,但我希望允许用户在通过 UIWebview 观看全屏视频时旋转到横向。我做了一些研究,发现我应该将我的班级添加为这些通知的观察者:

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。

4

2 回答 2

3

您没有收到UIMoviePlayerControllerWillExitFullscreenNotification回调的原因是因为您将自己作为观察者移除viewWillDisappear:

于 2013-10-02T15:38:16.070 回答
0

由于这些回调未记录在案,我使用 Javascript 事件(如 H2CO3在此处建议的那样)来确定视频何时开始、结束或暂停。

顺便说一句,我正在使用 YouTube 播放器。

首先,我设置 UIWebview 并将我的 ViewController 设置为委托。

接下来,我将 HTML 文件加载到 UIWebview 中。

索引.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>

视图控制器内部

    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;
}
于 2013-10-02T18:44:51.453 回答