科技帖 如何將AI原生的應用到Android系統
譯文【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】