如何发布MPMoviePlayerController?

时间:2009-03-29 20:23:46

标签: iphone memory-management mpmovieplayercontroller

我有几个访问电影播放器​​的视图。我已将以下代码放在AppDelegate中用于这些视图的方法中。他们发送文件名进行播放。代码工作正常,但我知道某个地方需要发布。如果我将最后一行添加为发布或自动释放,则一旦用户在电影播放器​​上按下完成,应用程序将崩溃。

MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] 
                 initWithContentURL:[NSURL fileURLWithPath:moviePath]];
moviePlayer.movieControlMode = MPMovieControlModeDefault;
[moviePlayer play];
//[moviePlayer release];

我收到此错误:

objc [51051]:FREED(id):消息videoViewController发送给释放对象= 0x1069b30

收到信号:“EXC_BAD_INSTRUCTION”。

我应该如何释放播放器?

6 个答案:

答案 0 :(得分:18)

我发现MPMoviePlayerController必须先发送停止消息才能安全释放。所以我在handlePlaybackEnd中做 - 首先我停止它,然后我自动释放它。调用版本似乎不太好用:

- (void) moviePlayBackDidFinish : (NSNotification *) notification
{
  VideoPlayerController * player = notification.object;
  [player stop];
  [player autorelease];
}

整个事情变得有点棘手,因为MPMoviePlayerPlaybackDidFinishNotification可以多次发送,但是调用stop / autorlease两次也不会对你有任何帮助。所以你需要以某种方式防范它。

最后,似乎需要对主运行循环进行一些迭代,直到您可以安全地创建新的MPMoviePlayerController实例。如果你做得太快,你会得到声音,但没有视频。非常有趣,是吗?

答案 1 :(得分:7)

要回答4thSpace对上述答案的评论,您可以删除通知观察者,以免多次收到:

- (void)moviePlayBackDidFinish:(NSNotification *)notification {
    MPMoviePlayerController *theMovie = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
        name:MPMoviePlayerPlaybackDidFinishNotification
        object:theMovie];
    [theMovie stop];
    [theMovie release];
}

答案 2 :(得分:3)

for iphone os 3.2 你需要打电话 [moviePlayer暂停]; 在打电话之前 [moviePlayer stop];

答案 3 :(得分:2)

如果玩家没有达到目的,停止和释放对我来说还不够。

我的解决方案是设置moviePlayer.initialPlaybackTime = -1 在释放之前moviePlayBackDidFinish:

-(void)playMovie: (NSString *)urlString{ 
    movieURL = [NSURL URLWithString:urlString]; 
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL]; 
    moviePlayer.initialPlaybackTime = 0; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish: ) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer] ;

    moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 
    moviePlayer.movieControlMode = MPMovieControlModeDefault;
    moviePlayer.backgroundColor = [UIColor blackColor];

    [moviePlayer play];
}

-(void)moviePlayBackDidFinish: (NSNotification*)notification{ 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer] ; 

    moviePlayer.initialPlaybackTime = -1; 

    [moviePlayer stop]; 
    [moviePlayer release]; 
}

答案 4 :(得分:0)

我遇到了同样的问题,我刚刚意识到我使用object设置了通知方法:nil(它是一个复制粘贴)。

虽然我根本不应该收到任何通知,但我收到了多个通知。

这是我修复所有的新通知设置代码(请参阅对象:moviePlayer):

[[NSNotificationCenter defaultCenter] addObserver:self 
                           selector:@selector(moviePlaybackDidFinish:) 
                           name:MPMoviePlayerPlaybackDidFinishNotification 
                           object:moviePlayer]; 

希望有所帮助。现在我的所有代码都正常运行。

答案 5 :(得分:0)

这似乎显着降低了记忆力。但是对于IOS 4.1来说似乎很好。

- (void)videoFinishedCallback:(NSNotification *)aNotification
{
    thePlayer = [aNotification object];
    [[NSNotificationCenter defaultCenter]
    removeObserver:self
    name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];

    thePlayer.initialPlaybackTime = -1;

    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
        [thePlayer pause]; 
    #endif

    [thePlayer stop];
    [thePlayer release];    
}