0

My Situation: I am recording an audio file with the AVAudioRecorder. To play the record I am using the MPMoviePlayerViewController. Recording, playing, stopping etc. is working.

My Problem: After the playback: When I press the home button (bring my app to the background) and open my app again the app crashes. It happens only when the playback is completed (pressed 'Finish' or let it play till the end). When I press the home button during playing and open the app again everything is fine.

Here my code for the movieplayer: (using the code from here)

- (void)playVideo:(NSString*)aVideoUrl {
  MPMoviePlayerViewController *playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:aVideoUrl]];  

  // Remove the movie player view controller from the "playback did finish" notification observers
  [[NSNotificationCenter defaultCenter] removeObserver:playerVC
                                              name:MPMoviePlayerPlaybackDidFinishNotification
                                            object:playerVC.moviePlayer];

  // Register this class as an observer instead
  [[NSNotificationCenter defaultCenter] addObserver:self
                                       selector:@selector(movieFinishedCallback:)
                                           name:MPMoviePlayerPlaybackDidFinishNotification
                                         object:playerVC.moviePlayer];

  // Set the modal transition style of your choice
  playerVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;

  // Present the movie player view controller
  //[self presentModalViewController:playerVC animated:YES];
  [self presentMoviePlayerViewControllerAnimated:playerVC];

  // Start playback
  [playerVC.moviePlayer prepareToPlay];
  [playerVC.moviePlayer play];
}

- (void)movieFinishedCallback:(NSNotification*)aNotification {
  MPMoviePlayerController *moviePlayer = [aNotification object];

  // Remove this class from the observers
  [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:moviePlayer];

  // Dismiss the view controller
  [self dismissModalViewControllerAnimated:YES];
  //[self dismissMoviePlayerViewControllerAnimated];
}

I am using iOS5 and ARC.

The class with the code is a normal UIViewController and is also the caller of the method.

It happens only on devices. No crash in the Simulator, but on my iPhone 4 and iPad 1.

I can't find the reason why my app crashes.

Stacktrace from the crash

Edit: I am not quite sure right know, but I think I solved my crash.

I believe the problem is not playing the audiofile, or not only. Recording seems to be part of this problem, too.

In my class for recording with AVAudioRecorder I registered this class as the delegate of AVAudioSession: [[AVAudioSession sharedInstance] setDelegate: self];

I moved this to my "root" class (my "main" class, from where I start everything) and when I implement the protocol methods 'beginInterruption' and 'endInterruption' (know only with a NSLog statement) I can see that 'beginInterruption' is called when I set the app in the background and 'endInterruption' when I start the app again (foreground). In the stacktrace you can see something like "AudioSessionInterruptionListener"...seems to be part of the delegate methods.

Edit 2:

I tested my app a few times and the crash never happens again. Setting the AVAudioSessionDelegate to my "root" class seems to be the answer.

1 Answer 1

0

For me it was necessary to register the AVAudioSession delegate:

[[AVAudioSession sharedInstance] setDelegate: self];

I added this in the "parent" class of my audiorecord handling class.
Also I had to implement beginInterruption and endInterruption of the AVAudioSession delegate.

After this the crash never happened again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.