views:

35

answers:

2

I have a class that plays a repeating background music loop with an AVAudioPlayer, and on specific occasion, plays a full-screen video with its own sound track using MPMoviePlayerController. In order to to have only one track at a time, I stop the background music before launching the video:

-(void)startVideo{
    [backgroundMusic stop];
    MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"videofile" ofType:@"m4v"]]];
    [self presentMoviePlayerViewControllerAnimated:mp];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoOver) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    [mp.moviePlayer play];
    [mp release];
}

-(void)videoOver{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
    if(![backgroundMusic play]){
         NSLog(@"bad: can't resume bg music!");
         [backgroundMusic release];
         backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"videofile" ofType:@"m4v"]] error:NULL];
         backgroundMusic.delegate = self; 
         backgroundMusic.numberOfLoops = -1; 
         [backgroundMusic play];
    }
}

The resumption worked fine without recreating the AVAudioPlayer object (i.e. the play method returned YES) on analogous code on os versions up to and including 3.2. But on iOS4, the play method always returns NO, and has to recreate the object. Why is that, and can I get to resume the background track properly (I have cases where the solution used above is unacceptable.)?

A: 

AVAudioPlayer has property to pause and resume the music use that before movie starts and after move stops to resume that.

[self.audioPlayer pause]; to pause. [self.audioPlayer play]; to play from where it stop.

jeeva
That doesn't work. When the audioplayer is paused, the movie appears and freezes on the first frame, and doesn't play.
executor21
A: 

Figured this out. It turns out that in iOS 3.2 and above, when a video finishes playing, it goes into MPMoviePlaybackStatePaused state rather than MPMoviePlaybackStateStopped, and in order to make it release the hardware, you have to explicitly call the stop method on MPMoviePlayerController after it finishes playing before trying to resume AVAudioPlayer.

executor21