我正在创建一个iPad应用程序,因为我想打开媒体播放器来播放视频。为此,我在ViewDidLoad()方法中创建了一个按钮。贝娄是我的代码。
- (void)viewDidLoad
{
[super viewDidLoad]
self.view.backgroundColor = [[[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"1.png"]] autorelease];
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];
m_btn2000.frame = CGRectMake(180, 330, 130, 200);
[m_btn2000 setImage:[UIImage imageNamed:@"2.png"] forState:UIControlStateNormal];
m_btn2000.clipsToBounds = YES;
[m_btn2000 addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:m_btn2000];
}
Bellow是我点击按钮的方法代码:
- (void)btnClicked:(id)sender
{
NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"test_video" ofType:@"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath1];
m_moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
m_moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
[m_moviePlayer.view setFrame:CGRectMake(0, 0, 1026, 748)];
[self.view addSubview:m_moviePlayer.view];
[m_moviePlayer play];
}
当我要点击按钮时,它会给我EXC_BAD_ACCESS错误。 但是我注意到一件事,当我把上面的整个代码放在按钮点击方法放入ViewDidLoad()方法时,它以正确的方式播放视频。
答案 0 :(得分:1)
原因是:您正在创建一个带有+(工厂方法)的按钮,并且您正在发送autorelease
消息。
您需要更改此行:
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];
到
m_btn2000 = [UIButton buttonWithType:UIButtonTypeCustom];