首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WatchOS上通过AVAudioRecorder录制音频

问在WatchOS上通过AVAudioRecorder录制音频
EN

Stack Overflow用户
提问于 2019-06-01 13:50:27
回答 1查看 1.3K关注 0票数 2

有关于这方面的任何示例代码或教程吗?我发现AVAudioRecorder支持自WatchOS 4.0 https://developer.apple.com/documentation/avfoundation/avaudiorecorder以来。但当我试图使用它-它记录了1秒,没有实际的声音(只是噪音)。

这是我的代码:

代码语言:javascript
复制
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,以及如何处理?谢谢你的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-01 16:14:08

这个方法很有效:

代码语言:javascript
复制
    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中。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56407339

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档