【问题标题】:iphone - force MPMoviePlayerController to play video in landscape modeiphone - 强制 MPMoviePlayerController 以横向模式播放视频
【发布时间】:2011-01-19 21:04:29
【问题描述】:

我有一个只有纵向模式的应用,但是当用户播放视频时,我希望它以全屏横向模式播放(视频播放器在纵向模式下看起来不太好)。我是这样玩的:

[self.view addSubview:myMoviePlayer.view];
[self.myMoviePlayer setFullscreen:YES animated:YES];
[self.myMoviePlayer play];

最好的方法是什么?

【问题讨论】:

    标签: iphone objective-c video mpmovieplayercontroller


    【解决方案1】:

    阅读这篇文章:

    http://iosdevelopertips.com/video/getting-mpmovieplayercontroller-to-cooperate-with-ios4-3-2-ipad-and-earlier-versions-of-iphone-sdk.html

    主要思想是:

    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
    
    [[self view] setBounds:CGRectMake(0, 0, 480, 320)];
    [[self view] setCenter:CGPointMake(160, 240)];
    [[self view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
    

    【讨论】:

      【解决方案2】:

      我的应用程序非常简单,使用 UINavigationController 作为根视图控制器,它显示一个主导航屏幕和一些细节屏幕。由于布局限制,主屏幕不支持旋转,为了保持一致性,细节屏幕(简单地推送到导航控制器的堆栈上)也不支持。但是,某些详细信息屏幕包含指向视频的链接,并且视频需要以纵向模式显示。该应用程序使用 MPMoviePlayerController 模态显示播放器。

      我刚刚遇到了类似的问题,并尝试了上面的解决方案,该解决方案将仿射变换应用于 MPMoviePlayerController 的视图,但问题是状态栏继续显示为纵向模式,并与模态电影播放器​​视图重叠(在左边,如果根据上面的旋转观察)。我尝试了几件事无济于事:

      • 隐藏状态栏。这不起作用,因为玩家存在于自己的世界中,并且无论如何都会继续显示状态栏。我找不到解决办法。

      • 显式设置状态栏方向。我不确定为什么这不起作用,但我怀疑这是因为我在 info.plist 中指出仅支持肖像,因此无法进行更改。

      Net-net,以上对我不起作用。同样,b/c Apple 告诫开发人员将 MPMoviePlayerController 视为不透明的,并且(尤其是使用 transform 方法)我违反了这一点。

      最后,我找到了一个更简单的解决方案:

      1. 在 info.plist 中,我指出支持所有方向(除了颠倒,因此是标准 iPhone 习语)。

      2. 子类 UINavigationController,并覆盖适当的 shouldAutorotate 方法,以便仅支持 Portrait(请参阅 this solution 等,了解如何在 iOS

        李>

      这很有效,因为:

      1. 虽然我指出应用程序支持自动旋转,但我在 UINavigationController 子类中将其关闭,该子类包含我正在渲染的所有视图...所以一切都保持纵向,除了:

      2. MPMoviePlayerController 以模态方式呈现,位于 NavigationController 之上,并且存在于自己的世界中。因此,可以随意关注 info.plist 中的内容,并自行旋转。

      有很多关于如何以模态方式呈现播放器的示例,但为了快速参考,这是我的代码:

      - (void)presentModalMediaPlayerForURL:(NSURL *)inURL
      {
          NSLog(@"Will play URL [%@]", [inURL description]);
          MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:inURL];
          [player.view setBounds:self.view.bounds];
      
          [player.moviePlayer prepareToPlay];
          [player.moviePlayer setFullscreen:YES animated:YES];
          [player.moviePlayer setShouldAutoplay:YES];
          [player.moviePlayer setMovieSourceType:MPMovieSourceTypeFile];
          [self presentModalViewController:player animated:YES];
      
          [player release];
      }
      

      【讨论】:

        【解决方案3】:

        对于全屏播放,请使用 MPMoviePlayerViewController,然后使用 MPMoviePlayerViewController 类上的“shouldAutorotateToInterfaceOrientation”方法使其以横向格式启动和播放。

        看起来像这样:

        [yourInstanceOfMPMoviePlayerViewController shouldAutorotateToInterfaceOrientation: UIInterfaceOrientationLandscapeRight];
        

        【讨论】:

        • 这在 IOS 6 中已被弃用,它实际上不会让播放器旋转。
        • 这对我不起作用 - 我有一个显示电影的视图控制器,它完全处于横向模式,但电影仍然以纵向模式启动......想法?
        【解决方案4】:

        我遇到了同样的情况,但我使用的是 iOS 6 和基于 NavController 的项目。有趣的是 ViewController 承载了我不想旋转的 MPMoviePlayerController,但我希望旋转其中的视频。


        我只是根据设备方向根据需要手动旋转 MPMoviePlayerController。 _videoView 是 ViewController 的 MPMoviePlayerController 属性。例如,我只是将所需的宽度和高度硬编码为 16x9 的纵横比,因为我打算将此视频上传到 youtube。

        - (void)updateVideoRotationForCurrentRotationWithAnimation:(bool)animation
        {
            CGSize containerSize  = _videoView.frame.size;   // Container NOT rotated!
            float  videoWidth     = 384;                     // Keep 16x9 aspect ratio
            float  videoHeight    = 216;
        
            if( animation )
            {
                [UIView beginAnimations:@"swing" context:nil];
                [UIView setAnimationDuration:0.25];
            }
        
            switch( self.interfaceOrientation )
            {
                case UIInterfaceOrientationPortrait:
                case UIInterfaceOrientationPortraitUpsideDown:
                    m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(90));
        
                    // This video player is rotated so flip width and height, but the container view
                    // isn't rotated!
                    [m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoHeight)/2,
                                                            (containerSize.height-videoWidth)/2,
                                                            videoHeight,
                                                            videoWidth)];
                    break;
        
                case UIInterfaceOrientationLandscapeLeft:
                case UIInterfaceOrientationLandscapeRight:
                    m_videoPlayer.view.transform = CGAffineTransformMakeRotation(radians(0));
        
                    // This video player isn't rotated
                    [m_videoPlayer.view setFrame:CGRectMake((containerSize.width-videoWidth)/2,
                                                            (containerSize.height-videoHeight)/2,
                                                            videoWidth,
                                                            videoHeight)];
                    break;
            }
        
            if( animation )
                [UIView commitAnimations];
        
        }
        
        
        - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
        {
            [self updateVideoRotationForCurrentRotationWithAnimation:YES];
        }
        

        在视图确实出现后,我也会调用 updateVideoRotationForCurrentRotationWithAnimation,因此它具有正确的初始方向。

        【讨论】:

          【解决方案5】:

          我使用以下代码来处理 Movieplayer 的 ONLY LANDSCAPE 模式。

          NSURL *movieURL = [NSURL URLWithString:@"http://techslides.com/demos/sample-videos/small.mp4"];  // sample url
          MPMoviePlayerViewController *movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
          
          // Logic for play movie in landscape
          CGAffineTransform landscapeTransform;
          landscapeTransform = CGAffineTransformMakeRotation(90*M_PI/180.0f);
          landscapeTransform = CGAffineTransformTranslate(landscapeTransform, 80, 80);
          [movieController.moviePlayer.view setTransform: landscapeTransform];
          
          [self presentMoviePlayerViewControllerAnimated:movieController];
          [movieController.moviePlayer prepareToPlay];
          [movieController.moviePlayer play];
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-03-16
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-21
            • 1970-01-01
            相关资源
            最近更新 更多