科技帖 如何将AI原生的应用到Android系统

译文
新闻 移动应用
在今天的文章中,我们将探讨如何将沃森文本到语音(简称TTS)功能集成至现有Android原生移动应用当中。

【51CTO.com快译】相较于阅读消息内容,直接听取内容无疑更为便捷。将沃森的文本到语音功能集成至现有Android原生应用中能够帮助大家轻松实现这一目标。

在今天的文章中,我们将探讨如何将沃森文本到语音(简称TTS)功能集成至现有Android原生移动应用当中。

 在***次尝试向GitHub提交自己开发的Watbot时,这样的经历简直可以用“梦幻般”来形容。Watbot是一款利用沃森对话服务打造的Android聊天机器人,主要帮助高校学生们学习如何在30分钟内通过在Bluemix上创建服务并经由模拟器或者物理设备运行应用,最终实现学习目标。不过在我看来,将其它沃森服务集成至应用当中显然更为有趣,特别是沃森文本到语音服务。相较于阅读消息内容,直接听取其内容无疑更加便捷。

“文本到语音转换能够将书面文本转化为自然发声音频。您可以自定义并控制特定词汇的发音,从而为受众提供无缝化语音交互成果,其适用于儿童互动玩具、自动呼叫中心交互以及免提式导航系统。”

以不同语音听取消息内容

  • 在Bluemix上创建一项沃森文本到语音(简称TTS)服务。
  • 前往Service Credentials标签并点击View Credentials。
  1. curl -X GET -u "{username}":"{password}" 
  2. "https://stream.watsonplatform.net/text-to-speech/api/v1/voices" 

以上代表用于检索全部可用于服务的语音列表。其中提供的信息包括语音名称、语种以及性别等等。要了解关于特定语音的信息,请使用“Get a voice”方法:


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

感兴趣的朋友可以点击此处参阅沃森开发者云之上的API参考资料,从而了解更多与TTS API调用相关的知识

如何将TTS集成至我的Android原生应用?

这要求我们将TTS的Gradle条目添加至build.gradle(应用)文件当中:

  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' 

在您的MainActivity.java文件内添加以下代码,并将用户名与密码占位符替换为实际TTS服务凭据。另外,在添加以下代码后,点触一段消息即可将文本转换为语音:

  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. })); 
接下来,构建并运行您的应用。
现在只要点触某段消息,其文本亦会通过Voice(Voice.EN_LISA)形式进行播放。大家也可以在代码中变更语音格式。
请注意:如果您发现错误并希望检查完整代码,请使用以下命令:
  1. git clone https://github.com/VidyasagarMSC/WatBot.git 
而后检查MainActivity.java的第105到第134行。
当然,这段旅程到这里还远没有结束。在后续文章中,我们将继续介绍沃森语音到文本(简称STT)服务的集成方式与效果。敬请期待!
 
原文标题:Integrating Watson Text-to-Speech Into an Android Native App
原文作者:Vidyasagar Machupalli 

【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】

责任编辑:陈琳 来源: 51cto
相关推荐

2023-12-12 16:46:44

AI云团队云管理

2012-02-10 09:38:48

JMP工业工程

2018-02-05 00:02:48

WANSDN网络

2018-09-29 09:00:00

2022-07-04 14:21:11

人工智能工具存储

2021-07-05 16:53:04

AI人工智能教育

2023-11-03 15:12:44

数据治理AI/ML系统

2021-08-03 15:26:56

代码智能阿里云

2021-12-29 14:57:47

德勤人工智能AI驱动型企业

2018-11-13 10:40:01

区块链汽车大数据

2009-02-10 10:49:28

滕博视频应用

2023-10-06 23:36:50

2017-09-25 15:49:01

2022-11-25 16:27:07

应用开发鸿蒙

2010-03-15 14:03:37

CloudSwitch迁移应用到云端

2022-09-14 11:32:49

物联网物联网技术

2022-09-21 12:37:43

物联网安全设备

2023-03-31 14:30:10

2023-07-17 16:04:40

点赞
收藏

51CTO技术栈公众号