Add Watson Text to Speech to your Android app in simple steps

Vidyasagar Machupalli
3 min readFeb 22, 2017

--

The blog post shows how to integrate Watson Text-to-Speech(TTS) into your existing Android native mobile app.

It was a dream come true when I made my first Watbot commit to Github. Watbot is an Android chatbot built using Watson Conversation Service. This was for a Hands-on lab in a college. As the students were able to build a bot in less than 30 minutes — from creation of service on Bluemix to running the app on an emulator or a physical device, I thought of making this more interesting by integrating other Watson services into the app.

One such service which goes hand in hand is Watson Text-to-Speech. Rather than reading a message its always good to hear it.

Text to Speech converts written text into natural sounding audio in a variety of languages and voices. You can customize and control the pronunciation of specific words to deliver a seamless voice interaction that catered s to your audience. Use text to speech to develop interactive toys for children, automate call center interactions, and communicate directions hands-free.

Hear the message in different voices

  • Create a Watson Text to Speech(TTS) service on Bluemix.
  • Navigate to Service Credentials tab and click on “View Credentials”.
curl -X GET -u "{username}":"{password}"
"https://stream.watsonplatform.net/text-to-speech/api/v1/voices"

Retrieves a list of all voices available for use with the service. The information includes the voice’s name, language, and gender, among other things. To see information about a specific voice, use the Get a voice method.

Json:

{
"voices": [
{
"name": "pt-BR_IsabelaVoice",
"language": "pt-BR",
"customizable": true,
"gender": "female",
"url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/pt-BR_IsabelaVoice",
"supported_features": {
"voice_transformation": false,
"custom_pronunciation": true
},
"description": "Isabela: Brazilian Portuguese (português brasileiro) female voice."
},
{
"name": "es-US_SofiaVoice",
"language": "es-US",
"customizable": true,
"gender": "female",
"url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/es-US_SofiaVoice",
"supported_features": {
"voice_transformation": false,
"custom_pronunciation": true
},
"description": "Sofia: North American Spanish (español norteamericano) female voice."
},
{
"name": "en-GB_KateVoice",
"language": "en-GB",
"customizable": true,
"gender": "female",
"url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/en-GB_KateVoice",
"supported_features": {
"voice_transformation": false,
"custom_pronunciation": true
},
"description": "Kate: British English female voice."
},
{
"name": "en-US_LisaVoice",
"language": "en-US",
"customizable": true,
"gender": "female",
"url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/en-US_LisaVoice",
"supported_features": {
"voice_transformation": true,
"custom_pronunciation": true
},
"description": "Lisa: American English female voice."
},
{
"name": "ja-JP_EmiVoice",
"language": "ja-JP",
"customizable": true,
"gender": "female",
"url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/ja-JP_EmiVoice",
"supported_features": {
"voice_transformation": false,
"custom_pronunciation": true
},
"description": "Emi: Japanese (日本語) female voice."
},
. . .
]
}

Refer API Reference on Watson Developer Cloud for other TTS API calls.

How to integrate TTS into my Android native App?

The required gradle entries for TTS is already included in the build.gradle(app) file

compile 'com.ibm.watson.developer_cloud:text-to-speech:3.5.3'
compile 'com.ibm.watson.developer_cloud:android-sdk:0.2.1'

In your MainActivity.java file, add the below lines of code and replace the username and password placeholders with the TTS service credentials

Also, add the below code which on tap(click) of a message will convert the text into speech

recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
@Override
public void onClick(View view, final int position) {
Thread thread = new Thread(new Runnable() {
public void run() {
Message audioMessage;
try {
audioMessage =(Message) messageArrayList.get(position);
streamPlayer = new StreamPlayer();
if(audioMessage != null && !audioMessage.getMessage().isEmpty())
//Change the Voice format and choose from the available choices
streamPlayer.playStream(service.synthesize(audioMessage.getMessage(), Voice.EN_LISA).execute());
else
streamPlayer.playStream(service.synthesize("No Text Specified", Voice.EN_LISA).execute());
} catch (Exception e) {
e.printStackTrace();
}
}
});
thread.start();
}
@Override
public void onLongClick(View view, int position) {

}
}));
  • Build and Run your app.

Now when you TAP on any message, the text will be heard via a Voice (Voice.EN_LISA). You can change the voice formats in the code.

Check this video to see Watson Text-to-Speech in action,

WatBot — A Voice-enabled Android Chatbot from Vidyasagar MSC on Vimeo.

Note: If you are seeing errors and want to check the complete code,

git clone https://github.com/VidyasagarMSC/WatBot.git

and check lines 105–134 of MainActivity.java

The journey doesn’t stop hear. Let’s complete the flow by adding Watson Speech-to-Text (STT) in the next blog post.

Stay Tuned!!

--

--

Vidyasagar Machupalli
Vidyasagar Machupalli

Written by Vidyasagar Machupalli

Architect, Developer, IBMer, Speaker, Blogger, Teetotaller, Geek & many more…

No responses yet