我正在使用其中一个视图具有 MPMoviePlayer 的 tabbarcontroller。它工作正常,除了如果我更改标签,电影不会停止并继续在后台播放。然后,如果我尝试返回到电影选项卡,它就会崩溃。
我认为我必须释放 MPMoviePlayer 的唯一代码是在它完成播放时,但我希望它在我更改视图时被释放。然后,如果我回到电影选项卡,我们会重新开始。
在我的 .h 文件中设置为:
import < UIKit/UIKit.h>
import < MediaPlayer/MediaPlayer.h>
@interface SecondViewController : UIViewController {
MPMoviePlayerController *player;
}
@end
在我的 .m 文件中有:
- (void)viewDidLoad {
NSString *url = [[NSBundle mainBundle]
pathForResource:@"vid"
ofType:@"m4v"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//--called when the movie view and then add it to the View window--
player.view.frame = CGRectMake(10, 10, 300, 300);
[self.view addSubview:player.view];
//--play movie--
[player pause];
[super viewDidLoad];
}
//--called when the movie is done playing--
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
[player release];
}
有什么建议么?谢谢 :)