MPMoviePlayerController 视图未调整大小

发布于 2024-11-16 20:22:53 字数 504 浏览 10 评论 0原文

我正在使用 MPMoviePlayerController,它将其 UIView 渲染到横向上的正确尺寸,但当旋转到纵向时,它根本不调整大小。我认为它的父 UIView 是不调整大小的。我该如何设置?

我按照 @Alex Reynolds 在 UIView autoresizingmask not work for me 中提到的说明进行操作。这表明视图在旋转时确实会调整大小。我仍然有一个问题,当加载容纳播放器的 UIViewController 的 UIView 时,如果设备的方向是横向的,它们会渲染到正确的框架,但如果设备在加载时处于纵向,则不会调整其大小。根据 @Alex Reynolds 的回答,我所要做的就是旋转设备一次,之后它将开始正确调整大小。

第一次无法调整大小仍然很糟糕。以前有人发生过这种情况吗?如果是这样,我们将不胜感激任何意见。

I'm working with an MPMoviePlayerController which renders its UIView to the right dimensions on landscape, but when rotating to portrait it is simply not resizing. I think it's parent UIView is the one not resizing. How can I set this up?

I followed the instructions that @Alex Reynolds mentions in UIView autoresizingmask not working for me. This showed me that the view does resize when rotated. I still have the problem that when the UIView for the UIViewController that holds the player is loaded, if the orientation of the device is landscape, they it renders to the right frame, but if the devise is on portrait by the time it is loaded it is not resized to it. With @Alex Reynolds' answer, all I have to do is rotate the device once and it will start resizing properly after that.

It is still bad that it won't resize the first time. Has this happened to anyone before? If so, any input is greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-11-23 20:22:53

您是否正确设置了 MPMoviePlayerController 的 view 的 autoresizingMask ?其超级视图的 autoresizesSubviews 属性是否设置为 YES? (同样,这个超级视图在旋转时也会调整大小吗?我喜欢在测试期间为我的视图设置彩色背景颜色,以验证它们在自动旋转时是否正确调整大小。)

如果在检查这些属性后仍然无法正常工作,您可以随时设置影片手动更改玩家视图的 frame 属性。超级视图的 layoutSubviews 方法通常是执行此操作的最佳位置,但如果它不是您手动子类化的视图,您也可以在视图控制器中执行此操作。

Have you set the MPMoviePlayerController's view's autoresizingMask appropriately? Is its superview's autoresizesSubviews property set to YES? (and likewise, does this superview also resize when rotating? I like to set colourful background colours for my views during testing to verify that they resize correctly when autorotating.)

If it's still not working after checking those properties, you can always set the movie player's view's frame property manually. The super view's layoutSubviews method is generally the best place to do that, but if it's not a view you've manually subclassed, you can also do it in the view controller.

哭了丶谁疼 2024-11-23 20:22:53
- (void)viewDidLoad
{
[super viewDidLoad];
[self play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

}
-(IBAction)dismiss:(id)sender
{
//[self.view removeFromSuperview];

_moviePlayer =nil;

[self dismissViewControllerAnimated:YES completion:nil];
}
// Do any additional setup after loading the view from its nib.
-(void)play
 {

     NSURL *url = [NSURL URLWithString:@"stringurlvideo"];
    _moviePlayer =  [[MPMoviePlayerController alloc]initWithContentURL:url];
  // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)
      //      name:MPMoviePlayerPlaybackDidFinishNotification
  //                                             object:_moviePlayer];
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFull:)
                                         //        name:MPMoviePlayerDidEnterFullscreenNotification
                                         //      object:_moviePlayer];
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidExit:)
                                          //       name:MPMoviePlayerDidExitFullscreenNotification
                                          //     object:_moviePlayer];
    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    _moviePlayer.shouldAutoplay = YES;
