DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Deployment
  4. Integrating Watson Text-to-Speech Into an Android Native App

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.

Vidyasagar Machupalli user avatar by
Vidyasagar Machupalli
CORE ·
Feb. 23, 17 · Tutorial
Like (5)
Save
Tweet
Share
6.34K Views

Join the DZone community and get the full member experience.

Join For Free

this post shows how to integrate watson text-to-speech (tts) into your existing android native mobile app. watson text to speech 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 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!

mobile app Android (robot)

Published at DZone with permission of Vidyasagar Machupalli, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • The Role of Data Governance in Data Strategy: Part II
  • Why It Is Important To Have an Ownership as a DevOps Engineer
  • Utilize OpenAI API to Extract Information From PDF Files
  • API Design Patterns Review

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: