我正在创建一个应用程序,我可以在其中播放我通过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;
}
我不知道我错在哪里。请让我知道代码中的任何问题。为了获得正确的方向。
Ragards Abhi
答案 0 :(得分:1)
你应该只在shouldAutorotateToInterfaceOrientation:
方法中返回YES或NO,它仅由框架调用以获取有关控制器中支持的方向的信息,请阅读Apple文档。
您需要注册方向更改通知
[[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];
以下是一些链接
答案 1 :(得分: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
}
}
答案 2 :(得分: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] retain];
MPMoviePlayerController * theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.view.frame = CGRectMake(184,200,400,300);
[电影剧];
MPMoviePlayerViewController * moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];