2012-05-15 142 views
0

我的情况后: 我记录与AVAudioRecorder音频文件。 要播放记录,我正在使用MPMoviePlayerViewController。 录音,播放,停止等正在工作。崩溃与MPMoviePlayerViewController背景和前景之间切换应用

我的问题: 回放之后:当我按主页按钮(将我的应用程序背景)并再次打开我的应用程序时,应用程序崩溃。 只有当播放完成时才会发生(按'完成'或让它播放直到结束)。当我在播放过程中按Home键并再次打开应用程序时,一切都很好。

这里我对movieplayer代码:(从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]; 
} 

我使用的iOS5和ARC。

与代码的类是正常的UIViewController并且也是该方法的调用者。

它只发生在设备上。没有崩溃的模拟器,但在我的iPhone 4和iPad 1

我找不到为什么我的应用程序崩溃的原因。

Stacktrace from the crash

编辑: 我不太清楚知道对不对,但我想我解决了我的崩溃。

我相信这个问题是不是打的AudioFile,或既。录音似乎也是这个问题的一部分。

在我的AVAudioRecorder录音类中,我将这个类注册为AVAudioSession的代理: [[AVAudioSession sharedInstance] setDelegate:self];我把它移到我的“根”类(我的“主”类,从我开始的一切),当我实现协议方法'beginInterruption'和'endInterruption'(只知道一个NSLog语句),我可以当我在后台设置应用程序时会调用'beginInterruption',当我再次启动应用程序时(前台),将会调用'endInterruption'。 在堆栈跟踪中,您可以看到类似“AudioSessionInterruptionListener”的内容...似乎是委托方法的一部分。

编辑2

我测试我的应用程序几次了,这次暴跌不会再发生。将AVAudioSessionDelegate设置为我的“根”类似乎是答案。

[[AVAudioSession sharedInstance] setDelegate: self]; 

我在“父”类我audiorecord处理类的补充这一点:

回答

0

对于我注册AVAudioSession代表这是必要的。
此外,我不得不实施beginInterruption和AVAudioSession委托endInterruption

在此之后,崩溃再也没有发生过。