【问题标题】:AVAudioSession Interruption on declining phone call拒绝电话呼叫时的 AVAudioSession 中断
【发布时间】:2018-02-12 15:21:49
【问题描述】:

我知道关于 AVAudioSession 有类似的问题,但我的有点不同。 我遇到了 AVAudioSession 中断的问题。

电话被拒后,它会恢复,但它只保存中断后录制的部分 - 通话之前的部分会消失。

对于已接听的电话,即使 1 秒钟也可以正常工作 - 它会恢复并保存整个录音。

我注意到不同之处在于,当我接听电话时,.began 和 .ended 都被调用了两次,但当我拒绝来电时,它每次只调用一次。

开始录制:

func startRecording(name:String, quality:AudioQuality, progress:@escaping RecorderProgress, completed:@escaping RecorderCompletion, microphoneProgress:@escaping MicrophoneProgress) {
    log("Recorder - About to start recording")
    if isAudioRecordingGranted {
        self.device = EZMicrophone(microphoneDelegate: self, startsImmediately: true)
        self.name = name
        //Create the session.
        self.recorderProgress = progress
        self.recorderCompletion = completed
        self.microphoneProgress = microphoneProgress
        let session = AVAudioSession.sharedInstance()
        NotificationCenter.default.addObserver(self, selector: #selector(handleInterruption(notification:)), name: NSNotification.Name.AVAudioSessionInterruption, object: session)
        do {
            try session.setCategory(AVAudioSessionCategoryPlayAndRecord, with: .defaultToSpeaker)
            try session.setActive(true)
            let settings = [
                AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                AVSampleRateKey: 44100,
                AVNumberOfChannelsKey: 2,
                AVEncoderAudioQualityKey: audioQuality.rawValue
            ]
            let audioFilename = Directory.documents.appendingPathComponent("\(self.name).m4a")
            audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
            audioRecorder.delegate = self
            audioRecorder.isMeteringEnabled = true
            audioRecorder.record()
            isRecording = true
            log("Recorder - Started recording, file at path: \(audioFilename.absoluteString)")
            meterTimer = Timer.scheduledTimer(timeInterval: 0.1, target:self, selector:#selector(update), userInfo:nil, repeats:true)
        }
        catch let error {
            completed(error, 0, 0)
            log("Recorder - Error occured while starting audio recording: \(error.localizedDescription)")
        }
    }
}

通知处理程序:

@objc func handleInterruption(notification: Notification) {
    guard let userInfo = notification.userInfo,
        let interruptionTypeRawValue = userInfo[AVAudioSessionInterruptionTypeKey] as? UInt,
        let interruptionType = AVAudioSessionInterruptionType(rawValue: interruptionTypeRawValue),
        let session = notification.object as? AVAudioSession else {
            log("Recorder - something went wrong")
            return
    }
    switch interruptionType {
    case .began:
        log("Recorder - interruption began")
        try! session.setActive(false)
        audioRecorder.pause()
    case .ended:
        try! session.setActive(true)
        log("Recorder - interruption ended")
    }
}

【问题讨论】:

  • AVAudioRecorder 应该自动处理中断暂停和恢复,所以如果你删除你的 handleInterruption 实现可能会有所帮助。在任何情况下,您都不应该在中断时停用;这已经发生了。
  • @matt 它会自动暂停,但不会恢复。我删除了 audioRecorder.pause(),但是一旦我恢复问题仍然存在
  • 好的,但是在你的 handleInterruption 中我也没有看到 恢复。
  • @matt 我正在通过 audioRecorder.resume() 从按钮恢复,所以基本上在通话后,我的应用程序处于“暂停”状态。
  • 我设法通过暂停记录“applicationWillResignActive”通知来使其工作,该通知在中断前调用。

标签: ios swift xcode avaudiosession


【解决方案1】:

因此,我设法在使用 CallKit 和 CXCallObserver 委托函数 func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) 直接从 AVAudioSession 暂停录音之前暂停录音。

【讨论】:

    猜你喜欢
    • 2012-06-30
    • 1970-01-01
    • 2013-02-01
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    相关资源
    最近更新 更多