自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

HarmonyOS AI基礎(chǔ)技術(shù)賦能之語(yǔ)音播報(bào)

開(kāi)發(fā) OpenHarmony
在實(shí)際應(yīng)用開(kāi)發(fā)中,時(shí)不時(shí)的會(huì)遇到AI領(lǐng)域相關(guān)的一些技術(shù),本節(jié)主要詳細(xì)講述一下語(yǔ)音播報(bào)技術(shù),語(yǔ)音播報(bào)可能涉及的領(lǐng)域,如:實(shí)時(shí)語(yǔ)音交互、超長(zhǎng)文本播報(bào)等。

[[425058]]

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

引言

在實(shí)際應(yīng)用開(kāi)發(fā)中,時(shí)不時(shí)的會(huì)遇到AI領(lǐng)域相關(guān)的一些技術(shù),本節(jié)主要詳細(xì)講述一下語(yǔ)音播報(bào)技術(shù),語(yǔ)音播報(bào)可能涉及的領(lǐng)域,如:實(shí)時(shí)語(yǔ)音交互、超長(zhǎng)文本播報(bào)等。對(duì)于HarmonyOS開(kāi)發(fā)者而言,也需要了解和掌握HarmonyOS AI領(lǐng)域相關(guān)技術(shù)能力。

功能介紹

語(yǔ)音播報(bào)主要是基于華為智慧引擎(HUAWEI HiAI Engine)中的語(yǔ)音播報(bào)引擎,向開(kāi)發(fā)者提供人工智能應(yīng)用層API。該技術(shù)提供將文本轉(zhuǎn)換為語(yǔ)音并進(jìn)行播報(bào)的能力。

指南

1、創(chuàng)建與TTS服務(wù)的連接。context為應(yīng)用上下文信息,應(yīng)為ohos.aafwk.ability.Ability或ohos.aafwk.ability.AbilitySlice的實(shí)例或子類(lèi)實(shí)例。

  1. private static final TtsListener ttsListener = new TtsListener() { 
  2.         @Override 
  3.         public void onEvent(int eventType, PacMap pacMap) { 
  4.             // Log.info("onEvent:" + eventType); 
  5.             if (eventType == TtsEvent.CREATE_TTS_CLIENT_SUCCESS) { 
  6.                 // Log.info("TTS Client create success"); 
  7.             } 
  8.         } 
  9.         @Override 
  10.         public void onStart(String utteranceId) { 
  11.             // Log.info(utteranceId + " audio synthesis begins"); 
  12.         } 
  13.         @Override 
  14.         public void onProgress(String utteranceId, byte[] audioData, int progress) { 
  15.             // Log.info(utteranceId + " audio synthesis progress:" + progress); 
  16.         } 
  17.         @Override 
  18.         public void onFinish(String utteranceId) { 
  19.             // Log.info(utteranceId + " audio synthesis completed"); 
  20.         } 
  21.         @Override 
  22.         public void onSpeechStart(String utteranceId) { 
  23.             // Log.info(utteranceId + " begins to speech"); 
  24.         } 
  25.         @Override 
  26.         public void onSpeechProgressChanged(String utteranceId, int progress) { 
  27.             // Log.info(utteranceId + " speech progress:" + progress); 
  28.         } 
  29.         @Override 
  30.         public void onSpeechFinish(String utteranceId) { 
  31.             // Log.info(utteranceId + " speech completed"); 
  32.         } 
  33.         @Override 
  34.         public void onError(String utteranceId, String errorMessage) { 
  35.             // Log.info(utteranceId + " errorMessage: " + errorMessage); 
  36.         } 
  37. }; 
  38. TtsClient.getInstance().create(context, ttsListener); 

2、在TTS接口創(chuàng)建成功后初始化TTS引擎

  1. TtsParams ttsParams = new TtsParams(); 
  2. ttsParams.setDeviceId("deviceId"); 
  3. boolean initResult = TtsClient.getInstance().init(ttsParams); 

