【问题标题】:How to make MPMoviePlayerController fullscreen in landscape and not fullscreen in portrait如何使 MPMoviePlayerController 横向全屏而不是纵向全屏
【发布时间】:2018-01-15 20:12:50
【问题描述】:

我有一个MPMoviePlayerController,其初始化如下:

//Code in my UIViewController
@property (nonatomic, strong) UIView *myVideoView;
@property (nonatomic, strong) MPMoviePlayerController *myVideoPlayer;

- (void) initializeVideoPlayer
{
    CGRect frame = CGRectMake(0, 70, self.view.frame.size.width, 200);
    self.myVideoView = [[UIView alloc] initWithFrame:frame];
    [self.view addSubview:self.myVideoView];

    NSURL *videoURL = [NSURL fileURLWithPath:path];

    self.myVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
    self.myVideoPlayer.controlStyle = MPMovieControlStyleEmbedded;
    self.myVideoPlayer.shouldAutoplay = YES;
    [self.myVideoPlayer.view setFrame: self.myVideoView.bounds];
    [self.myVideoView addSubview: self.myVideoPlayer.view];

    //Play video
    [self.myVideoPlayer prepareToPlay];
    [self.myVideoPlayer play];

}

我的问题是,当用户将手机旋转到横向时,如何让视频全屏播放,而不是在手机处于纵向时全屏播放。

我尝试将以下内容添加到我的UIViewController

- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    if(UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
    {
        [self.myVideoPlayer setFullscreen:YES animated:YES];
    }
    else
    {
        [self.myVideoPlayer setFullscreen:NO animated:YES];
    }
}

但是,这样做的问题是,一旦播放器处于全屏状态,willAnimateRotationToInterfaceOrientation 就不再被调用;因此,即使用户旋转回纵向,视频仍处于全屏状态。

【问题讨论】:

    标签: objective-c ios mpmovieplayercontroller


    【解决方案1】:

    尝试使用MPMoviePlayerViewController 而不是MPMoviePlayerController。只需在您的UIViewController 中初始化它并使用它的moviePlayer 属性,就像使用普通的MPMoviePlayerController 一样。如果您将MPMoviePlayerViewController 子类化,您可以通过实现willAnimateRotationToInterfaceOrientation 等来控制设备旋转时发生的情况。

    【讨论】:

      【解决方案2】:

      在 AppDelegate.h 中:

      @property(nonatomic)BOOL allowRotation;
      

      在 AppDelegate.m 中:

      - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
      {
          self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
      
          RootViewController * root = [[RootViewController alloc] init];
          self.window.rootViewController = root;
      
      //add two Notification
      
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPVisionVideoNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
          [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPVisionVideoNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
      
          self.window.backgroundColor = [UIColor whiteColor];
          [self.window makeKeyAndVisible];
          return YES;
      }
      
      - (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification {
          self.allowRotation = YES;
      }
      
      - (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification {
          self.allowRotation = NO;
      }
      
      -(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
      {
          if (self.allowRotation) {
      
              return UIInterfaceOrientationMaskLandscapeRight ;
          }
          return UIInterfaceOrientationMaskPortrait;
      }
      
      //this can rotate the windows when to fullscreen state
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多