0

在我的一个应用程序中,我想从 local.so 播放视频文件,因此我使用以下代码播放文件。

-(IBAction)playMovie{

NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:@"Myvideo" ofType:@"mov"]];

MPMoviePlayerController *moviePlayer=[[[MPMoviePlayerController alloc]initWithContentURL:url]autorelease];
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
moviePlayer.shouldAutoplay = YES;

[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
//[moviePlayer release];
}

-(void) moviePlayBackDidFinish:(NSNotification*)notification {

MPMoviePlayerController *moviePlayer = [notification object];

[[NSNotificationCenter defaultCenter] removeObserver:self      
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:moviePlayer];
if ([moviePlayer 
     respondsToSelector:@selector(setFullscreen:animated:)])
{
   [moviePlayer.view removeFromSuperview];
}
[moviePlayer release];

}

当我去分析或做上述代码的配置文件时,显示警告潜在泄漏发生在 2 个地方,即,

[moviePlayer setFullscreen:YES animated:YES];

//[moviePlayer release];

另一个是泄漏在这部分

{

  [moviePlayer.view removeFromSuperview];

}
[moviePlayer release]

那么如何避免这种内存泄漏。即使我尝试在代码的开头自动释放 NSURL 行,当我执行自动释放泄漏时不会发生但视频不会播放。那么如何解决这个问题有人能帮忙吗?

4

1 回答 1

0

在moviePlayBackDidFinish方法中试试这个,可能会有所帮助

 if (videoPlayer) {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification object:videoPlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerLoadStateDidChangeNotification object:videoPlayer];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMovieMediaTypesAvailableNotification object:videoPlayer];
    [videoPlayer pause];
    videoPlayer.initialPlaybackTime = -1;
    [videoPlayer stop];
    [videoPlayer.view removeFromSuperview];
    [videoPlayer release];
    videoPlayer = nil;
} 
于 2012-10-04T09:54:45.193 回答