mediaplayer框架不能在iOS 6中播放video

我正在使用iOS 6中包含的mediaplayer框架来尝试从应用内播放电影。 我导入,然后:

-(IBAction)playMovie:(id)sender { NSString *filepath = [[NSBundle mainBundle] pathForResource:@"buyTutorial" ofType:@"mov"]; NSURL *fileURL = [NSURL fileURLWithPath:filepath]; MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [self.view addSubview:moviePlayerController.view]; moviePlayerController.fullscreen = YES; [moviePlayerController play]; } 

当调用这个函数时,视图会变成空白屏幕和无限加载。 我已经尝试了许多其他版本的这种实现,结果各不相同,都失败了。 login中的特殊情况是:

 2012-11-05 21:19:27.900 [MPAVController] Autoplay: Disabling autoplay for pause 2012-11-05 21:19:27.902 [MPAVController] Autoplay: Disabling autoplay 2012-11-05 21:19:27.977 [MPAVController] Autoplay: Disabling autoplay for pause 2012-11-05 21:19:27.978 [MPAVController] Autoplay: Disabling autoplay 2012-11-05 21:19:27.984 [MPAVController] Autoplay: Skipping autoplay, disabled (for current item: 1, on player: 0) 2012-11-05 21:19:28.156 [MPAVController] Autoplay: Enabling autoplay 

有什么想法的原因? 这是我第一次尝试播放video,现在已经变成了一场噩梦。

在.h文件中添加以下内容

 @property (nonatomic, strong) MPMoviePlayerController *controller; 

尝试这个

  -(IBAction)playMovie:(id)sender { NSString *filepath = [[NSBundle mainBundle] pathForResource:@"buyTutorial" ofType:@"mov"]; NSURL *fileURL = [NSURL fileURLWithPath:filepath]; MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; [self.view addSubview:moviePlayerController.view]; moviePlayerController.fullscreen = YES; [moviePlayerController prepareToPlay]; [moviePlayerController play]; [self setController:moviePlayerController]; } 
 Am facing the same issue, you can else try playing it on the web view. NSURL *url = [NSURL fileURLWithPath:self.filePath]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [self.docWebView loadRequest:request]; 

你的代码的问题是variables“moviePlayerController”只是本地作用域,所以你调用后[moviePlayerController play]; 并退出局部variables释放的函数(因为这里有一个asyn操作,用play方法从队列中播放)。 它不保留内容指向的URL了。 所以你看到一个黑屏和一个不定式的“加载…”

您需要声明一个类的实例variables,并将内容从局部variables复制到类的属性,如上面的示例代码@iAppDeveloper。 它应该工作!