ios – Swift: Acknowledge speech from microphone whereas audio performs within the background

[ad_1]

I would like my app to acknowledge speech from the microphone and permit audio within the background to maintain taking part in.

My app acknowledges speech coming in by means of the microphone and converts it to textual content. When my app launches it shuts down any audio taking part in within the background.

Is it attainable to let the background audio proceed to play whereas my app listens for speech utilizing the microphone?

Stripped down code:

import UIKit
import Speech
class ViewController: UIViewController {
public personal(set) var isRecording = false
personal var audioEngine: AVAudioEngine!
personal var inputNode: AVAudioInputNode!
personal var audioSession: AVAudioSession!
personal var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?

override func viewDidLoad() {
    tremendous.viewDidLoad()
}

override public func viewDidAppear(_ animated: Bool) {
    checkPermissions()
    startRecording()
    isRecording.toggle()
}

personal func startRecording() {

    guard let recognizer = SFSpeechRecognizer(), recognizer.isAvailable else {
        handleError(withMessage: "Speech recognizer not accessible.")
        return
    }
    recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
    recognitionRequest!.shouldReportPartialResults = true
    recognizer.recognitionTask(with: recognitionRequest!) { (consequence, error) in
        guard error == nil else { self.handleError(withMessage: error!.localizedDescription); return }
        guard let consequence = consequence else { return }
        print(consequence.bestTranscription.segments)
    }
    audioEngine = AVAudioEngine()
    inputNode = audioEngine.inputNode
    let recordingFormat = inputNode.outputFormat(forBus: 0)
    inputNode.installTap(onBus: 0, bufferSize: 1024, format: recordingFormat) { (buffer, _) in
        self.recognitionRequest?.append(buffer)
    }
    audioEngine.put together()

    do {
        audioSession = AVAudioSession.sharedInstance()
        attempt audioSession.setCategory(.file, mode: .spokenAudio, choices: .duckOthers)
        attempt audioSession.setActive(true, choices: .notifyOthersOnDeactivation)
        attempt audioEngine.begin()
    } catch {
        handleError(withMessage: error.localizedDescription)
    }
}
personal func checkPermissions() {
    SFSpeechRecognizer.requestAuthorization { authStatus in
        DispatchQueue.fundamental.async {
            swap authStatus {
            case .licensed: break
            default: self.handlePermissionFailed()
            }
        }
    }
}

personal func handlePermissionFailed() {
    // Current an alert asking the consumer to alter their settings.
    let ac = UIAlertController(title: "This app should have entry to speech recognition to work.",
                               message: "Please take into account updating your settings.",
                               preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "Open settings", type: .default) { _ in
        let url = URL(string: UIApplication.openSettingsURLString)!
        UIApplication.shared.open(url)
    })
    ac.addAction(UIAlertAction(title: "Shut", type: .cancel))
    current(ac, animated: true)
}
personal func handleError(withMessage message: String) {
    // Current an alert.
    let ac = UIAlertController(title: "An error occured", message: message, preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "OK", type: .default))
    current(ac, animated: true)
}

}

While you run my app and there’s audio working within the background my app pauses the audio. I attempted exiting my app and restarting the audio however once I return to my app it as soon as once more pauses the background audio. I would really like the audio to maintain taking part in whereas my app is utilizing the microphone to hear.

I attempted eradicating “choices: .duckOthers” however it made no distinction.

I imagine what I wish to do is feasible. Shazam, as an example, can play a music on the speaker and concurrently use the microphone to take heed to it and establish it.

[ad_2]

Leave a Reply