【发布时间】:2019-12-02 20:49:00
【问题描述】:
是否可以访问手表麦克风并处理实时音频流以获得特定信息(分贝级别和当前频率)? 我知道可以使用 swift presentAudioRecorderController 在手表上录制音频。但是,我不想展示它附带的默认语音备忘录 UI。即使我确实使用了它,它也无法访问直播流。相反,我想在后台持续安静地聆听。
【问题讨论】:
标签: swift xcode audio watchkit microphone
是否可以访问手表麦克风并处理实时音频流以获得特定信息(分贝级别和当前频率)? 我知道可以使用 swift presentAudioRecorderController 在手表上录制音频。但是,我不想展示它附带的默认语音备忘录 UI。即使我确实使用了它,它也无法访问直播流。相反,我想在后台持续安静地聆听。
【问题讨论】:
标签: swift xcode audio watchkit microphone
您当然可以只使用手表进行录制。只要记住将对应的 NSMicrophoneUsageDescription 添加到 plist 中即可。
下面是一些可以帮助您入门的代码。然后,您可以从 audioRecorder 获取平均功率,并将其用于在 UI 上创造一些魔力。
func startRecording () {
if isRecording {
recordButton.setBackgroundImage(UIImage(named: "stopImage"))
let fileGuid = "\(UUID().uuidString)s.m4a"
let audioFilename = getDocumentsDirectory().appendingPathComponent(fileGuid)
fileOutputPath = audioFilename
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(category, mode: .spokenAudio)
} catch let sessionError {}
let settings: [String: Int] = [
AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
AVSampleRateKey: 48000,
AVEncoderBitRateKey: 256000,
AVNumberOfChannelsKey: 2,
AVEncoderAudioQualityKey: .max
]
do {
audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder?.prepareToRecord()
audioRecorder?.isMeteringEnabled = true
audioRecorder?.record()
} catch {}
} else {
recordButton.setBackgroundImage(UIImage(named: "recordImage"))
do {
try AVAudioSession.sharedInstance().setActive(false, options: .notifyOthersOnDeactivation)
} catch let sessionError {
print(sessionError)
}
audioRecorder?.stop()
send()
}
}
func send() {
// waking app if its not running in the background
session.sendMessage(["wake": "up"], replyHandler: { _ in
self.session.transferFile(self.fileOutputPath, metadata: nil)
}, errorHandler: { _ in
// waking up device failed. Still, we should try putting the file into the queue so it eventually gets synced to the device
self.session.transferFile(self.fileOutputPath, metadata: nil)
})
}
【讨论】: