How to continuously start and stop speech recognition in Swift

Oscar de la Hera Gomez
Two flowers that represent Swift and Xcode. Beneath them sits the text "Looping Speech Recognition."

A step by step guide on creating endlessly looping speech recognition in Swift.

The following tutorial builds off Apple's Recognizing Speech in Live Audio tutorial linked below.

We recommend that you clone our Open Source Swift Starter Project, checking out the tutorial/voice branch and carrying out the steps below. The changes can be found on the tutorial/voice-loop branch.

Please note if you do this, you can skip Step One.

git clone git@github.com:delasign/swift-starter-project.git

Step One: Setup Speech Recognition

A screenshot of Xcode showing "Hello World" on the terminal. This was created using the code in this tutorial and demonstrates that the SpeechCoordinator works.

Setup Speech Recognition by following Apple's tutorial or following the tutorial below.

Step Two: Add Variables

The SpeechCoordinator.swift open in Xcode with a highlight on the variables that you must add for this tutorial.

In SpeechCoordinator.swift, add the required variables. There are available in the snippet below.

Please note that we have set the algorithm to loop every 5 seconds, Apple advises to not make this more than 60 seconds as it has a high burden on the battery.

Step Three: Add End Audio Recording Functionality

Our SpeechCoordinator.swift file open in Xcode showing the end audio recording function. The code is available below.

In SpeechCoordinator.swift, add the endAudioRecording function by pasting in the code below.

This can also be used to end the recording through a button or action.

Step Four: Add looping functionality

The SpeechCoordinator+Transcription.swift file with a highlight on the looping functionality. This code is available below.

In SpeechCoordinator+Transcription.swift, add the restartAudioBuffer and setRestartAudioBufferTimer functions by pasting in the code below at the bottom of the extension.

Step Five: Update the Availability extension

The SpeechCoordinator+Availability.swift file open in Xcode with a highlight on where we have replaced the attempt to transcribe audio, with the function that initiates the looping recording.

Update the In SpeechCoordinator+Availability.swift extension to call the restartAudioBuffer function instead of attempting to transcribe audio.

Step Six: Update the request authorization function

The SpeechCoordinator.swift file with a highlight on the requestAuthroization function, where we have replaced the attempt to transcribe audio with the looping functionality.

Update the In SpeechCoordinator.swift, in the requestAuthorization function call the restartAudioBuffer function instead of attempting to transcribe audio.

Step Seven: Verify

A screenshot of Xcode showing the looping functionality working.

Run the app. You should now see that the speech recognition continuously loops and successfully listens to words throughout the process.

Any Questions ?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details