2011-01-10 2 views
0

새로운 ViewController (.h 및 .m 파일 만 사용)를 만들고 비디오를 재생하기 위해 코드를 추가했습니다. 비디오가 끝나면 "Exe_bad_access"오류가 발생합니다.MPMoviePlayer 비디오 재생 후 잘못된 액세스 오류

인자가 같이 excecutable에 "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 

답변

1

이 많은 혼란이 여기에 당신이 방출하고 이상입니다 : 예를 들어, 여기에 동영상 플레이어의 주요 ALLOC입니다 :

MPMoviePlayerViewController* moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:url] ]; 

그러나 당신이 을 발표하는 것은이 아니다 moviePlayerController - MPMoviePlayerController의 .moviePlayer 속성 만 릴리스합니다. NSNotification을 만들 때 moviePlayerController.moviePlayer, 이 아닌이 단순히 moviePlayerController가됩니다.

moviePlayerController를 릴리스하지 않으므로 실제로 해당 객체의 속성을 해제하려고 시도하고 있습니다. 당신이하지 말아야 할 것은 - 객체를 해제해야하고, 객체의 속성을 해제하는 것에 대해 걱정해야합니다.

+0

먼저 귀하의 빠른 답변에 감사드립니다. 그렇다면 정확하게 MPMoviePlayerViewController 유형의 객체와 해당 movieplayer-Object에 대한 알림을 설정하는 방법은 무엇입니까? "moviePlayerController.moviePlayer"를 전달하지 않으면 동영상이 끝나면 알림을받지 못합니다. MPMoviePlayerViewController를 사용하면서 비디오를 표시하는 올바른 방법은 무엇입니까? – geforce

+0

moviePlayerController.moviePlayer를 전달하지 않으면 왜 알림을받지 못할지 잘 모릅니다. 객체 매개 변수는 모니터링 할 객체를 제한하는 데 사용되지만 항상 두 개 이상의 영화 컨트롤러를 사용하지 않을 것이므로 반드시 필요하지는 않습니다. 해당 속성이 아닌 MPMoviePlayerViewController를 해제해야합니다. 이 작업을 수행하는 한 가지 방법은 무비보기 컨트롤러를 현재보기 컨트롤러의 속성으로 만들고 dealloc 메서드에서 해제하는 것입니다. – lxt

+0

이제 알림을 다음으로 변경했습니다. "MPMoviePlayerViewController * moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL : [ NSURL fileURLWithPath : url]]; \t [NSNotificationCenter defaultCenter] addObserver : 셀렉터 : @selector (moviePlayBackComplete :) 이름 : MPMoviePlayerPlaybackDidFinishNotification 객체 : moviePlayerController]; ". 이제 알림이 MPMoviePlayerViewController가 아닌 MPMoviePlayer에만 있기 때문에 NSLog 출력을 얻지 못합니다. – geforce