2011-01-10 14 views
0

新しいViewController(.hと.mファイルのみ)を作成し、ビデオを再生するコードを追加しました。ビデオが終了した後、私は "Exe_bad_access"エラーを取得します。MPMoviePlayerビデオ再生後の不正アクセスエラー

エラーメッセージ引数としてexcecutableに "真NSZombieEnabled =" を追加する場合:


「TestPlayingVideo [654:207] - [MPMoviePlayerController停止]:割り当て解除インスタンスに送信されたメッセージ 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の.movi​​ePlayerプロパティを解放するだけです。あなたがNSNotificationを作成するときに、moviePlayerController.movi​​ePlayer、を渡しているのではなく、だけをmoviePlayerControllerに渡してください。

あなたはmoviePlayerControllerを解放していないので、実際にはそのオブジェクトのプロパティを解放しようとしています。あなたがしてはいけないこと - オブジェクトを解放し、そのプロパティを解放することを心配させるべきです。

+0

まずはお返事ありがとうございます。だから、私は正しくどのように私のオブジェクトを正しく設定するMPMoviePlayerViewControllerとそのムービープレイヤーオブジェクトに通知? "moviePlayerController.movi​​ePlayer"を渡さないと、ムービーが終了しても通知が表示されません。 MPMoviePlayerViewControllerを使用してビデオを表示する正しい方法は何ですか? – geforce

+0

moviePlayerController.movi​​ePlayerを渡さないと、なぜあなたは通知を受け取らないと思うか分かりません。あなたはまだ通知を受け取ります - オブジェクトパラメータは監視したいオブジェクトを制限するために使用されますが、いつでも複数のムービーコントローラを使用しているわけではないので、厳密には必須ではありません。そのプロパティのいずれかではなく、MPMoviePlayerViewControllerを解放していることを確認する必要があります。これを行う方法の1つは、ムービービューコントローラを現在のビューコントローラのプロパティにして、deallocメソッドで解放することです。 – lxt

+0

通知を「MPMoviePlayerViewController * moviePlayerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[ [NSNRL fileURLWithPath:url]]; \t [[NSNotificationCenter defaultCenter] addObserver:セルフセレクタ:@selector(moviePlayBackComplete :) name:MPMoviePlayerPlaybackDidFinishNotificationオブジェクト:moviePlayerController]; "今すぐ通知がMPMoviePlayerViewControllerではなくMPMoviePlayerのみであるため、NSLog出力は表示されません。 – geforce