我创建了一个新的ViewController(只有.h和.m文件)并添加了该代码来播放视频。视频播放完毕后,我收到“Exe_bad_access”错误。
将“NSZombieEnabled = true”作为参数添加到excecutable时出现错误消息:
“TestPlayingVideo [654:207] - [MPMoviePlayerController stop]:发送给deallocated实例的消息 0x63042d0"
那有什么不对?如何在播放视频时进行正确的内存管理?
#import "TestPlayingVideoViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@implementation TestPlayingVideoViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self.view setBackgroundColor:[UIColor darkGrayColor]];
UIButton* btn = [[UIButton alloc] initWithFrame:CGRectMake(50 , 50, 200, 25)];
[btn setTitle:@"press me" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
}
- (void)action:(id)sender
{
NSLog(@"UIButton was clicked");
NSString *url = [[NSBundle mainBundle] pathForResource:@"mymovie" ofType:@"m4v"];
MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackComplete:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController.moviePlayer];
[moviePlayerController.moviePlayer play];
//[self.view addSubview:moviePlayerController.view];
[self presentMoviePlayerViewControllerAnimated:moviePlayerController];
}
- (void) moviePlayBackComplete:(NSNotification*) notification {
MPMoviePlayerController* player = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:player];
[self dismissMoviePlayerViewControllerAnimated];
[player stop];
//[self.view removeFromSuperView];
[player release];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
答案 0 :(得分:1)
这里有很多关于你发布的内容的混淆:例如,这是你的电影播放器的主要内容:
MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ];
但是你发布的不是这个moviePlayerController - 你只是发布了你的MPMoviePlayerController的.moviePlayer属性。请注意,当您创建NSNotification时,您正在传递moviePlayerController.moviePlayer,不是只是moviePlayerController。
所以你没有发布你的moviePlayerController,你实际上是在尝试释放该对象的属性。您不应该这样做 - 您应该释放该对象,并让它担心释放它的属性。