电话无法恢复后恢复AVAudioPlayer

Iva*_*tov 5 iphone avaudioplayer ios swift ios9

SO上已经有一些看似相似的问题,但经过几个小时的搜索和我的代码试验,我一直无法找到明确的答案.我能找到的最接近的是这个答案,这个答案暗示了4年前的"已知错误",但没有详细说明如何解决它.

我有一个audioPlayer类,正在播放AVAudioPlayer的音频文件并监听AVAudioSessionDelegate方法beginInterruptionendInterruption.我知道这endInterruption不保证所以我存储了一个布尔值beginInterruption并处理重新启动音频播放applicationDidBecomeActive.

如果手机接到电话并且用户拒绝或让它转到语音邮件,这一切都可以正常运行.一旦我的应用程序返回活动状态,音频播放就会恢复.

在这里,我得到了奇怪的行为:如果用户接听电话,一旦他们挂机,一切似乎按预期工作,但通过耳机或扬声器没有声音.

我可以通过每秒打印音量和当前时间来验证音频是否在技术上播放,但声音没有出来.

如果我等待20-40秒,音频突然切入并变得可听见,好像它已经在后台默默播放.

经过更多的调试,我注意到这些20-40秒的静音AVAudioSession.sharedInstance().secondaryAudioShouldBeSilencedHint仍然存在true,然后才突然改变false并让音频播放.

我订阅了AVAudioSessionSilenceSecondaryAudioHintNotification以查看是否可以检测到此更改,但它永远不会被调用,即使.secondaryAudioShouldBeSilencedHint从更改truefalse.

我甚至尝试在恢复播放音频的方法中明确设置AVAudioSession.sharedInstance().setActive(true),但行为没有改变.

最后,我尝试设置一个计时器,将恢复延迟10秒后applicationDidBecomeActive,但行为没有改变.

那么,为什么电话似乎没有放弃对我的应用程序的音频会话的控制?

谢谢参观!


码:

AVAudioSession设置init()如下:

NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.handleAudioHintChange), name: AVAudioSessionSilenceSecondaryAudioHintNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(self.handleAudioInterruption), name: AVAudioSessionInterruptionNotification, object: nil)

do {
  try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, withOptions: AVAudioSessionCategoryOptions.MixWithOthers)
  print("AVAudioSession Category Playback OK")
  do {
    try AVAudioSession.sharedInstance().setActive(true, withOptions: .NotifyOthersOnDeactivation)
    print("AVAudioSession is Active")
  } catch let error as NSError {
    print(error.localizedDescription)
  }
} catch let error as NSError {
  print(error.localizedDescription)
}
Run Code Online (Sandbox Code Playgroud)

通知处理程序:

///////////////////////////////////
// This never gets called :( //////

func handleAudioHintChange(notification: NSNotification) {
  print("audio hint changed")
}
///////////////////////////////////

func handleAudioInterruption(notification: NSNotification) {
  if notification.name != AVAudioSessionInterruptionNotification || notification.userInfo == nil{
    return
  }

  if let typeKey = notification.userInfo [AVAudioSessionInterruptionTypeKey] as? UInt,
  let type = AVAudioSessionInterruptionType(rawValue: typeKey) {
    switch type {
      case .Began:
        print("Audio Interruption Began")
        NSUserDefaults.standardUserDefaults().setBool(true, forKey:"wasInterrupted")

      case .Ended:
        print("Audio Interuption Ended")
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

应用代表:

func applicationDidBecomeActive(application: UIApplication) {
  if(NSUserDefaults.standardUserDefaults().boolForKey("wasInterrupted")) {
    audioPlayer.resumeAudioFromInterruption()
  }
}
Run Code Online (Sandbox Code Playgroud)

重启功能:

// This works great if the phone call is declined
func resumeAudioFromInterruption() {
  NSUserDefaults.standardUserDefaults().removeObjectForKey("wasInterrupted")
  do {
    try AVAudioSession.sharedInstance().setActive(true)
    print("AVAudioSession is Active")
  } catch let error as NSError {
    print(error.localizedDescription)
  }
  thisFunctionPlaysMyAudio()
Run Code Online (Sandbox Code Playgroud)

}

L A*_*L A 5

我尝试做同样的事情,虽然我没有在setActive方法中使用任何选项.

iOS 9.3.1中似乎存在一个错误,该错误在电话呼叫结束后无法恢复AVAudioPlayer的播放.

以下是为我解决的问题片段:(抱歉,Objective-C)

- (void)handleInterruption:(NSNotification *) notification{
    if (notification.name != AVAudioSessionInterruptionNotification || notification.userInfo == nil) {
        return;
    }

    NSDictionary *info = notification.userInfo;

    if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {

        if ([[info valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
            NSLog(@"InterruptionTypeBegan");
        } else {
            NSLog(@"InterruptionTypeEnded");

            //*The* Workaround - Add a small delay to the avplayer's play call; Without the delay, the playback will *not* be resumed
            //
            //(I didn't play much with the times, but 0.01 works with my iPhone 6S 9.3.1)
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                NSLog(@"playing");
                [_player play];
            });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在播放器的播放通话中添加了一个小延迟,并且它有效.

你可以在这里找到我完整的Demo项目:https: //github.com/liorazi/AVAudioSessionWorkaround

我向Apple提交了一个雷达,希望它能在下一个版本中得到修复.