MPMoviePlayerController will play once, then throw an error

坚强是说给别人听的谎言 提交于 2019-12-24 10:44:54

问题


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


回答1:


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.




回答2:


I fixed this problem. Turns out this code was the problem:

movieURL = [NSURL fileURLWithPath:moviePath];

The NSURL was being autoreleased too early for some reason. If I allocated the memory for this and released it myself, then the issue stopped happening.

Hopes this helps other people.

Stu



来源:https://stackoverflow.com/questions/1177329/mpmovieplayercontroller-will-play-once-then-throw-an-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!