How to continuously start and stop speech recognition in Swift

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

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 02/02/2023 at 12:15
Last Updated on 02/02/2023 at 13:31
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.

SubscribeLearn more about Apple's Recognizing Speech in Live Audio

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
View RepositoryHow to create a SwiftLint enabled Swift XCode Project

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.

How to setup speech recognition in SwiftDownload Apple's Recognizing Speech in Live Audio

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.

Plan for a one-minute limit on audio duration. Speech recognition places a relatively high burden on battery life and network usage. To minimize this burden, the framework stops speech recognition tasks that last longer than one minute. This limit is similar to the one for keyboard-related dictation.

Read up on Apple's advice on creating a great speech recognition experience

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.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe