MPMoviePlayerController视图未调整大小

时间:2011-06-22 18:57:36

标签: ios cocoa-touch uiview mpmovieplayercontroller

我正在使用MPMoviePlayerController,它在风景上将其UIView渲染到正确的尺寸,但是当旋转到肖像时,它根本就不会调整大小。我认为它的父母UIView是没有调整大小的人。我该怎么设置呢?

我按照@Alex Reynolds在UIView autoresizingmask not working for me中提到的说明进行操作。这向我展示了视图在旋转时会调整大小。我仍然遇到这样的问题:当加载了保存播放器的UIView的{​​{1}}时,如果设备的方向是横向的,它们会渲染到正确的框架,但是如果设备打开了加载时的肖像不会调整大小。有了@Alex Reynolds的回答,我所要做的就是旋转设备一次,之后它会开始正确调整大小。

第一次不会调整大小仍然很糟糕。以前发生过这种情况吗?如果是这样,我们非常感谢任何输入。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

- (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];

   }
 }