gpt4 book ai didi

swift - 通过 AVAudioRecorder 在 WatchOS 上录制音频

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

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

最佳答案

这个有效:

    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 添加到您的手机 配套应用 Info.plist 中。

关于swift - 通过 AVAudioRecorder 在 WatchOS 上录制音频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56407339/

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