Technology Post: How to apply AI natively to Android system

Technology Post: How to apply AI natively to Android system

[51CTO.com Quick Translation] Compared to reading the content of the message, it is undoubtedly more convenient to listen to the content directly. Integrating Watson's text-to-speech function into existing Android native applications can help everyone easily achieve this goal.

In today’s article, we will explore how to integrate Watson text-to-speech (TTS) functionality into existing Android native mobile applications.

The first time I tried to submit my own Watbot to GitHub, the experience was simply "dreamlike". Watbot is an Android chatbot built using Watson conversational services. It mainly helps college students learn how to create services on Bluemix and run applications through simulators or physical devices in 30 minutes to achieve learning goals. However, in my opinion, it is obviously more interesting to integrate other Watson services into the application, especially Watson text-to-speech services. Compared with reading the content of the message, it is undoubtedly more convenient to directly listen to its content.

“Text-to-speech converts written text into natural-sounding audio. You can customize and control the pronunciation of specific words, providing your audience with seamless voice interactions for interactive toys for children, automated call center interactions, and hands-free navigation systems.”

Hear messages in different voices

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

The above delegate is used to retrieve a list of all voices available for the service. Information provided includes the voice name, language, gender, etc. To get information about a specific voice, use the "Get a voice" method:


  1. {
  2. "voices": [
  3. {
  4. "name": "pt-BR_IsabelaVoice",
  5. "language": "pt-BR",
  6. "customizable": true,
  7. "gender": "female",
  8. "url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/pt-BR_IsabelaVoice",
  9. "supported_features": {
  10. "voice_transformation": false,
  11. "custom_pronunciation": true
  12. },
  13. "description": "Isabela: Brazilian Portuguese (português brasileiro) female voice."
  14. },
  15. {
  16. "name": "es-US_SofiaVoice",
  17. "language": "es-US",
  18. "customizable": true,
  19. "gender": "female",
  20. "url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/es-US_SofiaVoice",
  21. "supported_features": {
  22. "voice_transformation": false,
  23. "custom_pronunciation": true
  24. },
  25. "description": "Sofia: North American Spanish (español norteamericano) female voice."
  26. },
  27. {
  28. "name": "en-GB_KateVoice",
  29. "language": "en-GB",
  30. "customizable": true,
  31. "gender": "female",
  32. "url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/en-GB_KateVoice",
  33. "supported_features": {
  34. "voice_transformation": false,
  35. "custom_pronunciation": true
  36. },
  37. "description": "Kate: British English female voice."
  38. },
  39. {
  40. "name": "en-US_LisaVoice",
  41. "language": "en-US",
  42. "customizable": true,
  43. "gender": "female",
  44. "url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/en-US_LisaVoice",
  45. "supported_features": {
  46. "voice_transformation": true,
  47. "custom_pronunciation": true
  48. },
  49. "description": "Lisa: American English female voice."
  50. },
  51. {
  52. "name": "ja-JP_EmiVoice",
  53. "language": "ja-JP",
  54. "customizable": true,
  55. "gender": "female",
  56. "url": "https://stream-s.watsonplatform.net/text-to-speech/api/v1/voices/ja-JP_EmiVoice",
  57. "supported_features": {
  58. "voice_transformation": false,
  59. "custom_pronunciation": true
  60. },
  61. "description": "Emi: Japanese female voice."
  62. },
  63. . . .
  64. ]
  65. }

Interested friends can click here to refer to the API reference on Watson Developer Cloud to learn more about TTS API calls.

How do I integrate TTS into my Android native app?

This requires us to add the TTS Gradle entry to our build.gradle (app) file:

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

Add the following code to your MainActivity.java file and replace the username and password placeholders with your actual TTS service credentials. In addition, after adding the following code, tap a message to convert text to speech:

  1. recyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), recyclerView, new ClickListener() {
  2. @Override
  3. public void onClick(View view, final int position) {
  4. Thread thread = new Thread(new Runnable() {
  5. public void run() {
  6. Message audioMessage;
  7. try {
  8. audioMessage = (Message) messageArrayList.get(position);
  9. streamPlayer = new StreamPlayer();
  10. if (audioMessage != null && !audioMessage.getMessage().isEmpty())
  11. //Change the Voice format and choose from the available choices
  12. streamPlayer.playStream(service.synthesize(audioMessage.getMessage(), Voice.EN_LISA).execute());
  13. else
  14. streamPlayer.playStream(service.synthesize("No Text Specified", Voice.EN_LISA).execute());
  15. } catch (Exception e) {
  16. e.printStackTrace();
  17. }
  18. }
  19. });
  20. thread.start();
  21. }
  22. @Override
  23. public void onLongClick(View view, int position) {
  24. }
  25. }));
Next, build and run your app. Now whenever you tap on a message, the text will be played back through the Voice (Voice.EN_LISA). You can also change the voice format in the code. Note: If you find an error and want to check the full code, use the following command:
  1. git clone https://github.com/VidyasagarMSC/WatBot.git
Then check lines 105 to 134 of MainActivity.java. Of course, this journey is far from over. In subsequent articles, we will continue to introduce the integration methods and effects of Watson Speech-to-Text (STT) service. Stay tuned! Original title: Integrating Watson Text-to-Speech Into an Android Native App Original author: Vidyasagar Machupalli

[Translated by 51CTO. Please indicate the original translator and source as 51CTO.com when reprinting on partner sites]

<<:  Young man, it’s time to create your first cool 3D effect!

>>:  iOS vs Android Development — How Do We Choose?

Recommend

SumanSoul Level 2 Yoga Course 5th

Course Catalog: ├──01 Sumansoul Level 2 Enhanced ...

Weibo marketing promotion strategy, share these 3 points!

Weibo may not bring direct sales, but it can subt...

Operational list for private domain traffic diversion on TikTok!

The article is short and only contains essential ...

How to create a short video with a mobile phone?

How to make a card-point video with a mobile phon...

A guide to avoiding pitfalls when placing ads on Weibo and WeChat!

There was an article that went viral this morning...

3 Tips to Avoid Pitfalls in Kuaishou Paid Promotion and Increasing Followers

Operators will come into contact with paid promot...

11 commonly used data analysis methods for product operations!

There are many methods of data analysis . Today w...

There are only a few APP downloads per day, how to deal with it

If your app has been launched and follows the fol...