首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Watch OS :麦克风和音频处理

Watch OS :麦克风和音频处理
EN

Stack Overflow用户
提问于 2019-07-25 00:17:30
回答 1查看 401关注 0票数 1

是否可以访问手表麦克风并处理现场音频流,以获得特定信息(分贝水平和当前频率)?我知道使用swift presentAudioRecorderController可以在手表上录制音频。但是,我不想展示它附带的默认语音备忘录UI。即使我使用它,它也不能提供对实时流的访问。取而代之的是,我想在后台持续默默地听。

EN

回答 1

Stack Overflow用户

发布于 2019-07-26 16:43:46

你当然可以只使用手表来记录。只需记住将相应的NSMicrophoneUsageDescription添加到plist中即可。

下面是一些可以帮助您入门的代码。然后,您可以从audioRecorder获得平均功率,并将其用于在UI上制作一些魔术。

代码语言:javascript
复制
    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)
        })
    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57187320

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档