【发布时间】:2011-01-10 20:51:55
【问题描述】:
我创建了一个新的 ViewController(仅包含 .h 和 .m 文件)并添加了该代码来播放视频。视频完成后,我收到“Exe_bad_access”错误。
将“NSZombieEnabled=true”作为参数添加到可执行文件时出现错误消息:
"TestPlayingVideo[654:207] -[MPMoviePlayerController stop]:发送到已释放实例的消息 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
【问题讨论】:
标签: iphone video mpmovieplayercontroller exc-bad-access