ios - AVAudioSession 在拒绝电话调用时中断

标签 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")
    }
}

最佳答案

因此,我设法在使用 CallKit 和 CXCallObserver 委托(delegate)函数直接从 AVAudioSession 暂停录音之前暂停录音/p>

关于ios - AVAudioSession 在拒绝电话调用时中断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48749729/

相关文章:

ios - swift : cannot subscript a value of type [Dictionary<String, AnyObject>] 具有字符串类型的索引

ios - 突出显示事件的分段控件 + 在事件段周围添加边框

iphone - 应用的专辑封面 - iPhone

ios - swift - 如何强制应用程序在从后台返回后显示特定 View

ios - 更改导航栏色调颜色 iOS 7。

ios - 在 VCard Swift 中导出图片

swift - 如何检查 UITextField 在 Swift 2.0 中不为空?有没有更好的方法来计算字符串?

objective-c - 带有动态字符串的 UIImage 错误

ios - iPhone 6 启动图像/启动文件问题

ios - setValue:forKey总是 swift 崩溃