在我的一个应用程序中,我想播放来自local.so iam的视频文件,使用以下代码播放该文件。
-(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 Line,当我做autorelease泄漏不会发生但视频不会播放。所以如何解决这个问题谁能帮忙?? < / p>
答案 0 :(得分: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;
}