-2

当 ARC 开启时,我无法播放视频(视频一直在加载)。当 ARC 关闭时,我可以成功播放视频。我不知道为什么以及如何编辑我的代码以在 ARC 开启的情况下播放视频。这里是我的代码

-(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];

}
4

1 回答 1

0

有几种方法:

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