skip to Main Content

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:

I have a Bluetooth LE device with buttons and an iOS app. 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.

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.

I have already enable Background Modes:

enter image description here

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.

2

Answers


  1. 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).

    Workarounds could be:

    • send a local notification e.g. "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). When the user presses "Accept" in the CallKit system UI, it is possible to start recording audio even in background.
    Login or Signup to reply.
  2. Here are my solutions:

    1. Trigger a CallKit call from you app and control your headphone to answer the call.

    2. Use your headphone to record and transfer voice data with bluetooth channel.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search