if([[UIScreen mainScreen] bounds].size.height==568)
  {

    [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
  }
else
   {
     [_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];

   }
    [self.view addSubview:_moviePlayer.view];
    //[_moviePlayer.view setCenter:self.view.center];
    [_moviePlayer setFullscreen:YES animated:YES];

    }
 - (void) moviePlayBackDidFull:(NSNotification*)notification
    {


    }
 -(void)moviePlayBackDidExit:(NSNotification*)notification 
 {
////[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];




 }
 -(void)viewWillAppear:(BOOL)animated
   {
 /*   if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
   {
      [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
    CGRect rect=[bar frame];
    rect.size.width=self.view.frame.size.width;
    [bar setFrame:rect];

   }
  else
   {

    CGRect rect=[bar frame];
    rect.size.width=480;
    [bar setFrame:rect];
    [_moviePlayer.view setFrame:CGRectMake(0,44, 480, 320-44)];

  }*/

  }
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];

    if ([player
         respondsToSelector:@selector(setFullscreen:animated:)])
     {
        [player.view removeFromSuperview];
     }
     }

 - (void)didReceiveMemoryWarning
    {
   [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
   }


  -(NSUInteger)supportedInterfaceOrientations
  {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
   }


 - (BOOL)shouldAutorotate
 {
 return YES;
  }
  /*- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
return UIInterfaceOrientationLandscapeLeft;
 }*/
   - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation    duration:(NSTimeInterval)duration
    {
if(toInterfaceOrientation==UIInterfaceOrientationPortrait && toInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
    {

    if([[UIScreen mainScreen] bounds].size.height==568)
    {

    [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
    }
    else
    {
        [_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];

    }
    CGRect rect=[bar frame];
    rect.size.width=self.view.frame.size.width;
    [bar setFrame:rect];
    }
  else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight)
    {
    CGRect rect=[bar frame];
    if([[UIScreen mainScreen] bounds].size.height==568)
    {
    rect.size.width=568;
    }
    else{
        rect.size.width=480;

    }
    [bar setFrame:rect];
    [_moviePlayer.view setFrame:CGRectMake(0,44, rect.size.width, 320-44)];
      }
      }
   - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
      {
  /*  if(fromInterfaceOrientation==UIInterfaceOrientationPortrait && fromInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
      {
      CGRect rect=[bar frame];
    rect.size.width=568;
    [bar setFrame:rect];
    [_moviePlayer.view setFrame:CGRectMake(0,44, 480, 320-44)];
    }
   else
    {
    [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
    CGRect rect=[bar frame];
    rect.size.width=self.view.frame.size.width;
    [bar setFrame:rect];
    }*/
   }
    - (void)didRotate:(NSNotification *)notification
     {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation     ==UIInterfaceOrientationLandscapeRight)
    {
    CGRect rect=[bar frame];
    if([[UIScreen mainScreen] bounds].size.height==568)
    {
        rect.size.width=568;
    }
    else{
        rect.size.width=480;

    }
    [bar setFrame:rect];
    [_moviePlayer.view setFrame:CGRectMake(0,44, rect.size.width, 320-44)];

  }
else if(orientation==UIInterfaceOrientationMaskPortrait && orientation!=UIDeviceOrientationPortraitUpsideDown)
   {
    if([[UIScreen mainScreen] bounds].size.height==568)
    {
    [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
    }
    else
    {
        [_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];

    }
    CGRect rect=[bar frame];
    rect.size.width=self.view.frame.size.width;
    [bar setFrame:rect];

   }
 }
- (void)viewDidLoad
{
[super viewDidLoad];
[self play];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didRotate:) name:UIDeviceOrientationDidChangeNotification object:nil];

}
-(IBAction)dismiss:(id)sender
{
//[self.view removeFromSuperview];

_moviePlayer =nil;

[self dismissViewControllerAnimated:YES completion:nil];
}
// Do any additional setup after loading the view from its nib.
-(void)play
 {

     NSURL *url = [NSURL URLWithString:@"stringurlvideo"];
    _moviePlayer =  [[MPMoviePlayerController alloc]initWithContentURL:url];
  // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:)
      //      name:MPMoviePlayerPlaybackDidFinishNotification
  //                                             object:_moviePlayer];
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFull:)
                                         //        name:MPMoviePlayerDidEnterFullscreenNotification
                                         //      object:_moviePlayer];
   // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidExit:)
                                          //       name:MPMoviePlayerDidExitFullscreenNotification
                                          //     object:_moviePlayer];
    _moviePlayer.controlStyle = MPMovieControlStyleDefault;
    _moviePlayer.shouldAutoplay = YES;
if([[UIScreen mainScreen] bounds].size.height==568)
  {

    [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
  }
else
   {
     [_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];

   }
    [self.view addSubview:_moviePlayer.view];
    //[_moviePlayer.view setCenter:self.view.center];
    [_moviePlayer setFullscreen:YES animated:YES];

    }
 - (void) moviePlayBackDidFull:(NSNotification*)notification
    {


    }
 -(void)moviePlayBackDidExit:(NSNotification*)notification 
 {
////[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];




 }
 -(void)viewWillAppear:(BOOL)animated
   {
 /*   if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait)
   {
      [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
    CGRect rect=[bar frame];
    rect.size.width=self.view.frame.size.width;
    [bar setFrame:rect];

   }
  else
   {

    CGRect rect=[bar frame];
    rect.size.width=480;
    [bar setFrame:rect];
    [_moviePlayer.view setFrame:CGRectMake(0,44, 480, 320-44)];

  }*/

  }
- (void)moviePlayBackDidFinish:(NSNotification*)notification {
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];

    if ([player
         respondsToSelector:@selector(setFullscreen:animated:)])
     {
        [player.view removeFromSuperview];
     }
     }

 - (void)didReceiveMemoryWarning
    {
   [super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
   }


  -(NSUInteger)supportedInterfaceOrientations
  {
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
   }


 - (BOOL)shouldAutorotate
 {
 return YES;
  }
  /*- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
 {
return UIInterfaceOrientationLandscapeLeft;
 }*/
   - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation    duration:(NSTimeInterval)duration
    {
if(toInterfaceOrientation==UIInterfaceOrientationPortrait && toInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
    {

    if([[UIScreen mainScreen] bounds].size.height==568)
    {

    [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
    }
    else
    {
        [_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];

    }
    CGRect rect=[bar frame];
    rect.size.width=self.view.frame.size.width;
    [bar setFrame:rect];
    }
  else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation ==UIInterfaceOrientationLandscapeRight)
    {
    CGRect rect=[bar frame];
    if([[UIScreen mainScreen] bounds].size.height==568)
    {
    rect.size.width=568;
    }
    else{
        rect.size.width=480;

    }
    [bar setFrame:rect];
    [_moviePlayer.view setFrame:CGRectMake(0,44, rect.size.width, 320-44)];
      }
      }
   - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
      {
  /*  if(fromInterfaceOrientation==UIInterfaceOrientationPortrait && fromInterfaceOrientation!=UIInterfaceOrientationPortraitUpsideDown )
      {
      CGRect rect=[bar frame];
    rect.size.width=568;
    [bar setFrame:rect];
    [_moviePlayer.view setFrame:CGRectMake(0,44, 480, 320-44)];
    }
   else
    {
    [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
    CGRect rect=[bar frame];
    rect.size.width=self.view.frame.size.width;
    [bar setFrame:rect];
    }*/
   }
    - (void)didRotate:(NSNotification *)notification
     {
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIInterfaceOrientationLandscapeLeft || orientation     ==UIInterfaceOrientationLandscapeRight)
    {
    CGRect rect=[bar frame];
    if([[UIScreen mainScreen] bounds].size.height==568)
    {
        rect.size.width=568;
    }
    else{
        rect.size.width=480;

    }
    [bar setFrame:rect];
    [_moviePlayer.view setFrame:CGRectMake(0,44, rect.size.width, 320-44)];

  }
else if(orientation==UIInterfaceOrientationMaskPortrait && orientation!=UIDeviceOrientationPortraitUpsideDown)
   {
    if([[UIScreen mainScreen] bounds].size.height==568)
    {
    [_moviePlayer.view setFrame:CGRectMake(0,200, 320, 200)];
    }
    else
    {
        [_moviePlayer.view setFrame:CGRectMake(0,150, 320, 200)];

    }
    CGRect rect=[bar frame];
    rect.size.width=self.view.frame.size.width;
    [bar setFrame:rect];

   }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文