gpt4 book ai didi

swift - 观看操作系统 : Microphone & Audio Processing

转载 作者:行者123 更新时间:2023-11-28 05:39:53 25 4
gpt4 key购买 nike

是否可以访问 watch 麦克风并处理实时音频流以获得特定信息(分贝级别和当前频率)?我知道可以使用 swift presentAudioRecorderController 在 watch 上录制音频。但是,我不想展示它附带的默认语音备忘录 UI。即使我确实使用了它,它也无法访问实时流来玩。相反,我想在后台持续、安静地聆听。

最佳答案

您当然可以仅使用 watch 进行录制。请记住将相应的 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)
})
}

关于swift - 观看操作系统 : Microphone & Audio Processing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57187320/

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