科技帖 如何將AI原生的應(yīng)用到Android系統(tǒng)
譯文【51CTO.com快譯】相較于閱讀消息內(nèi)容,直接聽取內(nèi)容無疑更為便捷。將沃森的文本到語音功能集成至現(xiàn)有Android原生應(yīng)用中能夠幫助大家輕松實現(xiàn)這一目標(biāo)。
在今天的文章中,我們將探討如何將沃森文本到語音(簡稱TTS)功能集成至現(xiàn)有Android原生移動應(yīng)用當(dāng)中。
在***次嘗試向GitHub提交自己開發(fā)的Watbot時,這樣的經(jīng)歷簡直可以用“夢幻般”來形容。Watbot是一款利用沃森對話服務(wù)打造的Android聊天機器人,主要幫助高校學(xué)生們學(xué)習(xí)如何在30分鐘內(nèi)通過在Bluemix上創(chuàng)建服務(wù)并經(jīng)由模擬器或者物理設(shè)備運行應(yīng)用,最終實現(xiàn)學(xué)習(xí)目標(biāo)。不過在我看來,將其它沃森服務(wù)集成至應(yīng)用當(dāng)中顯然更為有趣,特別是沃森文本到語音服務(wù)。相較于閱讀消息內(nèi)容,直接聽取其內(nèi)容無疑更加便捷。
“文本到語音轉(zhuǎn)換能夠?qū)嫖谋巨D(zhuǎn)化為自然發(fā)聲音頻。您可以自定義并控制特定詞匯的發(fā)音,從而為受眾提供無縫化語音交互成果,其適用于兒童互動玩具、自動呼叫中心交互以及免提式導(dǎo)航系統(tǒng)。”
以不同語音聽取消息內(nèi)容
- 在Bluemix上創(chuàng)建一項沃森文本到語音(簡稱TTS)服務(wù)。
- 前往Service Credentials標(biāo)簽并點擊View Credentials。
- curl -X GET -u "{username}":"{password}"
- "https://stream.watsonplatform.net/text-to-speech/api/v1/voices"
以上代表用于檢索全部可用于服務(wù)的語音列表。其中提供的信息包括語音名稱、語種以及性別等等。要了解關(guān)于特定語音的信息,請使用“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."
- },
- . . .
- ]
- }
感興趣的朋友可以點擊此處參閱沃森開發(fā)者云之上的API參考資料,從而了解更多與TTS API調(diào)用相關(guān)的知識
如何將TTS集成至我的Android原生應(yīng)用?
這要求我們將TTS的Gradle條目添加至build.gradle(應(yīng)用)文件當(dāng)中:
- 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文件內(nèi)添加以下代碼,并將用戶名與密碼占位符替換為實際TTS服務(wù)憑據(jù)。另外,在添加以下代碼后,點觸一段消息即可將文本轉(zhuǎn)換為語音:
- 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譯稿,合作站點轉(zhuǎn)載請注明原文譯者和出處為51CTO.com】