gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:54:13 25 4
gpt4 key购买 nike

我知道关于 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com