在来电中恢复AVAudioRecorder的解决方法如下:
import AVFoundation
import CoreTelephony
class ViewController: UIViewController {
var audioRecorder: AVAudioRecorder?
override func viewDidLoad() {
super.viewDidLoad()
// 监听电话状态变化
NotificationCenter.default.addObserver(self, selector: #selector(handleCallStateChange), name: NSNotification.Name.CTCallStateDidChange, object: nil)
// 设置录音会话
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(.playAndRecord, mode: .default, options: [])
try session.setActive(true)
} catch {
print("Failed to set audio session: \(error.localizedDescription)")
}
// 初始化AVAudioRecorder
let audioFilename = getDocumentsDirectory().appendingPathComponent("recording.wav")
let settings = [
AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder?.prepareToRecord()
} catch {
print("Failed to initialize audio recorder: \(error.localizedDescription)")
}
}
@objc func handleCallStateChange(notification: NSNotification) {
guard let call = CTCallCenter().currentCalls.first else { return }
if call.callState == CTCallStateConnected {
// 来电接通,暂停录音
audioRecorder?.pause()
} else if call.callState == CTCallStateDisconnected {
// 来电挂断,恢复录音
audioRecorder?.record()
}
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
}
上述代码中,我们在viewDidLoad
方法中设置了录音会话,并初始化了AVAudioRecorder
对象。在handleCallStateChange
方法中,我们根据来电状态的变化来暂停和恢复录音。当电话接通时,我们调用pause
方法暂停录音;当电话挂断时,我们调用record
方法恢复录音。
请注意,为了监听来电状态变化,需要在项目中导入CoreTelephony.framework,并添加对CoreTelephony的引用。
onReceive
修饰符来监听电话状态变化。import AVFoundation
import CoreTelephony
import SwiftUI
class RecorderViewModel: ObservableObject {
var audioRecorder: AVAudioRecorder?
init() {
// 监听电话状态变化
NotificationCenter.default.addObserver(self, selector: #selector(handleCallStateChange), name: NSNotification.Name.CTCallStateDidChange, object: nil)
// 设置录音会话
let session = AVAudioSession.sharedInstance()
do {
try session.setCategory(.playAndRecord, mode: .default, options: [])
try session.setActive(true)
} catch {
print("Failed to set audio session: \(error.localizedDescription)")
}
// 初始化AVAudioRecorder
let audioFilename = getDocumentsDirectory().appendingPathComponent("recording.wav")
let settings = [
AVFormatIDKey: Int(kAudioFormatLinearPCM),
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder?.prepareToRecord()
} catch {
print("Failed to initialize audio recorder: \(error.localizedDescription)")
}
}
@objc func handleCallStateChange(notification: NSNotification) {
guard let call = CTCallCenter().currentCalls.first else { return }
if call.callState == CTCallStateConnected {
// 来电接通,暂停录音
audioRecorder?.pause()
} else if call.callState