是否可以访问手表麦克风并处理现场音频流,以获得特定信息(分贝水平和当前频率)?我知道使用swift presentAudioRecorderController可以在手表上录制音频。但是,我不想展示它附带的默认语音备忘录UI。即使我使用它,它也不能提供对实时流的访问。取而代之的是,我想在后台持续默默地听。
发布于 2019-07-26 16:43:46
你当然可以只使用手表来记录。只需记住将相应的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)
})
}https://stackoverflow.com/questions/57187320
复制相似问题