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

[ad_1]

I need my app to acknowledge speech from the microphone and permit audio within the background to maintain enjoying.

My app acknowledges speech coming in via the microphone and converts it to textual content. When my app launches it shuts down any audio enjoying within the background.

Is it doable 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 obtainable.")
        return
    }
    recognitionRequest = SFSpeechAudioBufferRecognitionRequest()
    recognitionRequest!.shouldReportPartialResults = true
    recognizer.recognitionTask(with: recognitionRequest!) { (end result, error) in
        guard error == nil else { self.handleError(withMessage: error!.localizedDescription); return }
        guard let end result = end result else { return }
        print(end result.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()
        strive audioSession.setCategory(.document, mode: .spokenAudio, choices: .duckOthers)
        strive audioSession.setActive(true, choices: .notifyOthersOnDeactivation)
        strive audioEngine.begin()
    } catch {
        handleError(withMessage: error.localizedDescription)
    }
}
personal func checkPermissions() {
    SFSpeechRecognizer.requestAuthorization { authStatus in
        DispatchQueue.major.async {
            change authStatus {
            case .licensed: break
            default: self.handlePermissionFailed()
            }
        }
    }
}

personal func handlePermissionFailed() {
    // Current an alert asking the consumer to vary their settings.
    let ac = UIAlertController(title: "This app should have entry to speech recognition to work.",
                               message: "Please think about updating your settings.",
                               preferredStyle: .alert)
    ac.addAction(UIAlertAction(title: "Open settings", model: .default) { _ in
        let url = URL(string: UIApplication.openSettingsURLString)!
        UIApplication.shared.open(url)
    })
    ac.addAction(UIAlertAction(title: "Shut", model: .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", model: .default))
    current(ac, animated: true)
}

}

While you run my app and there may be 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 love the audio to maintain enjoying whereas my app is utilizing the microphone to hear.

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

I imagine what I need to do is feasible. Shazam, for 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