WWDC 2016 brought us a ton of new goodies, letโ€™s dive right in. Today weโ€™ll take a look at . It allows us to recognize spoken words in audio files or even audio buffers. Letโ€™s check it out.

Weโ€™ll start by importing Speech, and requesting the userโ€™s authorization:

import Speech

SFSpeechRecognizer.requestAuthorization {
  DispatchQueue.main.async {
    switch $0 {
      case .authorized:
        // TODO

      default: break

Before we can proceed, weโ€™ll need to add a called NSSpeechRecognitionUsageDescription to our โ€™s Info.plist and give it a value explaining how our will use the functionality.

Users will see this text, so we should try to be short and simple. Something like โ€œSpeech recognition will be used to provide closed captioning of your Instagram videos.โ€ (for example) should work fine.

Next we create a recognizer, then configure a request with the URL to our audio file.

Then weโ€™ll kick off a recognition task. We configure it to even partial results, then print each one.

let audioURL = Bundle.main().urlForResource("crazy-ones", withExtension: "m4a")!
let recognizer = SFSpeechRecognizer(locale: Locale(localeIdentifier: "en-US"))
let request = SFSpeechURLRecognitionRequest(url: audioURL)

request.shouldReportPartialResults = true

recognizer?.recognitionTask(with: request) { result, error in
  guard error == nil else { print("Error: (error)"); return }
  guard let result = result else { print("No result!"); return }


Success! The words spoken inside our audio file are printed, neat!

- Screen Shot 2016 06 19 23 13 38 - #236: SFSpeechRecognizer Basics ๐ŸŽ™ – Little Bites of Cocoa – Tips and techniques for iOS and Mac development

This is just the beginning, weโ€™ll explore more of SFSpeechRecognizerโ€™s capabilities in upcoming !

Source link


Please enter your comment!
Please enter your name here