R – MPMoviePlayerController will play once, then throw an error

iphonempmovieplayercontroller

I realise that a similar question has been posted before, but I really can't seem to find a solution that works for me. I have a MoviePlayer class which stores an ivar of MPMoviePlayerController, and I have the following method in the class:

-(void)playMovie:(NSString *)movieName
{
    NSURL *movieURL;
    NSBundle *bundle = [NSBundle mainBundle];
    if(bundle)
    {
        NSString *moviePath = [bundle pathForResource:movieName ofType:@"m4v"];
        if(moviePath)
        {
            movieURL = [NSURL fileURLWithPath:moviePath];
        }
    }
    MPMovieController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    if(mp)
    {
        self.moviePlayer = mp;
        [mp release];

        [self.moviePlayer play];
    }
    [movieURL release];
}

When call I play movie once the movie plays fine, but when it is called again on a different (or the same) movie file I get the following error stack:

_class_isInitialized
_class_lookupMethodAndLoadCache objc_msgSend
-[MoviePlayer setMoviePlayer:]
-[MoviePlayer playMovie:]

I'm not sure how to fix it! I assumed that when self.moviePlayer = mp is called then the current moviePlayer is released and the new one is added? The property is set to (nonatomic, retain). Can someone help please?

Thanks

Best Answer

You have released the movie player. So it has been deallocated.

It seems you have released it elsewhere in your code, probably in the call back method. Just look for every instance you used it.

moviePlayer now points to garbage. So when you try to create a new moviePlayer, your property accessor attempts to send a release message to the garbage stored in moviePlayer.

if you want to deallocate moviePlayer between uses, don't deallocate it, instead set it to nil.

[self setMoviePlayer:nil];

Then when you try to create one you won't be messaging to garbage.

Related Topic