简体   繁体   English

应用程序在后台运行时如何开始录音?

[英]How to start audio recording when app running on background?

I am working on audio recording.我正在从事录音工作。 when application running on foreground i have to start audio recording and going to background at that time audio recording working fine.当应用程序在前台运行时,我必须开始录音并转到后台,那时录音工作正常。

But my question is that how to start audio recording when i am already in background, My audio recording function fired like this:但我的问题是,当我已经在后台时如何开始录音,我的录音 function 是这样触发的:

I have a Bluetooth LE device with buttons and an iOS app.我有一个带按钮的蓝牙 LE 设备和一个 iOS 应用程序。 Those two are paired (Bluetooth LE device and the iPhone which runs the iOS app) and the iOS app is listening for events on the Bluetooth LE device, events like a hit of a button.这两个是配对的(蓝牙 LE 设备和运行 iOS 应用程序的 iPhone)并且 iOS 应用程序正在监听蓝牙 LE 设备上的事件,例如按下按钮的事件。

Now, when the user hits a button on the Bluetooth LE device, the iOS app captures the event and I am able to run code even if the app is in background, but I am not able to start a voice recording.现在,当用户点击 Bluetooth LE 设备上的按钮时,iOS 应用程序会捕获该事件,即使该应用程序处于后台,我也可以运行代码,但我无法开始录音。

I have already enable Background Modes :我已经启用了Background Modes

在此处输入图像描述

Here is my Code for Audio Recording:这是我的录音代码:

func startRecording() {
        
        DispatchQueue.global(qos: .background).asyncAfter(deadline: DispatchTime.now(), qos: .background) {
            let audioFilename = self.getDocumentsDirectory().appendingPathComponent("recording.m4a")
            print("record Audio \(audioFilename)")
            let settings = [
                AVFormatIDKey: Int(kAudioFormatMPEG4AAC),
                AVSampleRateKey: 12000,
                AVNumberOfChannelsKey: 1,
                AVEncoderAudioQualityKey: AVAudioQuality.high.rawValue
            ]

            do {
                self.audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
                self.audioRecorder.delegate = self
                self.audioRecorder.record()
            } catch {
                self.finishRecording(success: false)
            }
        }
    }

I can't find proper solution to do that thing, Please suggest me proper way to do that, Thanks in Advance.我找不到合适的解决方案来做那件事,请建议我这样做的正确方法,在此先感谢。

This is not possible to do without an explicit UI interaction for security reasons, or else it would be possible to "spy" on a person (once paired, a person outside of a room could start recording and listen to what happens inside the room).出于安全原因,如果没有明确的 UI 交互,这是不可能的,否则就有可能“监视”一个人(一旦配对,房间外的人就可以开始录制并收听房间内发生的事情) .

Workarounds could be:解决方法可能是:

  • send a local notification eg "The device is ready to record, want to start?".发送本地通知,例如“设备已准备好录制,要开始吗?”。 Once you tap this, the app opens in foreground, and should be able to start the recording.点击此按钮后,应用程序将在前台打开,并且应该能够开始录制。
  • use CallKit (suitable for phone-like use cases).使用 CallKit(适用于类似电话的用例)。 When the user presses "Accept" in the CallKit system UI, it is possible to start recording audio even in background.当用户在 CallKit 系统 UI 中按下“Accept”时,即使在后台也可以开始录制音频。

Here are my solutions:这是我的解决方案:

  1. Trigger a CallKit call from you app and control your headphone to answer the call.从您的应用程序触发 CallKit 呼叫并控制您的耳机接听电话。

  2. Use your headphone to record and transfer voice data with bluetooth channel.使用耳机通过蓝牙通道记录和传输语音数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM