AVAudioSession中断电话中断

时间:2018-02-12 15:21:49

标签: ios swift xcode avaudiosession

我知道有关于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")
    }
}

1 个答案:

答案 0 :(得分:0)

所以我设法暂停录制,然后直接从AVAudioSession暂停,使用CallKit和CXCallObserver委托函数func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall),这是在来电时调用的。