【发布时间】:2016-04-17 14:38:27
【问题描述】:
应用程序录制音频,并在出现中断(例如电话呼叫)时停止录制音频,然后在电话结束时恢复录制音频。
应用程序当前在有电话时注册,但是当我挂断电话时,应用程序没有注册中断已经结束。 (我没有打开其他应用程序)。
使用函数委托
在我的 ViewController 中查看下面的代码。
func audioRecorderBeginInterruption(recorder: AVAudioRecorder){
print("* * * * * * * inside begin interruption")}
func audioRecorderEndInterruption(recorder: AVAudioRecorder, withFlags flags: Int) {
// THIS NEVER PRINTS, EVEN WHEN PHONE IS HUNG UP
print("* * * * * * * inside end interruption")
}
有通知
我也尝试通过通知处理中断,但 .Ended 仍未处理,除非我接到电话并拒绝接听电话。查看我的 AppDelegate 中的代码
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
let center = NSNotificationCenter.defaultCenter()
center.addObserver(self,
selector:"sessionInterrupted:",
name:AVAudioSessionInterruptionNotification,
object:AVAudioSession.sharedInstance()) //self.myViewController.audioSession)
return true
}
func sessionInterrupted(notification: NSNotification){
if let typeValue = notification.userInfo?[AVAudioSessionInterruptionTypeKey] as? NSNumber{
if let type = AVAudioSessionInterruptionType(rawValue: typeValue.unsignedLongValue){
if type == .Began{
print("interruption: began")
} else{
// THIS ALSO NEVER PRINTS
print("interruption: ended")
}
}
对我不起作用的相关解决方案
- 解决方案:添加无效的
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]; - 解决方案:使用
MixWithOthers,它可以用于恢复播放音频,但在我的情况下不适用于恢复录制音频
当前假设
我目前的假设是,“结束”中断仅适用于诸如当您接到电话并且您拒绝接听电话时的中断,而不适用于您实际上拿起电话,聊了一会儿,然后挂断。我的猜测是,如果不使用越狱手机,就无法检测到后一种情况。
在这里以更广泛的方式展开:iOS AVAudioSession interruption notification not working as expected
这个假设似乎在DropVox's FAQ得到验证:
一次只有一个应用可以控制音频。如果 DropVox 正在录制 另一个应用程序控制,这就是所谓的“中断”。什么时候 中断结束,DropVox 只能恢复 记录如果它在前台7,这就是为什么我们告诫不要 使用“在后台录制”设置。
我可以通过使用routing 来检测是否正在使用麦克风来处理中断。但我不认为我可以在后台重新激活录音,所以一旦应用程序回到前台,我就会这样做。
是吗?
【问题讨论】:
-
你找到解决办法了吗?
-
Apple 文档中确实有一行 (developer.apple.com/library/content/documentation/Audio/…) 说:“不能保证开始中断会导致结束中断。您的应用需要注意切换到前台运行状态或用户按下播放按钮。无论哪种情况,请确定您的应用是否应重新激活其音频会话。"
标签: ios swift avaudiorecorder avaudiosession interruptions