开发者

MPMoviePlayer Bad-Access error after playing video

开发者 https://www.devze.com 2023-02-03 17:37 出处:网络
I´ve created an new ViewController (only with the .h and .m file) and added that code to play a video. After the video has finished, i get a \"Exe_bad_access\" error.

I´ve created an new ViewController (only with the .h and .m file) and added that code to play a video. After the video has finished, i get a "Exe_bad_access" error.

Error message when adding "NSZombieEnabled=true" to the excecutable as a argument:


"TestPlayingVideo[654:207] -[MPMoviePlayerController stop]: message sent to deallocated instance 0x63042d0"


Whats wrong with that? How can i do correct memory management when playing video?

#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 defaultC开发者_StackOverflow中文版enter] 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


There's a lot of confusion here over what you're releasing: for example, here's your main alloc of your movie player:

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

But what you are releasing isn't this moviePlayerController - you're only releasing the .moviePlayer property of your MPMoviePlayerController. Notice when you create your NSNotification you're passing moviePlayerController.moviePlayer, not simply moviePlayerController.

So you're not releasing your moviePlayerController, you're in fact attempting to release a property of that object. Which you shouldn't do - you should release the object, and let it worry about releasing its properties.

0

精彩评论

暂无评论...
验证码 换一张
取 消