DEV Community

DEV Community

Younes Charfaoui

Posted on Oct 22, 2021

Make your Android apps talk with Text-To-Speech

In a previous article , I described how to utilize speech recognition in Android. This consisted of capturing user speech, processing it, and implementing it.

But what if that process is reversed, and you want to take a text input and output speech? This type of system is, so-called, text-to-speech (TTS). TTS software, in general, creates a computer-generated voice that’s also considered as an assistive technology tool.

This technology has already been adopted widely in real-world applications like Assistants (Google and Siri) for interacting with the user commands. By applying this feature, you can rapidly increase the accessibility of your application for those with visual impairments and reading struggles. For example, this feature could especially help those with learning disabilities or those with difficulty reading large amounts of text due to dyslexia.

In today’s article, we’ll clarify the way to integrate this technology into Android applications using only the core SDK, so your applications can start speaking.

This article presumes that you have some experience in building Android apps in Kotlin.

Project Setup

To implement TTS technology, open up a new Android Studio project—no need for permissions or other library dependencies.

Text-To-Speech (TTS)

To start, we’ll work with a class called TextToSpeech . We also need the text to be spoken, so this one should be in string format. The first task is to create an instance of this class. Here’s how we initialize a global variable for this instance:

The first “this” in the constructor is the context of the activity I’m in; the second “this” will be the initialization listeners of type TextToSpeech.OnInitListener , which will tell us about the TextToSpeech initialization result. For that, we need to let our activity implement the previous interface, and then we override the method called onInit() . Here’s a code sample:

As the code snippet above shows, if the status is TextToSpeech.SUCCESS , that means we’re ready to move forward. Next, we need to set up the language in which we want to speak—bear in mind that this isn’t always guaranteed, so we need to do an extra check to see if the language we’d like to use is supported and if there are sufficient data packages (voices) available.

At this point, we can say something with the TTS Engine via the speak() method. This will take 3 parameters: the “something”, which is what we want the phone to say in string format, a queuing strategy (we will talk more about this next), and finally the utteranceid , which can be used to identify the request to the TTS so we can use them in callbacks later on.

The speak() method will return the results of queuing the speak operation. Note that this method is asynchronous, and as the docs say The synthesis might not have finished (or even started!) at the time when this method returns. Thus, we can’t rely on the result of this method as a speech state, but we can use callbacks (as shown in the Add Callbacks section below) to detect errors.

Here’s a code snippet illustrates this:

Queuing Strategy

The queueMode parameter passed with the speak method is used to handle multiple requests by the TTS. It can be in 2 states: QUEUE_ADD or QUEUE_FLUSH .

The QUEUE_ADD state is a queue mode where the new entry is added at the end of the playback queue, while QUEUE_FLUSH is also a queue mode where all entries in the playback queue (media to be played and text to be synthesized) are dropped and replaced by the new entry.

For example, let’s say that we call the speak method 3 times with Strings of “Hello”, “Hi” and “How are you”. With queueMode being set to QUEUE_ADD , this means that the TTS Engine will speak the three different strings in order, The QUEUE_FLUSH mode, on the other hand, will indicate to the TTS Engine to only speak the last String (“How are you”).

Add Callbacks

For advanced flexibility with the TTS Engine, you can add callbacks to understand the state of the TTS Engine. For example, you can find out if the Engine is finished talking or when/if it starts, and so on.

This kind of information can be very useful when you want to do something based on its state. For instance, I built an Android application where I wanted to show an ad banner just after saying an expression. In this case, these callbacks were very helpful in doing this. Here’s some sample code that shows how to accomplish that by implementing an interface called UtteranceProgressListener via the setOnUtteranceProgressListener() method. The overridden methods are self-expressive, as shown:

As you can see in the methods, the utteranceId is passed, by which we can identify which speech is being processed. Thus, we can take action upon it.

Stopping the TTS Engine

You can also interrupt the TTS Engine at various points in time. Say the user hit the cancel button or something similar—here, you can stop it by using the stop() method. This way, we discard the current utterance and those in the queue as well.

