ARC使播放视频失败

时间:2013-06-06 08:52:57

标签: ios objective-c cocoa-touch

当ARC打开时,我无法播放视频(视频正在加载)。当ARC关闭时,我可以成功播放视频。我不知道为什么以及如何编辑我的代码以使用ARC播放视频on.Here是我的代码

-(IBAction)playMovie:(id)sender
{
    NSString *filepath   =   [[NSBundle mainBundle] pathForResource:@"big-buck-bunny-clip" ofType:@"m4v"];
    NSURL    *fileURL    =   [NSURL fileURLWithPath:filepath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlaybackComplete:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:moviePlayerController];
    [self.view addSubview:moviePlayerController.view];
    moviePlayerController.fullscreen = YES;
    [moviePlayerController play];
}
- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:moviePlayerController];
    [moviePlayerController.view removeFromSuperview];

}

1 个答案:

答案 0 :(得分:0)

有几种方法:

  1. 将MPMoviePlayerController移动到视图委托而不是局部变量。 (这是最好的方式,)
  2. 使用CFRetain()手动保留它(此CoreFoundation函数将在Objective-C对象上执行其工作而不会触发ARC错误。请记住在完成其工作后立即检索它并CFRelease()它。)
  3. 转到项目设置并使用编译器标志-fno-objc-arc标记此文件。 (这会让你的代码像地狱一样泄漏内存。)