HarmonyOS AI基礎技術賦能之語音播報
作者:軟通動力HOS
在實際應用開發中,時不時的會遇到AI領域相關的一些技術,本節主要詳細講述一下語音播報技術,語音播報可能涉及的領域,如:實時語音交互、超長文本播報等。
引言
在實際應用開發中,時不時的會遇到AI領域相關的一些技術,本節主要詳細講述一下語音播報技術,語音播報可能涉及的領域,如:實時語音交互、超長文本播報等。對于HarmonyOS開發者而言,也需要了解和掌握HarmonyOS AI領域相關技術能力。
功能介紹
語音播報主要是基于華為智慧引擎(HUAWEI HiAI Engine)中的語音播報引擎,向開發者提供人工智能應用層API。該技術提供將文本轉換為語音并進行播報的能力。
指南
1、創建與TTS服務的連接。context為應用上下文信息,應為ohos.aafwk.ability.Ability或ohos.aafwk.ability.AbilitySlice的實例或子類實例。
- 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接口創建成功后初始化TTS引擎
- TtsParams ttsParams = new TtsParams();
- ttsParams.setDeviceId("deviceId");
- boolean initResult = TtsClient.getInstance().init(ttsParams);
3、初始化TTS引擎成功后調用音頻轉換并播放接口
- if (initResult) {
- TtsClient.getInstance().speakText("歡迎使用語音播報!", null);
- }
4、使用完成后銷毀TTS客戶端
- 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語音播報"
- 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="某軟件公司是中國領先的軟件與信息技術服務商,企業數字轉型可信賴合作伙伴。公司2001年成立于北京,立足中國,服務全球市場。經過18年發展,目前公司在全球43個城市設有90多個分支機構26個全球交付中心,員工總數近60000人。該軟件公司擁有深厚的行業積累和領先的技術實力,可以為客戶提供端到端的數字化產品和服務,包括數字化咨詢與解決方案、云智能與基礎設施、軟件與技術服務和數字化運營等;在10余個重要行業服務超過1000家國內外客戶,其中世界500強企業客戶超過110家,為各領域客戶創造價值。"
- 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="語音播報"
- ohos:text_size="16fp"/>
- <Text
- ohos:id="$+id:time"
- ohos:height="35vp"
- ohos:width="150vp"
- ohos:margin="15vp"
- ohos:text="播報耗時: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, "播報耗時:" + time + " s");
- timeText.setText("播報耗時:" + 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);//語速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客戶端創建成功的回調函數
- 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) {
- // 開始計時
- 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) {
- // 結束計時
- HiLog.info(LABEL_LOG, "onSpeechFinish...");
- timer.cancel();
- time = 0;
- timer = null;
- timerTask = null;
- }
- };
- }
實現效果:

責任編輯:jianghua
來源:
鴻蒙社區