Also, don’t forget to release the resources used by the TTS engine when you don’t need them. As a rule of thumb, when the activity destroyed or stopped, we should use the shutdown() method.

Resources & References

One of the best resources to learn how to use android API’s is the official documentation—check out the TextToSpeech class for more helpful methods.

Other tutorials are available on TutorialsPoint and JavaPapers , which showcase end-to-end TTS projects with Java code.

I hope you enjoyed reading this article and it contributed to your knowledge and interest in TextToSpeech. Android now talks, and so can your apps. Implementing such a feature can make your app contemporary, straightforward, and user-friendly, since most of the users expect such intelligent behaviors from today’s apps, especially for those with disabilities.

Top comments (0)

pic

Templates let you quickly answer FAQs or store snippets for re-use.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink .

Hide child comments as well

For further actions, you may consider blocking this person and/or reporting abuse

siddhantkcode profile image

Mastering the Art of Invisible Indexes in MySQL

Siddhant Khare - May 22

paulike profile image

Copying Arrays

Paul Ngugi - May 18

oluwadamisisamuel1 profile image

How to Automate Test Generation with AI: Using CodiumAI Cover-Agent

Oluwadamisi Samuel Praise - May 22

egeaytin profile image

JWT vs PASETO: New Era of Token-Based Authentication

Ege Aytin - May 22

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

  • Java for Android
  • Android Studio
  • Android Kotlin
  • Android Project
  • Android Interview

How to Convert Text to Speech in Android?

  • How to Convert Speech to Text in Android?
  • How to Convert Text to Speech in Android using Kotlin?
  • How to Change Spinner Text Style in Android?
  • How to Create Marquee Text in Android?
  • How to Create Text Stickers in Android?
  • Speech to Text Application in Android with Kotlin
  • How to Use Text Conversion API in Android 13?
  • How to Convert a Vector to Bitmap in Android?
  • How to Read a Text File in Android?
  • Converting Text to Speech in Java
  • How to Change Typeface of TextView in Android?
  • How to convert text to speech in Node.js ?
  • Python | Convert image to text and then to speech
  • Python: Convert Speech to text and text to Speech
  • Convert Text to Speech in Python
  • How to Copy & Paste in Android?
  • How to Create a Simple Text to Speech Application?
  • Convert PDF File Text to Audio Speech using Python
  • How to convert speech into text using JavaScript ?

Text to Speech App converts the text written on the screen to speech like you have written “Hello World” on the screen and when you press the button it will speak “Hello World”. Text-to-speech is commonly used as an accessibility feature to help people who have trouble reading on-screen text, but it’s also convenient for those who want to be read too. This feature has come out to be a very common and useful feature for the users.

Running Condition of the App

Note: To implement its vice versa that is to convert speech to text please refer to How to Convert Speech to Text in Android?

Steps for Converting Text to Speech in Android

Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio . Note that select Java as the programming language.

Step 2: Working with activity_main.xml file

Go to the app -> res -> layout -> activity_main.xml section and set the layout for the app. In this file add an EditText to input the text from the user, a Button , so whenever the user clicks on the Button then it’s converted to speech and a TextView to display the GeeksforGeeks text. Below is the complete code for the activity_main.xml file.

activity_main.xml

Step 3: Working with MainActivity.java file

Go to the app -> java -> com.example.GFG(Package Name) -> MainActivity.java section. Now join the Button and Edittext to Java code and comments are added inside code to understand the code easily. Below is the complete code for the MainActivity.java file.

MainActivity.java

The user may choose another language as well. For that refer to the below image to see how to do that.

text to speech android app stack overflow

Output: Run on Emulator

Please login to comment..., similar reads, improve your coding skills with practice.

 alt=

What kind of Experience do you want to share?

Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications

Kotlin Multiplatform Text-to-Speech library for Android and browser (Kotlin/JS & Kotlin/Wasm)

Marc-JB/TextToSpeechKt

Folders and files, repository files navigation, texttospeechkt.

