Thursday, September 19, 2024
HomeiOS Developmentios - Swift: Acknowledge speech from microphone whereas audio performs within the...

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


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 way 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 potential 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!) { (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(.document, 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.foremost.async {
            change authStatus {
            case .approved: 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 contemplate 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)
}

}

If 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 consider what I wish to do is feasible. Shazam, for example, can play a tune on the speaker and concurrently use the microphone to take heed to it and determine it.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments