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 taking part in.

My app acknowledges speech coming in by 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 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 non-public(set) var isRecording = false
non-public var audioEngine: AVAudioEngine!
non-public var inputNode: AVAudioInputNode!
non-public var audioSession: AVAudioSession!
non-public var recognitionRequest: SFSpeechAudioBufferRecognitionRequest?

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

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

non-public 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!) { (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()
        attempt audioSession.setCategory(.file, mode: .spokenAudio, choices: .duckOthers)
        attempt audioSession.setActive(true, choices: .notifyOthersOnDeactivation)
        attempt audioEngine.begin()
    } catch {
        handleError(withMessage: error.localizedDescription)
    }
}
non-public func checkPermissions() {
    SFSpeechRecognizer.requestAuthorization { authStatus in
        DispatchQueue.important.async {
            swap authStatus {
            case .licensed: break
            default: self.handlePermissionFailed()
            }
        }
    }
}

non-public func handlePermissionFailed() {
    // Current an alert asking the person to alter their settings.
    let ac = UIAlertController(title: "This app will need to 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)
}
non-public 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)
}

}

Once you run my app and there may be audio operating 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 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 need to do is feasible. Shazam, as an example, can play a tune on the speaker and concurrently use the microphone to hearken to it and establish it.

[ad_2]

Leave a Reply

Your email address will not be published. Required fields are marked *