3、初始化TTS引擎成功后調(diào)用音頻轉(zhuǎn)換并播放接口

  1. if (initResult) { 
  2.  TtsClient.getInstance().speakText("歡迎使用語(yǔ)音播報(bào)!"null); 

4、使用完成后銷(xiāo)毀TTS客戶(hù)端

  1. TtsClient.getInstance().destroy(); 

示例代碼

1、xml布局

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <DirectionalLayout 
  3.   xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  4.   ohos:height="match_parent" 
  5.   ohos:width="match_parent" 
  6.   ohos:orientation="vertical"
  7.     <Text 
  8.       ohos:height="match_content" 
  9.       ohos:width="match_content" 
  10.       ohos:margin="15vp" 
  11.       ohos:text="AI語(yǔ)音播報(bào)" 
  12.       ohos:text_size="23fp" 
  13.       ohos:top_margin="40vp"/> 
  14.     <TextField 
  15.       ohos:id="$+id:text" 
  16.       ohos:height="300vp" 
  17.       ohos:width="match_content" 
  18.       ohos:layout_alignment="horizontal_center" 
  19.       ohos:left_margin="20vp" 
  20.       ohos:multiple_lines="true" 
  21.       ohos:right_margin="20vp" 
  22.       ohos:text="某軟件公司是中國(guó)領(lǐng)先的軟件與信息技術(shù)服務(wù)商,企業(yè)數(shù)字轉(zhuǎn)型可信賴(lài)合作伙伴。公司2001年成立于北京,立足中國(guó),服務(wù)全球市場(chǎng)。經(jīng)過(guò)18年發(fā)展,目前公司在全球43個(gè)城市設(shè)有90多個(gè)分支機(jī)構(gòu)26個(gè)全球交付中心,員工總數(shù)近60000人。該軟件公司擁有深厚的行業(yè)積累和領(lǐng)先的技術(shù)實(shí)力,可以為客戶(hù)提供端到端的數(shù)字化產(chǎn)品和服務(wù),包括數(shù)字化咨詢(xún)與解決方案、云智能與基礎(chǔ)設(shè)施、軟件與技術(shù)服務(wù)和數(shù)字化運(yùn)營(yíng)等;在10余個(gè)重要行業(yè)服務(wù)超過(guò)1000家國(guó)內(nèi)外客戶(hù),其中世界500強(qiáng)企業(yè)客戶(hù)超過(guò)110家,為各領(lǐng)域客戶(hù)創(chuàng)造價(jià)值。" 
  23.       ohos:text_size="50" 
  24.       ohos:top_margin="20vp" 
  25.       /> 
  26.     <DirectionalLayout 
  27.       xmlns:ohos="http://schemas.huawei.com/res/ohos" 
  28.       ohos:height="match_parent" 
  29.       ohos:width="match_parent" 
  30.       ohos:orientation="horizontal"
  31.         <Button 
  32.           ohos:id="$+id:read_btn" 
  33.           ohos:height="35vp" 
  34.           ohos:width="80vp" 
  35.           ohos:background_element="$graphic:background_button" 
  36.           ohos:margin="15vp" 
  37.           ohos:text="語(yǔ)音播報(bào)" 
  38.           ohos:text_size="16fp"/> 
  39.         <Text 
  40.           ohos:id="$+id:time" 
  41.           ohos:height="35vp" 
  42.           ohos:width="150vp" 
  43.           ohos:margin="15vp" 
  44.           ohos:text="播報(bào)耗時(shí):0 s" 
  45.           ohos:text_size="16fp"/> 
  46.     </DirectionalLayout> 
  47. </DirectionalLayout> 

 2、案例代碼

  1. package com.isoftstone.tts.slice; 
  2.  
  3. import com.isoftstone.tts.ResourceTable; 
  4. import ohos.aafwk.ability.AbilitySlice; 
  5. import ohos.aafwk.content.Intent; 
  6. import ohos.agp.components.Button; 
  7. import ohos.agp.components.Component; 
  8. import ohos.agp.components.Text; 
  9. import ohos.agp.components.TextField; 
  10. import ohos.ai.tts.TtsClient; 
  11. import ohos.ai.tts.TtsListener; 
  12. import ohos.ai.tts.TtsParams; 
  13. import ohos.ai.tts.constants.TtsEvent; 
  14. import ohos.eventhandler.EventHandler; 
  15. import ohos.eventhandler.EventRunner; 
  16. import ohos.eventhandler.InnerEvent; 
  17. import ohos.hiviewdfx.HiLog; 
  18. import ohos.hiviewdfx.HiLogLabel; 
  19. import ohos.utils.PacMap; 
  20. import java.util.Timer; 
  21. import java.util.TimerTask; 
  22. import java.util.UUID; 
  23.  
  24. public class MainAbilitySlice extends AbilitySlice { 
  25.     private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "MainAbilitySlice"); 
  26.     private TextField infoText; 
  27.     private Text timeText; 
  28.     private boolean initItsResult; 
  29.     private static final int EVENT_MSG_TIME_COUNT = 0x1000002; 
  30.     private int time = 0; 
  31.     private Timer timer = null
  32.     private TimerTask timerTask = null
  33.  
  34.     private EventHandler handler = new EventHandler(EventRunner.current()) { 
  35.         @Override 
  36.         protected void processEvent(InnerEvent event) { 
  37.             switch (event.eventId) { 
  38.                 case EVENT_MSG_TIME_COUNT: 
  39.                     getUITaskDispatcher().delayDispatch(() -> { 
  40.                         time = time + 1; 
  41.                         HiLog.info(LABEL_LOG, "播報(bào)耗時(shí):" + time + " s"); 
  42.                         timeText.setText("播報(bào)耗時(shí):" + time + " s"); 
  43.                     }, 0); 
  44.                     break; 
  45.                 default
  46.                     break; 
  47.             } 
  48.         } 
  49.     }; 
  50.  
  51.     @Override 
  52.     public void onStart(Intent intent) { 
  53.         super.onStart(intent); 
  54.         super.setUIContent(ResourceTable.Layout_ability_main); 
  55.         initView(); 
  56.         initTtsEngine(); 
  57.     } 
  58.  
  59.     private void initView() { 
  60.         infoText = (TextField) findComponentById(ResourceTable.Id_text); 
  61.         Button readBtn = (Button) findComponentById(ResourceTable.Id_read_btn); 
  62.         timeText = (Text) findComponentById(ResourceTable.Id_time); 
  63.         readBtn.setClickedListener(this::readText); 
  64.     } 
  65.  
  66.     private void initTtsEngine() { 
  67.         TtsClient.getInstance().create(this, ttsListener); 
  68.     } 
  69.  
  70.     private void readText(Component component) { 
  71.         if (initItsResult) { 
  72.             TtsParams ttsParams = new TtsParams(); 
  73.             ttsParams.setSpeed(0);//語(yǔ)速0~15越大越快 
  74.             TtsClient.getInstance().setParams(ttsParams); 
  75.             HiLog.info(LABEL_LOG, "initItsResult is true, speakText"); 
  76.             TtsClient.getInstance().speakText(infoText.getText(), null); 
  77.         } else { 
  78.             HiLog.error(LABEL_LOG, "initItsResult is false"); 
  79.         } 
  80.     } 
  81.  
  82.     private TtsListener ttsListener = new TtsListener() { 
  83.         @Override 
  84.         public void onEvent(int eventType, PacMap pacMap) { 
  85.             HiLog.info(LABEL_LOG, "onEvent..."); 
  86.             // 定義TTS客戶(hù)端創(chuàng)建成功的回調(diào)函數(shù) 
  87.             if (eventType == TtsEvent.CREATE_TTS_CLIENT_SUCCESS) { 
  88.                 TtsParams ttsParams = new TtsParams(); 
  89.                 ttsParams.setDeviceId(UUID.randomUUID().toString()); 
  90.                 initItsResult = TtsClient.getInstance().init(ttsParams); 
  91.             } 
  92.         } 
  93.         @Override 
  94.         public void onStart(String utteranceId) { 
  95.             HiLog.info(LABEL_LOG, "onStart..."); 
  96.         } 
  97.         @Override 
  98.         public void onProgress(String utteranceId, byte[] audioData, int progress) { 
  99.         } 
  100.         @Override 
  101.         public void onFinish(String utteranceId) { 
  102.             HiLog.info(LABEL_LOG, "onFinish..."); 
  103.         } 
  104.         @Override 
  105.         public void onError(String s, String s1) { 
  106.             HiLog.info(LABEL_LOG, "onError..."); 
  107.         } 
  108.         @Override 
  109.         public void onSpeechStart(String utteranceId) { 
  110.             // 開(kāi)始計(jì)時(shí) 
  111.             HiLog.info(LABEL_LOG, "onSpeechStart..."); 
  112.             if (timer == null && timerTask == null) { 
  113.                 timer = new Timer(); 
  114.                 timerTask = new TimerTask() { 
  115.                     public void run() { 
  116.                         handler.sendEvent(EVENT_MSG_TIME_COUNT); 
  117.                     } 
  118.                 }; 
  119.                 timer.schedule(timerTask, 0, 1000); 
  120.             } 
  121.         } 
  122.         @Override 
  123.         public void onSpeechProgressChanged(String utteranceId, int progress) { 
  124.         } 
  125.         @Override 
  126.         public void onSpeechFinish(String utteranceId) { 
  127.             // 結(jié)束計(jì)時(shí) 
  128.             HiLog.info(LABEL_LOG, "onSpeechFinish..."); 
  129.             timer.cancel(); 
  130.             time = 0; 
  131.             timer = null
  132.             timerTask = null
  133.         } 
  134.     }; 

實(shí)現(xiàn)效果:

HarmonyOS AI基礎(chǔ)技術(shù)賦能之語(yǔ)音播報(bào)-鴻蒙HarmonyOS技術(shù)社區(qū)

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):

