TextToSpeech

public protocol TextToSpeech : AnyObject

Manages the TTS (Text-To-Speech) engine. Provides functionality for changing the engine, setting the language, playing messages, stopping playback, and adjusting volume.

The TextToSpeech protocol is essential for applications that require text-to-speech capabilities. It enables dynamic replacement of the TTS engine, adjustment of the spoken language, prioritization of messages, playback control, and speech volume configuration.

Important

This is a Public Preview API. It may be changed or removed at any time.
  • Replaces the current instance of the TextToSpeechEngine with a new one.

    It allows for runtime replacement of the TTS engine, enabling flexibility and customization. Ensure the new engine is properly configured before passing it to this method.

    Declaration

    Swift

    func changeTTsEngine(to ttsEngine: TextToSpeechEngine)

    Parameters

    ttsEngine

    The new text-to-speech engine to be used. Must conform to the TextToSpeechEngine protocol.

  • Updates the language of the TextToSpeechEngine to a specified locale.

    This method changes the locale used by the TTS engine, facilitating support for multiple languages. Ensure the specified locale is supported by the TTS engine to prevent errors.

    Throws

    TextToSpeechEngineError.unavailableLocaleInVoices if the TTS engine does not support the specified locale.

    Declaration

    Swift

    func changeLanguage(locale: Locale) throws

    Parameters

    locale

    The new locale to set. This should be a locale supported by the TTS engine.

  • Play the message.

    Plays a specified message using the TTS engine. Messages can be prioritized and have timeouts.

    Declaration

    Swift

    func play(message: TTSMessage, priority: TTSMessagePriority)

    Parameters

    message

    The message to play. This should be a TTSMessage object containing the text to be spoken.

    priority

    The priority of the message with a timeout. This determines the order and urgency of message playback.

  • Stop playing the voice message.

    It stops any ongoing message playback, allowing for interruption of speech.

    Declaration

    Swift

    func stop()
  • Set the speech volume.

    It adjusts the speech volume, allowing for dynamic volume changes. Ensure the volume level is within the acceptable range before setting it.

    Declaration

    Swift

    func setVolume(_ volume: SpeechVolume)

    Parameters

    volume

    The volume level to set. This should be a SpeechVolume value representing the desired volume.