Kotlin Multiplatform Text-to-Speech library for Android and browser (Kotlin/JS & Kotlin/Wasm).

Quality Gate Status

📔 Table of Contents

  • Prerequisites
  • Installation
  • Acknowledgements

🌟 About the Project

👾 tech stack.

Uses Kotlin Multiplatform with support for the following targets:

  • Create the engine with Kotlin Coroutines
  • Await speech synthesis completion using Kotlin Coroutines
  • Modify the volume or mute the volume entirely
  • Modify the voice pitch
  • Modify the voice rate
  • Compose support with rememberTextToSpeechOrNull() (works in multiplatform code!)

🧰 Getting Started

‼️ prerequisites.

A build tool like Gradle or Maven.

⚙️ Installation

Configure the Maven Central repository:

And add the library to your dependencies:

Add the library to your dependencies:

Documentation files

View documentation generated by Dokka

Demo projects

Go to the /demo directory of this project.

This project is published under the MIT License. Read more about this license in the LICENSE file.

💎 Acknowledgements

  • Awesome Readme Template

Releases 30

Packages 12, contributors 3.

@Marc-JB

  • Kotlin 100.0%

Android Overview

Spokestack can be integrated with Android apps developed in Java and Kotlin.

Integrations by Feature

Add speech recognition , language understanding , and text-to-speech to your Android app with one simple API.

Select a feature you’d like to use to see a minimal configuration using only that feature. Configurations shown here may also be combined; see the individual documentation pages on the left for more information.

Try a Wake Word in Your Browser

Test a wake word model by pressing “Start test,” then saying “Spokestack”. Wait a few seconds for results. This browser tester is experimental.

Activated! Confidence: 0

Say “ Spokestack ” when testing

Instructions

  • Test a model by pressing "start test" above
  • Then, try saying any of the utterances listed above. Wait a few seconds after saying an utterance for a confirmation to appear.

Spokestack Android SDKs

Spokestack manages voice interactions and delivers actionable user commands with just a few lines of code. To integrate, first decide whether you want to manage the UI yourself or use a drop-in UI widget to display the conversation history.

Extensible Android mobile voice framework: wakeword, ASR, NLU, and TTS. Easily add voice to any Android app!

A UI component that makes it easy to add voice interaction to your app.

Related Resources

Want to dive deeper into the world of Android voice integration? We've got a lot to say on the subject:

Explore Related Tutorials

Explore related blog posts, explore related docs, something missing, become a spokestack maker and #ownyourvoice.

Podcast 350: A deep dive into natural language processing and speech to text systems

From Siri to services that transcribe our every word, we explore advancements in computer systems that can understand human conversation and commands.

Article hero image

Today's episode is sponsored by Rev . We explore the history of automatic speech recognition and computer systems that can understand human commands. From there, we explain the machine learning revolution that has powered recent advancements in speech to text systems, like the one employed by Rev for automatic transcription. Finally, we look to the future, and imagine the features and services that the next generation of this AI could produce.

In this episode we chat with three guests:

Miguel Jetté : Head of AI R&D

Josh Dong : AI Engineering Manager

Jenny Drexler : Senior Speech Scientist

When Jetté was studying mathematics in the early 2000s, his focus was on computational biology, and more specifically, phylogenetic trees, and DNA sequences. He wanted to understand the evolution of certain traits and the forces that explain why our bones are a certain length or our brains a certain size. As it turned out, the algorithms and techniques he learned in this field mapped very well to the emerging discipline of automatic speech recognition, or ASR.

During this period, Montreal was emerging as a hotbed for artificial intelligence, and Jetté found himself working for Nuance, the company behind the original implementation of Siri. That experience led him to several positions in the world of speech recognition, and he eventually landed at Rev, where he founded the company’s AI department.

Jetté describes Rev as an “Uber for Transcription.” Anyone can sign up for the platform and earn money by listening to audio submitted by clients and transcribing the speech into text. This means the company has a tremendous dataset of raw audio that has been annotated by human beings and, in many cases, assessed a second time by the client. For someone looking to build an AI system that mastered the domain of speech to text, this was a goldmine.