51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)

https://harmonyos.51cto.com

 

責(zé)任編輯:jianghua 來(lái)源: 鴻蒙社區(qū)
相關(guān)推薦

2021-08-26 09:50:06

鴻蒙HarmonyOS應(yīng)用

2021-09-13 15:14:01

鴻蒙HarmonyOS應(yīng)用

2021-08-31 14:58:52

鴻蒙HarmonyOS應(yīng)用

2021-09-03 15:27:17

鴻蒙HarmonyOS應(yīng)用

2021-12-24 10:34:11

鴻蒙HarmonyOS應(yīng)用

2022-02-17 17:19:31

鴻蒙語(yǔ)音識(shí)別語(yǔ)音播報(bào)

2021-01-14 15:07:33

人工智能游戲網(wǎng)絡(luò)

2021-08-27 09:57:18

鴻蒙HarmonyOS應(yīng)用

2018-09-18 11:47:17

2020-12-14 09:23:16

人工智能教育機(jī)器人

2020-04-27 10:39:19

人工智能技術(shù)語(yǔ)音識(shí)別

2021-06-24 13:20:29

人工智能AI

2022-08-25 13:19:31

5G人工智能無(wú)線技術(shù)

2021-07-14 17:25:59

AI/網(wǎng)絡(luò)/體驗(yàn)至上

2023-05-26 01:01:06

AI幻燈片人工智能
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)