【问题标题】:Crash with MPMoviePlayerViewController after switching app between background and foreground在后台和前台之间切换应用程序后与 MPMoviePlayerViewController 崩溃
【发布时间】:2012-05-15 13:16:34
【问题描述】:

我的情况: 我正在使用 AVAudioRecorder 录制音频文件。 要播放我正在使用 MPMoviePlayerViewController 的记录。 正在录制、播放、停止等。

我的问题: 播放后:当我按下主页按钮(将我的应用程序置于后台)并再次打开我的应用程序时,应用程序崩溃。 它仅在播放完成时发生(按“完成”或让它播放到最后)。当我在播放过程中按下主页按钮并再次打开应用程序时,一切都很好。

这是我的电影播放器​​代码:(使用来自 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 上。

我找不到我的应用崩溃的原因。

编辑: 我不太确定是否知道,但我想我解决了我的崩溃问题。

我相信问题不在于播放音频文件,或者不仅仅是。录音似乎也是这个问题的一部分。

在我使用 AVAudioRecorder 录制的课程中,我将此类注册为 AVAudioSession 的代表: [[AVAudioSession sharedInstance] setDelegate: self];

我把它移到我的“根”类(我的“主”类,我从那里开始一切),当我实现协议方法“beginInterruption”和“endInterruption”(只知道一个NSLog语句)我可以看到当我在后台设置应用程序时调用“beginInterruption”,当我再次启动应用程序(前台)时调用“endInterruption”。 在堆栈跟踪中,您可以看到类似“AudioSessionInterruptionListener”的内容...似乎是委托方法的一部分。

编辑 2

我测试了我的应用几次,崩溃再也没有发生。将 AVAudioSessionDelegate 设置为我的“根”类似乎是答案。

【问题讨论】:

    标签: ios5 mpmovieplayercontroller


    【解决方案1】:

    对我来说,有必要注册AVAudioSession 代表:

    [[AVAudioSession sharedInstance] setDelegate: self];
    

    我在我的音频记录处理类的“父”类中添加了这个。
    我还必须实现 AVAudioSession 委托的 beginInterruptionendInterruption

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

    【讨论】:

      猜你喜欢
      • 2013-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 2011-04-20
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      相关资源
      最近更新 更多