首页 > 解决方案 > 手表操作系统:麦克风和音频处理

问题描述

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

标签: swiftxcodeaudiowatchkitmicrophone

解决方案


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

推荐阅读