Jetté built the earliest version of Rev’s AI, but it was up to our second guest, Josh Dong, to productize and scale that system. He helped the department transition from older technologies like Perl to more popular languages like Python. He also focused on practical concerns like modularity and reusable components. To combine machine learning and DevOps, Dong added Docker containers and a testing pipeline. If you’re interested in the nuts and bolts of keeping a system like Rev’s running at tremendous scale, you’ll want to check out this part of the show.

We also explore some of the fascinating future and promise this technology holds in our time with Jenny Drexler. She explains how Rev is moving from a hybrid model—one that combines Jetté’s older statistical techniques with Dong’s newer machine learning approach—to a new system that will be ML from end-to-end. This will open up the door for powerful applications, like a single system that can convert speech text across multiple languages in a single piece of audio.

“One of the things that's really cool about these end to end models is that basically, whatever data you have, it can learn to handle it. So a very similar architecture can do sequence to sequence learning with different kinds of sequences. The model architecture that you might use for speech recognition can actually look very similar to what you might use for translation. And you can use that same architecture, to say, feed in audio in lots of different languages and be able to do transcription for any of them within one model. It's much harder with the hybrid models to sort of put all the right pieces together to make that happen,” explains Drexler.

If you’re interested in learning more about the past, present, and future of artificial intelligence that can understand our spoken language and learn how to respond, check out the full episode. If you want to learn more about Rev or check out some of the positions they have open, you can find their careers page  here .

The Stack Overflow blog is committed to publishing interesting articles by developers, for developers. From time to time that means working with companies that are also clients of Stack Overflow’s through our advertising, talent, or teams business. When we publish work from clients, we’ll identify it as Partner Content with tags and by including this disclaimer at the bottom.

IMAGES

  1. text to speech

    text to speech android app stack overflow

  2. Speech To Text App TUTORIAL (using in-built feature)

    text to speech android app stack overflow

  3. How to Build a Speech to Text App in Android Studio

    text to speech android app stack overflow

  4. Text to Speech

    text to speech android app stack overflow

  5. Text to speech

    text to speech android app stack overflow

  6. Text To Speech Recognition -Android

    text to speech android app stack overflow

VIDEO

  1. Working with TTS in Android Studio

  2. Blaupunkt Android TV : How to Turn OFF Talkback mode

  3. Text reader for Android Phone.Select To Speak Setting.#androidtricks2023

  4. Text to Speech In Android Like Google Translate By Voice

  5. TEXT To Speech Emoji Groupchat Conversations

  6. Android Text to Speech App Project with Source code

