Ios 拒绝接听电话时音频中断
我知道关于音频会话也有类似的问题,但我的有点不同。 我有一个音频中断的问题 在被拒绝的呼叫之后,它会继续,但它只保存中断后录制的部分-呼叫前的部分只是消失 对于有人接听的电话,即使是1秒,它也可以正常工作——它可以恢复并保存整个录音 我注意到的区别是,当我接听电话时,.start和.end都会被呼叫两次,但当我拒绝一个电话时,它只会每次呼叫一次 开始录制:Ios 拒绝接听电话时音频中断,ios,swift,xcode,avaudiosession,Ios,Swift,Xcode,Avaudiosession,我知道关于音频会话也有类似的问题,但我的有点不同。 我有一个音频中断的问题 在被拒绝的呼叫之后,它会继续,但它只保存中断后录制的部分-呼叫前的部分只是消失 对于有人接听的电话,即使是1秒,它也可以正常工作——它可以恢复并保存整个录音 我注意到的区别是,当我接听电话时,.start和.end都会被呼叫两次,但当我拒绝一个电话时,它只会每次呼叫一次 开始录制: func startRecording(name:String, quality:AudioQuality, progress:@escap
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委托函数从AVAudioSession暂停录制。AVAudioRecorder应处理中断暂停并自动恢复,因此,如果删除
handleInterruption
实现,可能会有所帮助。在任何情况下,您都不应在中断时停用;这已经发生了。@matt它会自动暂停,但不会继续。我删除了audioRecorder.pause(),但一旦我恢复,问题仍然会出现OK,但在您的handleInterruption
中,我也看不到您正在恢复。@matt我正在从audioRecorder.resume()按钮恢复,所以基本上在通话后,我的应用程序处于“暂停”状态。我在“applicationWillResignActive”通知上暂停录制,该通知会在中断前调用,从而使其正常工作。