How to release MPMoviePlayerController?

淳于博文
2023-12-01

        在制作在线播放视频功能时,嵌入一个MPMoviePlayerController,本以为在ViewController的dealloc函数中,将MPMoviePlayerController也 release了就行,结果发现它依然很坚强的在后台播放,通过查阅,原来需要先stop了才能release,所以解决方案如下:

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

添加事件通知监听:

	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinish:) 
												 name:MPMoviePlayerPlaybackDidFinishNotification 
											   object:moviePlayer]; //设置视频播放结束后的回调处理
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreload:) 
												 name:MPMediaPlaybackIsPreparedToPlayDidChangeNotification//MPMoviePlayerContentPreloadDidFinishNotification 
											   object:moviePlayer]; //设置视频开始播放的回调处理

停止时的处理:

-(void)movieFinish:(NSNotification*)notification{
	
	[self stopPlayer];
	
}

-(void)stopPlayer{
	
	moviePlayer.initialPlaybackTime = -1;
	
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200 //3.2以上的需要先暂停再停止
    [moviePlayer pause]; 
#endif
	
	[moviePlayer stop];
	
}

dealloc的时候再释放

- (void)dealloc {
	
	[self stopPlayer];//再停一次, 确保用户没点击“停止”而直接跳转
	[moviePlayer release]; 
	moviePlayer = nil;	
}


 类似资料:

相关阅读

相关文章

相关问答