注意: SDK4.0之前, 直接用MPMoviePlayerController,然后传个视频路径就可以看到播放界面了.
而SDK4.0之后, 如果你还是用MPMoviePlayerController, 需要自己在代码里把MPMoviePlayerController的view加到你的view上,否则虽然视频在播放,但你是看不到界面的. 当然, 如果你还是想直接就看到播放界面, 可以用新的MPMoviePlayerViewController这个controller(它就相当于4.0之前的 MPMoviePlayerController)
-(IBAction)playMovie
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"Movie" ofType:@"mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
//定义视频横屏播放
[[moviePlayerViewController view] setTransform:CGAffineTransformMakeRotation(M_PI / 2)];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:@"MPMoviePlayerPlaybackDidFinishNotification"
object:nil];
//在当前view上添加视频的视图
[[[UIApplication sharedApplication] keyWindow] addSubview:moviePlayerViewController.view];
}
//点击完成返回首页
- (void)moviePlayBackDidFinish:(NSNotification*)notification{
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO];
[moviePlayerViewController.moviePlayer stop];
[moviePlayerViewController.view removeFromSuperview];
}