【51CTO.com快译】相较于阅读消息内容,直接听取内容无疑更为便捷。将沃森的文本到语音功能集成至现有Android原生应用中能够帮助大家轻松实现这一目标。
在今天的文章中,我们将探讨如何将沃森文本到语音(简称TTS)功能集成至现有Android原生移动应用当中。
在***次尝试向GitHub提交自己开发的Watbot时,这样的经历简直可以用“梦幻般”来形容。Watbot是一款利用沃森对话服务打造的Android聊天机器人,主要帮助高校学生们学习如何在30分钟内通过在Bluemix上创建服务并经由模拟器或者物理设备运行应用,最终实现学习目标。不过在我看来,将其它沃森服务集成至应用当中显然更为有趣,特别是沃森文本到语音服务。相较于阅读消息内容,直接听取其内容无疑更加便捷。
“文本到语音转换能够将书面文本转化为自然发声音频。您可以自定义并控制特定词汇的发音,从而为受众提供无缝化语音交互成果,其适用于儿童互动玩具、自动呼叫中心交互以及免提式导航系统。”
以不同语音听取消息内容
- 在Bluemix上创建一项沃森文本到语音(简称TTS)服务。
- 前往Service Credentials标签并点击View Credentials。
- curl -X GET -u "{username}":"{password}"
- "https://stream.watsonplatform.net/text-to-speech/api/v1/voices"
以上代表用于检索全部可用于服务的语音列表。其中提供的信息包括语音名称、语种以及性别等等。要了解关于特定语音的信息,请使用“Get a voice”方法:
- {
- "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."
- },
- . . .
- ]
- }
感兴趣的朋友可以点击此处参阅沃森开发者云之上的API参考资料,从而了解更多与TTS API调用相关的知识
如何将TTS集成至我的Android原生应用?
这要求我们将TTS的Gradle条目添加至build.gradle(应用)文件当中:
- compile 'com.ibm.watson.developer_cloud:text-to-speech:3.5.3'
- compile 'com.ibm.watson.developer_cloud:android-sdk:0.2.1'
在您的MainActivity.java文件内添加以下代码,并将用户名与密码占位符替换为实际TTS服务凭据。另外,在添加以下代码后,点触一段消息即可将文本转换为语音:
- 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) {
- }
- }));
- git clone https://github.com/VidyasagarMSC/WatBot.git
【51CTO译稿,合作站点转载请注明原文译者和出处为51CTO.com】