HarmonyOS AI基礎(chǔ)技術(shù)賦能之語(yǔ)音播報(bào)
想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)
引言
在實(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í)例。
- private static final TtsListener ttsListener = new TtsListener() {
- @Override
- public void onEvent(int eventType, PacMap pacMap) {
- // Log.info("onEvent:" + eventType);
- if (eventType == TtsEvent.CREATE_TTS_CLIENT_SUCCESS) {
- // Log.info("TTS Client create success");
- }
- }
- @Override
- public void onStart(String utteranceId) {
- // Log.info(utteranceId + " audio synthesis begins");
- }
- @Override
- public void onProgress(String utteranceId, byte[] audioData, int progress) {
- // Log.info(utteranceId + " audio synthesis progress:" + progress);
- }
- @Override
- public void onFinish(String utteranceId) {
- // Log.info(utteranceId + " audio synthesis completed");
- }
- @Override
- public void onSpeechStart(String utteranceId) {
- // Log.info(utteranceId + " begins to speech");
- }
- @Override
- public void onSpeechProgressChanged(String utteranceId, int progress) {
- // Log.info(utteranceId + " speech progress:" + progress);
- }
- @Override
- public void onSpeechFinish(String utteranceId) {
- // Log.info(utteranceId + " speech completed");
- }
- @Override
- public void onError(String utteranceId, String errorMessage) {
- // Log.info(utteranceId + " errorMessage: " + errorMessage);
- }
- };
- TtsClient.getInstance().create(context, ttsListener);
2、在TTS接口創(chuàng)建成功后初始化TTS引擎
- TtsParams ttsParams = new TtsParams();
- ttsParams.setDeviceId("deviceId");
- boolean initResult = TtsClient.getInstance().init(ttsParams);
3、初始化TTS引擎成功后調(diào)用音頻轉(zhuǎn)換并播放接口
- if (initResult) {
- TtsClient.getInstance().speakText("歡迎使用語(yǔ)音播報(bào)!", null);
- }
4、使用完成后銷(xiāo)毀TTS客戶(hù)端
- TtsClient.getInstance().destroy();
示例代碼
1、xml布局
- <?xml version="1.0" encoding="utf-8"?>
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:orientation="vertical">
- <Text
- ohos:height="match_content"
- ohos:width="match_content"
- ohos:margin="15vp"
- ohos:text="AI語(yǔ)音播報(bào)"
- ohos:text_size="23fp"
- ohos:top_margin="40vp"/>
- <TextField
- ohos:id="$+id:text"
- ohos:height="300vp"
- ohos:width="match_content"
- ohos:layout_alignment="horizontal_center"
- ohos:left_margin="20vp"
- ohos:multiple_lines="true"
- ohos:right_margin="20vp"
- 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à)值。"
- ohos:text_size="50"
- ohos:top_margin="20vp"
- />
- <DirectionalLayout
- xmlns:ohos="http://schemas.huawei.com/res/ohos"
- ohos:height="match_parent"
- ohos:width="match_parent"
- ohos:orientation="horizontal">
- <Button
- ohos:id="$+id:read_btn"
- ohos:height="35vp"
- ohos:width="80vp"
- ohos:background_element="$graphic:background_button"
- ohos:margin="15vp"
- ohos:text="語(yǔ)音播報(bào)"
- ohos:text_size="16fp"/>
- <Text
- ohos:id="$+id:time"
- ohos:height="35vp"
- ohos:width="150vp"
- ohos:margin="15vp"
- ohos:text="播報(bào)耗時(shí):0 s"
- ohos:text_size="16fp"/>
- </DirectionalLayout>
- </DirectionalLayout>
2、案例代碼
- package com.isoftstone.tts.slice;
- import com.isoftstone.tts.ResourceTable;
- import ohos.aafwk.ability.AbilitySlice;
- import ohos.aafwk.content.Intent;
- import ohos.agp.components.Button;
- import ohos.agp.components.Component;
- import ohos.agp.components.Text;
- import ohos.agp.components.TextField;
- import ohos.ai.tts.TtsClient;
- import ohos.ai.tts.TtsListener;
- import ohos.ai.tts.TtsParams;
- import ohos.ai.tts.constants.TtsEvent;
- import ohos.eventhandler.EventHandler;
- import ohos.eventhandler.EventRunner;
- import ohos.eventhandler.InnerEvent;
- import ohos.hiviewdfx.HiLog;
- import ohos.hiviewdfx.HiLogLabel;
- import ohos.utils.PacMap;
- import java.util.Timer;
- import java.util.TimerTask;
- import java.util.UUID;
- public class MainAbilitySlice extends AbilitySlice {
- private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "MainAbilitySlice");
- private TextField infoText;
- private Text timeText;
- private boolean initItsResult;
- private static final int EVENT_MSG_TIME_COUNT = 0x1000002;
- private int time = 0;
- private Timer timer = null;
- private TimerTask timerTask = null;
- private EventHandler handler = new EventHandler(EventRunner.current()) {
- @Override
- protected void processEvent(InnerEvent event) {
- switch (event.eventId) {
- case EVENT_MSG_TIME_COUNT:
- getUITaskDispatcher().delayDispatch(() -> {
- time = time + 1;
- HiLog.info(LABEL_LOG, "播報(bào)耗時(shí):" + time + " s");
- timeText.setText("播報(bào)耗時(shí):" + time + " s");
- }, 0);
- break;
- default:
- break;
- }
- }
- };
- @Override
- public void onStart(Intent intent) {
- super.onStart(intent);
- super.setUIContent(ResourceTable.Layout_ability_main);
- initView();
- initTtsEngine();
- }
- private void initView() {
- infoText = (TextField) findComponentById(ResourceTable.Id_text);
- Button readBtn = (Button) findComponentById(ResourceTable.Id_read_btn);
- timeText = (Text) findComponentById(ResourceTable.Id_time);
- readBtn.setClickedListener(this::readText);
- }
- private void initTtsEngine() {
- TtsClient.getInstance().create(this, ttsListener);
- }
- private void readText(Component component) {
- if (initItsResult) {
- TtsParams ttsParams = new TtsParams();
- ttsParams.setSpeed(0);//語(yǔ)速0~15越大越快
- TtsClient.getInstance().setParams(ttsParams);
- HiLog.info(LABEL_LOG, "initItsResult is true, speakText");
- TtsClient.getInstance().speakText(infoText.getText(), null);
- } else {
- HiLog.error(LABEL_LOG, "initItsResult is false");
- }
- }
- private TtsListener ttsListener = new TtsListener() {
- @Override
- public void onEvent(int eventType, PacMap pacMap) {
- HiLog.info(LABEL_LOG, "onEvent...");
- // 定義TTS客戶(hù)端創(chuàng)建成功的回調(diào)函數(shù)
- if (eventType == TtsEvent.CREATE_TTS_CLIENT_SUCCESS) {
- TtsParams ttsParams = new TtsParams();
- ttsParams.setDeviceId(UUID.randomUUID().toString());
- initItsResult = TtsClient.getInstance().init(ttsParams);
- }
- }
- @Override
- public void onStart(String utteranceId) {
- HiLog.info(LABEL_LOG, "onStart...");
- }
- @Override
- public void onProgress(String utteranceId, byte[] audioData, int progress) {
- }
- @Override
- public void onFinish(String utteranceId) {
- HiLog.info(LABEL_LOG, "onFinish...");
- }
- @Override
- public void onError(String s, String s1) {
- HiLog.info(LABEL_LOG, "onError...");
- }
- @Override
- public void onSpeechStart(String utteranceId) {
- // 開(kāi)始計(jì)時(shí)
- HiLog.info(LABEL_LOG, "onSpeechStart...");
- if (timer == null && timerTask == null) {
- timer = new Timer();
- timerTask = new TimerTask() {
- public void run() {
- handler.sendEvent(EVENT_MSG_TIME_COUNT);
- }
- };
- timer.schedule(timerTask, 0, 1000);
- }
- }
- @Override
- public void onSpeechProgressChanged(String utteranceId, int progress) {
- }
- @Override
- public void onSpeechFinish(String utteranceId) {
- // 結(jié)束計(jì)時(shí)
- HiLog.info(LABEL_LOG, "onSpeechFinish...");
- timer.cancel();
- time = 0;
- timer = null;
- timerTask = null;
- }
- };
- }
實(shí)現(xiàn)效果:

想了解更多內(nèi)容,請(qǐng)?jiān)L問(wèn):
51CTO和華為官方合作共建的鴻蒙技術(shù)社區(qū)