Language Detection and Text to Speech in SwiftUI Apps

Simon Ng
AppCoda Tutorials
Published in
3 min readJul 11, 2022

--

With the advent of machine learning and artificial intelligence, the iOS SDK already comes with a number of frameworks for developers to develop apps with machine learning-related features.

In this tutorial, let’s explore two built-in ML APIs for converting text to speech and performing language detection.

Using Text to Speech in AVFoundation

Let’s say, if we are building an app that reads users’ input message, you need to implement some sort of text-to-speech functions.

The AVFoundation framework has come with some text-to-speech APIs. To use those APIs, we have to first import the framework:

import AVFoundation

Next, create an instance of AVSpeechSynthesizer:

let speechSynthesizer = AVSpeechSynthesizer()

To convert the text message to speech, you can write the code like this:

let utterance = AVSpeechUtterance(string: inputMessage)
utterance.pitchMultiplier = 1.0
utterance.rate = 0.5
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

speechSynthesizer.speak(utterance)

You create an instance of AVSpeechUtterance with the text for the synthesizer to speak. Optionally, you can configure the…

--

--

Simon Ng
AppCoda Tutorials

Founder of AppCoda. iOS Developer and Indie Entrepreneur. Love coffee, food and travel. http://www.appcoda.com