poweredbyHarmonyOSAI基础技术更多内容请访问:Harmonyos技术社区https://harmonyos.51cto.com,与华为官方共同建立遇到AI领域相关的一些技术,本版块主要详细描述了语音播报技术,以及语音播报可能涉及的领域,如:实时语音交互、超长文本播报等。对于HarmonyOS开发者来说,也需要了解和掌握相关技术能力在HarmonyOSAI领域。功能介绍语音播报主要基于HUAWEIHiAIEngine中的语音播报引擎,为开发者提供AI应用层API。该技术提供了将文本转换为语音并进行广播的能力。准则1.创建与TTS服务的连接。context为应用上下文信息,应该是ohos.aafwk.ability.Ability或ohos.aafwk.ability.AbilitySlice的实例或子类实例。privatestaticfinalTtsListenertsListener=newTtsListener(){@OverridepublicvoidonEvent(inteventType,PacMappacMap){//Log.info("onEvent:"+eventType);if(eventType==TtsEvent.CREATE_TTS_CLIENT_SUCCESS){//Log.info("TTSClientcreatesuccess");}}@OverridepublicvoidonStart(StringutteranceId){//Log.info(utteranceId+"audiosynthesisbegins");}@OverridepublicvoidonProgress(StringutteranceId,byte[]audioData,intprogress){//Log.info(utteranceId+"audiosynthesisprogress:"+progress);}@OverridepublicvoidonFinish(StringutteranceId){//Log.info(utteranceId+"audiosynthesiscompleted");}@OverridepublicvoidonSpeechStart(StringutteranceId){//Log.info(utteranceId+"beginstospeech");}@OverridepublicvoidonSpeechProgressChanged(StringutteranceId,intprogress){//Log.info(utteranceId+"speechprogress:"+progress);}@OverridepublicvoidonSpeechFinish(StringutteranceId){//Log.info(utteranceId+"speechcompleted");}@OverridepublicvoidonError(StringutteranceId,StringerrorMessage){//Log.info(utteranceId+"errorMessage:"+errorMessage);}};TtsClient.getInstance().create(context,ttsListener);2.TTS接口创建成功后初始化Tts引擎TtsParamsttsParams=newTtsParams();ttsParams.setDeviceId("deviceId");booleaninitResult=TtsClient.getInstance().init(ttsParams);3、TTS引擎初始化成功后,调用音频转换播放接口.getInstance().destroy();示例代码1.xml布局<TextFieldohos:id="$+id:text"ohos:height="300vp"ohos:width="match_content"ohos:layout_alignment="horizo??ntal_center"ohos:left_margin="20vp"ohos:multiple_lines="true"ohos:right_margin="20vp"ohos:text="某软件公司是中国领先的软件和信息技术服务商,是企业数字化转型值得信赖的合作伙伴。公司于2001年成立于北京,立足中国,服务于全球市场,经过18年的发展,目前公司在全球43个城市拥有90多家分支机构和26个全球交付中心,员工总数近60000人,软件公司拥有深厚的行业积累和领先的技术实力,并能为客户提供端到端的数字化产品与服务,包括数字化咨询与解决方案、云智能与基础设施、软件与技术服务、数字化运营等;服务于1000多家国内外客户10多个重要行业,包括世界500强企业的110多家客户。为现场客户创造价值。"ohos:text_size="50"ohos:top_margin="20vp"/>2、案例代码packagecom.isoftstone.tts.slice;importcom.isoftstone.tts.ResourceTable;importohos.aafwk.ability.AbilitySlice;importohos.aafwk.content.Intent;importohos.agp.components.Button;importohos.agp.components.Component;importohos.agp.components.Text;importohos.agp.components.TextField;importohos.ai.tts.TtsClient;importohos.ai.tts.TtsListener;importohos.ai.tts.TtsParams;importohos.ai.tts.constants。TtsEvent;importohos.eventhandler.EventHandler;importohos.eventhandler.EventRunner;importohos.eventhandler.InnerEvent;importohos.hiviewdfx.HiLog;importohos.hiviewdfx.HiLogLabel;importohos.utils.PacMap;importjava.util.Timer;importjava.util.TimerTask;importjava.util.UUID;publicclassMainAbilitySliceextendsAbilitySlice{privatestaticfinalHiLogLabelLABEL_LOG=newHiLogLabel(3,0xD001100,"MainAbilitySlice");privateTextFieldinfoText;privateTexttimeText;privatebooleaninitItsResult;privatestaticfinalintEVENT_MSG_TIME_COUNT=0x1000002;privateinttime=0;privateTimertimer=null;privateTimerTasktimerTask=null;privateEventHandlerhandler=newEventHandler(EventRunner.current()){@OverrideprotectedvoidprocessEvent(InnerEventevent){开关(event.eventId){caseEVENT_MSG_TIME_COUNT:getUITaskDispatcher().delayDispatch(()->{time=time+1;HiLog.info(LABEL_LOG,"播报时间:"+time+"s");timeText.setText("播报时间:"+time+"s");},0);break;default:break;}}};@OverridepublicvoidonStart(Intentintent){super.onStart(intent);super.setUIContent(ResourceTable.Layout_ability_main);initView();initTtsEngine();}privatevoidinitView(){infoText=(TextField)findComponentById(ResourceTable.Id_text);ButtonreadBtn=(Button)findComponentById(ResourceTable.Id_read_btn);timeText=(Text)findComponentById(ResourceTable.Id_time);readBtn.setClickedListener(this::readText);}privatevoidinitTtsEngine(){TtsClient.getInstance().create(this,ttsListener);}privatevoidreadText(Componentcomponent){if(initItsResult){TtsParamsttsParams=newTtsParams();ttsParams.setSpeed(0);//语速0~15越大越快TtsClient.getInstance().setParams(ttsParams);HiLog.info(LABEL_LOG,"initItsResultistrue,speakText");TtsClient.getInstance().speakText(infoText.getText(),null);}else{HiLog.error(LABEL_LOG,"initItsResultisfalse");}}privateTtsListenerttsListener=newTtsListener(){@OverridepublicvoidonEvent(inteventType,PacMappacMap){HiLog.info(LABEL_LOG,"onEvent...");//定义TTS客户端创建成功的回复数if(eventType==TtsEvent.CREATE_TTS_CLIENT_SUCCESS){TtsParamsttsParams=newTtsParams();ttsParams.setDeviceId(UUID.randomUUID().toString());initItsResult=tts.Clients();}}@OverridepublicvoidonStart(StringutteranceId){HiLog.info(LABEL_LOG,"onStart...");}@OverridepublicvoidonProgress(StringutteranceId,byte[]audioData,intprogress){}@OverridepublicvoidonFinish(StringutteranceId){HiLog.info(LABEL_LOG,"onFinish...");}@OverridepublicvoidonError(Strings,Strings1){HiLog.info(LABEL_LOG,"onError...");}@OverridepublicvoidonSpeechStart(StringutteranceId){//开始计算时HiLog.info(LABEL_LOG,"onSpeechStart...");if(timer==null&&timerTask==null){timer=newTimer();timerTask=newTimerTask(){publicvoidrun(){handler.sendEvent(EVENT_MSG_TIME_COUNT);}};timer.schedule(timerTask,0,1000);}}@OverridepublicvoidonSpeechProgressChanged(StringutteranceId,intprogress){}@OverridepublicvoidonSpeechFinish(StringutteranceId){//endtimingHiLog.info(LABEL_LOG,"onSpeechFinish...");timer.cancel();time=0;timer=null;timerTask=null;}};}实现效果:更多信息请访问:和华为Harmonyos官方技术社区https://harmonyos.51cto.com