iPhone development mpmovieplayer crashing

10,326

Solution 1

What fixed this problem for me was stopping the MPMoviePlayerController before doing the setContentURL.

    MPMoviePlayerController *streamPlayer;

    [streamPlayer stop];
    [streamPlayer setContentURL:[NSURL URLWithString:selectedStation]];

Solution 2

In the implementation you have above, ARC doesn't know that the MPMoviePlayerController is finished and needs to be released.

Define MPMoviePlayerController in your .h file and make it accessible via a @property (and @synthesize).

@property (strong, nonatomic) MPMoviePlayerController * moviePlayerController;

Then take the result of your alloc & init and assign it to that. I.E.

self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];

Solution 3

you should just keep the moviePlayerController and if you want to play another video, just use

[self.moviePlayerController setContentURL:movieURL];

then in your notification callback:

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    self.moviePlayer = nil;
    [self initanothermovieplayerandplay];
}

and please do not remove the notification handler from notification center, only do this in dealloc method of your VC.

now let's add some fade when the movie play is done:

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    [UIView animateWithDuration:1
                      delay: 0.0
                    options: UIViewAnimationOptionCurveEaseIn
                 animations:^{
                     // one second to fade out the view
                     self.moviePlayer.view.alpha = 0.0;
                 }
                 completion:^(BOOL finished){
                       self.moviePlayer = nil;
                       [self initanothermovieplayerandplay];
                 }
}

Solution 4

I had exactly the same problem. Nothing was wrong with my and i guess with your code :) Just a broken video file was mine problem. Changing *.mov type to m4a for example fixed it. Maybe one or more of the files you play are corrupted? Try to find out which files lead to crash and than if u can try to quickly forward backward the play position of one of them while playing - this should lead to crash in few tries. This is how i found the bad files. By the way all my bad files were movies .mov made with Snapz Pro X :)

Share:
10,326
Jeff B
Author by

Jeff B

Updated on July 21, 2022

Comments

  • Jeff B
    Jeff B almost 2 years

    I am working on an app that will let me play different videos on the iPad remotely with an iPhone. I have been following along with apples example for a video player but I've been having some troubles. The videos play just fine and I can get it to play from a variety of videos but switching between them a few times it will crash and i get this in the debugger:

    *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'An        AVPlayerItem cannot be associated with more than one instance of AVPlayer'
    *** First throw call stack:
    (0x380da8bf 0x37c261e5 0x30acbcb5 0x30abc1f7 0x30ac3bf3 0x30c93d55 0x30c95f7b 0x380ad2dd   0x380304dd 0x380303a5 0x37e07fcd 0x31bb0743 0x25e5 0x257c)
    

    This is the code I am using to create the player:

    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentOfURL:movieURL];
    if (player) {
        [self setMoviePlayerController:player];
        [self installMovieNotificationObservers];
        [player setContentURL:movieURL];
        [player setMovieSourceType:sourceType];
        [self applyUserSettingsToMoviePlayer];
        [self.view addSubview:self.backgroundView];
        [player.view setFrame:self.view.bounds];
        [player.view setBackgroundColor = [UIColor blackColor];
        [self.view addSubview:player.view];
    }
    

    And when the current movie is stopped I use:

    [[self moviePlayerController] stop];
    
    MPMoviePlayerController *player = [self moviePlayerController];
    [player.view removeFromSuperview];
    
    [self removeMovieNotificationHandlers];
    [self setMoviePlayerController:nil];
    

    Edit: So Ive now discovered it happens every time i try and switch a video for the 11th time. weird! I'm practically pulling my hair out.

  • Jeff B
    Jeff B over 12 years
    even if i have ARC turned on? when i try and put a release in it just says its not allowed in ARC mode. should i just turn arc off?
  • Michael Dautermann
    Michael Dautermann over 12 years
    gah... ARC. I'm still getting accustomed to it. You need your parent (view controller?) to explicitly retain your MPMoviePlayer and then ARC will know what to do with it. I'll change my answer to reflect this.
  • Jeff B
    Jeff B over 12 years
    still no luck :-( could it be trying to add a video before the other one is stopped? it only happens after switching between them a few times, until them everything seems ok. i started up interments to check for leaks and found nothing. is there something else i can use to debug this problem? I've enabled NSZombieEnabled but that doesn't help, i guess its not releasing something thats already been released.
  • ferostar
    ferostar over 12 years
    Sure! Besides setContentURL i add prepareToPlay, in order to auto-play. If i don't put prepare... it doesn't. The thing is, as @Jeff B said, it doesn't happens every time. Is after a couple of calls that this error happens.
  • Allen
    Allen over 12 years
    @ferostar i added some code in the answer, let me know it helps or not.
  • ferostar
    ferostar over 12 years
    Thanks for the code! But i believe something must be possible other then making the thing nil and recreating it, MPMovie... should be able to transition smoothly from one video to the other without something like this, don't you think?
  • Dev Kanchen
    Dev Kanchen about 12 years
    Interesting. I'll check this.
  • Dev Kanchen
    Dev Kanchen about 12 years
    This seems to be right. We've stopped seeing the crash after removing some test videos etc that were of random origin, but a "co-symptom" of the crash was high CPU usage and the app freezing when starting to play video. We've also resolved the high CPU issue, but now it's not entirely clear what caused the issue and what exactly removed it. We just fired all over the place and probably got it. But a week more of testing would prove that at least one of those methods work. Sadly I haven't found a way of extending a bounty. Let me see.
  • Dev Kanchen
    Dev Kanchen about 12 years
    Btw, we were trying to play m3u8's from a server, and not faced any problems until early last week. The videos were all sourced as MP4s, all from the same client - therefore highly likely to be recorded, encoded with the same settings. We know for a fact they're all 1080p at about 8-10mbps on an average. Maybe all thusly points to a content issue indeed - since last week is the first time we uploaded some random videos to test the system. And the video playback/serving code hadn't changed for the past few weeks.
  • Dev Kanchen
    Dev Kanchen about 12 years
    So this could be closest to the reason we faced this issue. But not having concrete proof yet, makes me reluctant to close the bounty now. One week of testing would be great. :( I had read somewhere about starting multiple bounties, but haven't been able to figure it out yet.
  • Dev Kanchen
    Dev Kanchen about 12 years
    P.s. The CPU-usage fix is the first change we made to the playback code in weeks, and that was AFTER we faced the issue.
  • Dev Kanchen
    Dev Kanchen about 12 years
    You know what, I'd rather award the bounty than letting these points go waste. Thanks Asen. For the sake of completeness, I'll post back with our test results so we have accurate data here for future developers coming here looking for an answer.
  • Ahmed Ahmedov
    Ahmed Ahmedov about 12 years
    Welcome and thanks! Post here the results after week i'm curious too :) By the way mine wasn't related to cpu usage in any way.
  • shawnwall
    shawnwall almost 12 years
    It is important to set the movieplayer instance to nil as Allen states - that alone solved my issue in my player that I wasn't even trying to reuse.