Contents

SFSpeechURLRecognitionRequest

A request to recognize speech in a recorded audio file.

Declaration

class SFSpeechURLRecognitionRequest

Overview

Use this object to perform speech recognition on the contents of an audio file.

The following example shows a method that performs recognition on an audio file based on the user’s default language and prints out the transcription.

Listing 1. Getting a speech recognizer and making a recognition request

func recognizeFile(url: URL) {
    // Create a speech recognizer associated with the user's default language.
    guard let myRecognizer = SFSpeechRecognizer() else {
        // The system doesn't support the user's default language.
        return
    }
    
    guard myRecognizer.isAvailable else {
        // The recognizer isn't available.
        return
    }
    
    // Create and execute a speech recognition request for the audio file at the URL.
    let request = SFSpeechURLRecognitionRequest(url: url)
    myRecognizer.recognitionTask(with: request) { (result, error) in
        guard let result else {
            // Recognition failed, so check the error for details and handle it.
            return
        }
        
        // Print the speech transcription with the highest confidence that the
        // system recognized.
        if result.isFinal {
            print(result.bestTranscription.formattedString)
        }
    }
}

Topics

Creating a speech recognition request

Accessing the audio file URL

Initializers

See Also

Audio sources