通过AVAudioRecorder在WatchOS上录制音频

时间:2019-06-01 13:50:27

标签: swift avfoundation watchkit watch-os

是否有任何关于此的示例代码或教程?我发现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以及如何处理?谢谢您的帮助!

1 个答案:

答案 0 :(得分:0)

此作品有效:

AVAudioEngine

别忘了将 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中。