2012-04-10 89 views
0

我正在創建一個iPad應用程序,我想打開媒體播放器播放視頻。爲此,我在ViewDidLoad()方法中創建一個按鈕。貝婁是我的代碼。視頻播放器給出錯誤(EXC_BAD_ACCESS)

- (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]; 
} 

貝婁是我的按鈕點擊方法代碼:

- (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()方法它播放適當的方式播放視頻。

回答

1

原因是:您正在使用+(工廠方法)創建按鈕,並且您正在發送autorelease消息。

你需要這條線從改變:
m_btn2000 = [[UIButton buttonWithType:UIButtonTypeCustom] autorelease];

m_btn2000 = [UIButton buttonWithType:UIButtonTypeCustom]; 
+0

最好的之一......解決。你能否就此簡單闡述一下? – 2012-04-10 11:10:21

+0

+1,它(按鈕對象)已經由工廠方法autoreleased了。 – Ilanchezhian 2012-04-10 11:11:35

+0

好的......謝謝.. – 2012-04-10 11:14:05