1

我正在创建一个应用程序,我可以在其中播放我通过 mpmovieplayer 控制器执行的视频。现在我需要为两个方向执行此操作。但是框架没有正确设置。

代码如下

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
    [self shouldAutorotateToInterfaceOrientation:[UIDevice currentDevice].orientation];

    NSURL *temp = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Floating" ofType:@"mp4"]];
    mpviewController = [[MPMoviePlayerViewController alloc] initWithContentURL:temp]; 
    mpviewController.view.frame = CGRectMake(0, 0, 768, 1024);
    mpviewController.view.backgroundColor = [UIColor clearColor]; 
    mpviewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
    mpviewController.view.userInteractionEnabled= NO;
    mpviewController.moviePlayer.fullscreen= YES;
    mpviewController.moviePlayer.controlStyle = MPMovieControlStyleNone;
    [[mpviewController moviePlayer] play];

    [self.view addSubview:mpviewController.view]; 

}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    currentOrientation = interfaceOrientation;
    //[self SetInterface];

    if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        mpviewController.view.frame = CGRectMake(0, 0, 768, 1024);
    else if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight)
        mpviewController.view.frame = CGRectMake(0, 0, 1024, 768);




    return YES;
}

我不知道我错在哪里。请让我知道在代码中进行的任何更改。从而获得正确的定位。

拉加兹阿比

4

3 回答 3

1

您应该只在方法中返回 YES 或 NO,shouldAutorotateToInterfaceOrientation:框架调用它只是为了获取有关控制器中支持的方向的信息,请阅读苹果文档以获取相同的信息。

您需要注册方向更改通知

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];

实现你的orientationChanged:方法。

//********** ORIENTATION CHANGED **********
- (void) orientationChanged:(NSNotification *)note
{
    NSLog(@"Orientation  has changed: %d", [[note object] orientation]);
   //Put your code here from shouldAutorotateToInterfaceOrientation:
}

不要忘记删除它。

[[NSNotificationCenter defaultCenter] removeObserver:self];

这是一些链接

设备方向 - 自动旋转?

方向改变通知

于 2011-06-18T13:53:14.300 回答
1

首先 ,我相信您不需要调整 mpviewController 的大小,因为它会自行调整大小。你应该只设置 -

第二

在 shouldAutorotateToInterfaceOrientation 中,您只需在 shouldAutorotateToInterfaceOrientation 中设置支持的方向。

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {  
        return YES;
}

如果它不这样做,您可以更改视图属性 -

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

    if (UIInterfaceOrientationIsPortrait(interfaceOrientation)){
     //do what you want for portrait
     }else{
     //do what you want for landscape
    }

}
于 2011-06-18T13:53:52.270 回答
0

无需更改任何编码..只需将以下编码插入应用程序,它将自动检测方向...

UINavigationBar *bar = [self.navigationController navigationBar];
[bar setTintColor:[UIColor blackColor]]; NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"sharkdivertrailer" ofType:@"mp4"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] 保留];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(184, 200, 400, 300); [电影播放];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];

于 2011-06-22T12:40:48.670 回答