Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Integrating Watson Text-to-Speech Into an Android Native App

DZone's Guide to

Integrating Watson Text-to-Speech Into an Android Native App

Rather than reading a message, it's always good to hear it. Integrating Watson text-to-speech into your existing Android native app lets you do just that.

· Big Data Zone ·
Free Resource

Hortonworks Sandbox for HDP and HDF is your chance to get started on learning, developing, testing and trying out new features. Each download comes preconfigured with interactive tutorials, sample data and developments from the Apache community.

This post shows how to integrate Watson text-to-speech (TTS) into your existing Android native mobile app.Watson Text to SpeechIt 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 the creation of the 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, it's 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 caters 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 the Service Credentials tab and click View Credentials.
curl -X GET -u "{username}":"{password}"
"https://stream.watsonplatform.net/text-to-speech/api/v1/voices"

The above code 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:

{
  "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 ARE 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, upon the 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) {

 }
}));

Next, 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:

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

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

...and check lines 105-134 of MainActivity.java.

The journey doesn't stop here. Let's complete the flow by adding Watson speech-to-text (STT) in the next blog post. Stay tuned!

Hortonworks Community Connection (HCC) is an online collaboration destination for developers, DevOps, customers and partners to get answers to questions, collaborate on technical articles and share code examples from GitHub.  Join the discussion.

Topics:
android ,watson ,tutorial ,chatbot ,mobile ,mobile app

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}