COMMENTS

  1. Text to speech(TTS)-Android

    Here are instructions on how to download sample code from the Android SDK Manager:. Launch the Android SDK Manager. a. On Windows, double-click the SDK Manager.exe file at the root of the Android SDK directory.

  2. Android Text-To-Speech API Sounds Robotic

    On an Android Studio Emulator: Create a new emulator and select a system image that has "Google APIs" or "Google Play" in the "target" column. On a real device: Go to the Play Store and install the Google speech engine. TTS on Android (or at least trying to predict its behavior) can be a real beast.

  3. Speech to Text Android App

    I'm trying to create a speech to text app, to get started I got sample code from android developer site. ... Products For Teams; Stack Overflow Public questions & answers; Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers; Talent Build your employer brand ... Android Text to Speech embedded ...

  4. Speech-to-text and Text-to-speech with Android

    There is a catch though - the device will require Google Search app for the service to work. The Text-to-speech API, unlike Speech Recognition, is available without Google Services, and can be found in android.speech.tts package. Source code You can find the source of this tutorial on GitHub. Let's develop!

  5. Unlocking the Power of Android TTS: A Guide to Using Different ...

    Handling Speech Pauses: Method: synthesizeToFile(CharSequence text, Bundle params, File file, String utteranceId) Use: This method allows you to synthesize the given text to a file with specified ...

  6. Make your Android apps talk with Text-To-Speech

    To start, we'll work with a class called TextToSpeech. We also need the text to be spoken, so this one should be in string format. The first task is to create an instance of this class. Here's how we initialize a global variable for this instance: val textToSpeech = TextToSpeech(this, this) The first "this" in the constructor is the ...

  7. TextToSpeech

    Build for Billions. Create the best experience for entry-level devices. Overview. About new markets. Android (Go edition) Develop. Gemini is here. Gemini in Android Studio is your AI development companion for Android development.

  8. Newest 'text-to-speech' Questions

    About Us Learn more about Stack Overflow the company , and our products ... The option to download and set high-quality voices in Android text-to-speech is not available in MIUI 14. ... enable the loudspeaker, and play a voice message aloud using text-to-speech. Meaning, the app ... applications; calls; text-to-speech; auto-answer ...

  9. Speech to Text Android

    0. Have an app written that automatically reads out received text messages when they arrive. Interested in perhaps a voice command that allows the receiver to speak a response - without touching the phone, which sends this string as a reply SMS. Iv'e done some searches and seen some examples, but unsure of the complexity of integrating this ...

  10. Newest 'text-to-speech' Questions

    Stack Exchange Network. Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange

  11. How to Convert Text to Speech in Android?

    Steps for Converting Text to Speech in Android. Step 1: Create a New Project. To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language. Step 2: Working with activity_main.xml file.

  12. android

    startActivity(installIntent); You can also check if the language exists or not. This is the first step you have to do. Intent checkIntent = new Intent(); checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent, MY_DATA_CHECK_CODE); You can do it like this.

  13. Text-to-speech for Android

    Note: As of version 9.0.0, TTS is included in the turnkey Spokestack object. This guide is still valid as an in-depth introduction to the TTS module itself, but see the configuration guide for more information about how it's integrated in newer versions of Spokestack.. Text-to-speech is a broad topic, but as far as Spokestack is concerned, there are two things your app has to handle: sending ...

  14. Android text to speech app

    What it does basically is taking a sentence phrase from an EditText ed and translate it to speech when you click the start button. I used onActivityResult to prevent app from crashing when the EditText becomes empty. I am sensing that this code, while working perfectly, is far from perfect. import android.support.v7.app.AppCompatActivity;

  15. GitHub

    Kotlin Multiplatform Text-to-Speech library for Android and browser (Kotlin/JS & Kotlin/Wasm) - Marc-JB/TextToSpeechKt

  16. I'm encountering difficulties developing an app that ...

    I'm currently developing an app that integrates video conferencing (using jitsiMeetSDK) and STT (using @react-native-voice/voice) with React Native. I'm encountering an issue on Android where, while connected to a video conference and using the microphone permission, I'm unable to use STT.

  17. Getting Started

    In a single-activity app, the easiest place for this is going to be your main activity. import io.spokestack.spokestack.Spokestack at the top of the file, and add a Spokestack member: private lateinit var spokestack: Spokestack. You'll probably want to build the pipeline when the activity is created.

  18. bluetooth

    StartTalking seems like an app to check out. It will let you listen to incoming text messages and even replay to them. It doesn't require any Bluetooth devices (but I think it will work with one), and it's true hands free - meaning you don't have to press any button to use it, both for listening to it and replaying, it's fully voice activated.

  19. Android Overview

    Spokestack can be integrated with Android apps developed in Java and Kotlin. Integrations by Feature. Add speech recognition, language understanding, and text-to-speech to your Android app with one simple API. Select a feature you'd like to use to see a minimal configuration using only that feature.

  20. Podcast 350: A deep dive into natural language ...

    Podcast 350: A deep dive into natural language processing and speech to text systems. From Siri to services that transcribe our every word, we explore advancements in computer systems that can understand human conversation and commands. ... The Stack Overflow blog is committed to publishing interesting articles by developers, for developers ...

  21. Is there a way to change the "text to speech" person's voice?

    Stack Exchange Network. Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. Visit Stack Exchange