SystemTextToSpeechEngine
public class SystemTextToSpeechEngine : TextToSpeechEngine
Provides text-to-speech capabilities using the system’s speech synthesis engine. Converts text into speech using the specified language and optional gender preferences.
This class leverages the AVSpeechSynthesizer
to convert text messages into spoken voice.
It allows customization of the voice’s language and gender.
It supports various message formats, including plain text and Speech Synthesis Markup Language (SSML).
You could use the following code to check if the specific voice for the given language is present on your device or not.
import AVFoundation
let languageCode = "en-US" // you can change this to the desired language code
if SystemTextToSpeechEngine.isVoiceAvailable(for: languageCode) {
// Creates an instance of ``SystemTextToSpeechEngine``
} else {
// Guide the user to download the voice or use a fallback
}
For more details about the AVSpeechSynthesisVoice
read the Apple documentation:
https://developer.apple.com/documentation/avfaudio/avspeechsynthesisvoice
Important
This is a Public Preview API. It may be changed or removed at any time.-
Initializes a new instance of
SystemTextToSpeechEngine
.The initializer will throw an error if voices are not downloaded to the iOS device.
Throws
TextToSpeechEngineError.unavailableLocaleInVoices
if there is no speech voice for the givenlanguage
.Declaration
Swift
public init(language: Locale = Locale(identifier: "en_US"), gender: AVSpeechSynthesisVoiceGender? = nil) throws
Parameters
language
Identifies in which language the voice instructions will be spoken. The default is
Locale(identifier: "en_US")
.gender
The gender for a voice. If
nil
, the default gender voice available for the specified language will be used.
-
Supported phonetic alphabets.
Declaration
Swift
public var supportedAlphabets: [String]
-
A locale that defines the voice’s language.
Declaration
Swift
public var locale: Locale { get }
-
The delegate object that receives text-to-speech engine events.
Declaration
Swift
public weak var delegate: TextToSpeechEngineDelegate? { get set }
-
Checks if a specific language is available for text-to-speech.
Declaration
Swift
public static func isLanguageAvailable(for locale: Locale) -> Bool
Parameters
locale
The locale representing the language to check.
Return Value
true
if the language is available; otherwise,false
. -
Plays a text-to-speech message.
Declaration
Swift
public func play(message: TTSMessage)
Parameters
message
The message to be converted into speech.
-
Stops the text-to-speech playback.
Declaration
Swift
public func stop()
-
Sets the speech volume.
Declaration
Swift
public func setVolume(_ volume: SpeechVolume)
-
Sets the language code of the messages.
Throws
TextToSpeechEngineError.unavailableLocaleInVoices
if there is no speech voice for the givenlocale
.Declaration
Swift
public func changeLocale(with locale: Locale) throws
Parameters
locale
The locale representing the new language.