无法使用MPMoviePlayerViewController播放video

我用下面的ViewController.m创build了一个新的项目。 当我运行应用程序,我可以看到一个预期的来源/大小(38,100,250,163)的盒子,但它是黑色的,没有video播放。 Xcode有一个奇怪的输出:

2012-08-23 15:36:45.559 VideoTest1[11398:c07] [MPAVController] Autoplay: Disabling autoplay for pause 2012-08-23 15:36:45.560 VideoTest1[11398:c07] [MPAVController] Autoplay: Disabling autoplay 2012-08-23 15:37:18.186 VideoTest1[11398:c07] [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 

请注意,video转换Videora iPhone转换器,并在Xcode中播放确定(所以这不是一个video问题); video的path是好的,因为当我指定demo-iPhone1(这不存在),我得到一个零例外。 我尝试在模拟器和iPhone上:总是黑匣子。 有任何想法吗?

 #import "ViewController.h" #import <MediaPlayer/MediaPlayer.h> @interface ViewController () @end @implementation ViewController - (void)moviePlaybackComplete:(NSNotification *)notification { MPMoviePlayerController *moviePlayerController = [notification object]; [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController]; [moviePlayerController.view removeFromSuperview]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString *filepath = [[NSBundle mainBundle] pathForResource:@"demo-iPhone" ofType:@"mp4"]; NSURL *fileURL = [NSURL fileURLWithPath:filepath]; MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController]; [moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)]; [self.view addSubview:moviePlayerController.view]; [moviePlayerController play]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } @end 

我刚刚解决了这行代码的类似问题。 玩家控制器现在显示出来,video播放完美:

 @property (nonatomic, strong) MPMoviePlayerController *moviePlayer; // @synthesize moviePlayer = _moviePlayer; // [self.moviePlayer prepareToPlay]; 

修改以适应您的环境。

我通过添加playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;解决类似的问题playercontroller.moviePlayer.movieSourceType = MPMovieSourceTypeFile;

你用ARC吗? 如果是这样,你必须保留MPMoviePlayerController!

将其添加到您的界面

 @property (nonatomic, strong) MPMoviePlayerController *controller; 

检查最后一行viewDidLoad

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSString *filepath = [[NSBundle mainBundle] pathForResource:@"demo-iPhone" ofType:@"mp4"]; NSURL *fileURL = [NSURL fileURLWithPath:filepath]; MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlaybackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController]; [moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)]; [self.view addSubview:moviePlayerController.view]; [moviePlayerController play]; [self setController:moviePlayerController]; } 

另外检查video格式。 我不断收到我的示例video.m4v (我从苹果网站下载)这个错误。 最终我用一个不同的video剪辑,它是.mp4 ,它工作正常。 许多错误仍然popup在我的控制台上。

 2013-01-22 15:44:04.850 VideoTesting[4497:c07] [MPAVController] Autoplay: Likely to keep up or full buffer: 0 2013-01-22 15:44:04.851 VideoTesting[4497:c07] [MPAVController] Autoplay: Skipping autoplay, not enough buffered to keep up. 2013-01-22 15:44:04.853 VideoTesting[4497:c07] [MPAVController] Autoplay: Disabling autoplay for pause 2013-01-22 15:44:04.853 VideoTesting[4497:c07] [MPAVController] Autoplay: Disabling autoplay 2013-01-22 15:44:04.861 VideoTesting[4497:c07] [MPAVController] Autoplay: Enabling autoplay 

该video仍然播放。