swift - 手表操作系统:麦克风和音频处理
问题描述
是否可以访问手表麦克风并处理实时音频流以获得特定信息(分贝级别和当前频率)?我知道可以使用 swift presentAudioRecorderController 在手表上录制音频。但是,我不想展示它附带的默认语音备忘录 UI。即使我确实使用它,它也无法访问直播流。相反,我想在后台连续安静地听。
解决方案
您当然可以只使用手表进行录制。只要记住将相应的 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)
})
}
推荐阅读
- c# - 无法在 C# 中将类型 void 隐式转换为 int
- c# - 将单个 KeyValuePair<> 序列化为 JSON 作为父对象的一部分
- css - 如何在 CSS 中设置自定义 HR 元素的样式?
- python - Django 没有识别出我的模型中有对象
- java - 在Java中没有Scanner.class的k-shingles中的分隔文本
- vert.x - 如何从 VertX Future 对象中获取值
- c++ - 允许调用者在我的 Builder 类中使用任何指针类型(包括智能指针)?
- sql - PL/SQL 和 SQL Developer 的结果不同
- swift - PushViewController 显示黑屏
- angular - 如何在 Ionic 5 中获得 canGoBack 功能?