有关于这方面的任何示例代码或教程吗?我发现AVAudioRecorder支持自WatchOS 4.0 https://developer.apple.com/documentation/avfoundation/avaudiorecorder以来。但当我试图使用它-它记录了1秒,没有实际的声音(只是噪音)。
这是我的代码:
let audioURL = self.getRecordedFileURL()
print(audioURL.absoluteString)
let settings = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
do {
recorder = try AVAudioRecorder(url: audioURL, settings: settings)
recorder?.delegate = self
recorder?.record()
} catch {
finishRecording(success: false)
}另外,我应该在这里使用AudioSession吗?如果是,是否需要requestRecordPermission,以及如何处理?谢谢你的帮助!
发布于 2019-06-01 16:14:08
这个方法很有效:
let recordingName = "audio.m4a"
let dirPath = getDirectory()
let pathArray = [dirPath, recordingName]
guard let filePath = URL(string: pathArray.joined(separator: "/")) else { return }
let settings = [AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey:12000,
AVNumberOfChannelsKey:1,
AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
]
//start recording
do {
audioRecorder = try AVAudioRecorder(url: filePath, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()
} catch {
print("Recording Failed")
}
func getDirectory()-> String {
let dirPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
return dirPath
}不要忘记将NSMicrophoneUsageDescription添加到您的phone配套应用程序Info.plist中。
https://stackoverflow.com/questions/56407339
复制相似问题