当前位置: 首页 > news >正文

HarmonyOS开发-系统AI能力-语音转文字

HarmonyOS中语音转文字

前言

在鸿蒙应用开发中,往往会用到语音播报,语音转文字等功能,那么华为HarmonyOS系统集成了基础语音服务的sdk(Core Speech Kit),这个能力由HarmonyOS系统提供,是一个本地的语音服务能力,需要注意的是,这个能力仅支持HarmonyOS,OpenHarmony系统中是没有的,那么下面我们就对这个能力进行讲解,本文主要讲文本转语音的使用方法

介绍

本文主要介绍华为HarmonyOS基础语音服务(Core Speech Kit)中文本转语音的功能开发顺序,通过对Core Speech Kit的使用,开发者可对播报的策略进行设置,包括单词播报、数字播报、静音停顿、汉字发音策略。

  • 支持的语种类型:中文、英文。(简体中文、繁体中文、中文语境下的英文)
  • 支持的音色类型:聆小珊女声音色、英语(美国)劳拉女声音色、凌飞哲男声音色。

开发步骤:

第一步需要引入对应的工具类

import { textToSpeech } from '@kit.CoreSpeechKit';

第二步创建文字转语音引擎,并且设置对应的引擎参数,引擎参数列表下面我也列出来了

let ttsEngine: textToSpeech.TextToSpeechEngine;
// 设置创建引擎参数
let extraParam: Record<string, Object> = {"style": 'interaction-broadcast', "locate": 'CN', "name": 'EngineName'};
let initParamsInfo: textToSpeech.CreateEngineParams = {language: 'zh-CN',person: 0,online: 1,extraParams: extraParam
};

语音引擎参数

名称类型只读可选说明
languagestring语种,当前支持中文、英文。中文推荐使用“zh-CN”格式,兼容“zh_CN”格式。英文推荐使用“en-US”格式,兼容“en_US”格式。
onlinenumber模式。0为在线,目前不支持;1为离线,当前仅支持离线模式。
personnumber音色。中文:13为聆小珊女声音色(推荐使用13,同时支持0);21为凌飞哲男声音色(需下载)。英文:8为英语(美国)劳拉女声音色(需下载)。
extraParamsRecord<string, Object>- <‘style’, string> 风格。

可选,不设置时默认为“interaction-broadcast”,当前仅支持“interaction-broadcast”。interaction-broadcast:广播风格。

- <‘locate’, string> 区域信息。

可选,不设置时默认为“CN”,当前仅支持“CN”。CN:中国。

- <‘name’, string> 引擎名称。

可选,引擎名称,不可以是随机数,不设置时默认为空,当前支持多应用、多实例,同一个设备上所有应用一共最多支持3个实例。

- <‘isBackStage’, boolean> 是否支持后台播报。

可选,不设置时默认不支持后台播报。设置’isBackStage’: true时,TTS支持后台播报。

第三步调用createEngine方法,创建引擎

textToSpeech.createEngine(initParamsInfo, (err: BusinessError, textToSpeechEngine: textToSpeech.TextToSpeechEngine) => {if (!err) {console.info('Succeeded in creating engine');// 接收创建引擎的实例ttsEngine = textToSpeechEngine;} else {console.error(`Failed to create engine. Code: ${err.code}, message: ${err.message}.`);}
});

第四步,我们创建一个语音播报监听的回调对象

// 设置speak的回调信息
let speakListener: textToSpeech.SpeakListener = {// 开始播报回调onStart(requestId: string, response: textToSpeech.StartResponse) {console.info(`onStart, requestId: ${requestId} response: ${JSON.stringify(response)}`);},// 合成完成及播报完成回调onComplete(requestId: string, response: textToSpeech.CompleteResponse) {console.info(`onComplete, requestId: ${requestId} response: ${JSON.stringify(response)}`);},// 停止播报回调onStop(requestId: string, response: textToSpeech.StopResponse) {console.info(`onStop, requestId: ${requestId} response: ${JSON.stringify(response)}`);},// 返回音频流onData(requestId: string, audio: ArrayBuffer, response: textToSpeech.SynthesisResponse) {console.info(`onData, requestId: ${requestId} sequence: ${JSON.stringify(response)} audio: ${JSON.stringify(audio)}`);},// 错误回调onError(requestId: string, errorCode: number, errorMessage: string) {console.error(`onError, requestId: ${requestId} errorCode: ${errorCode} errorMessage: ${errorMessage}`);}
};

第五步 设置回调,播报参数,调用播报功能

// 设置回调
ttsEngine.setListener(speakListener);
let originalText: string = 'Hello HarmonyOS';
// 设置播报相关参数
let extraParam: Record<string, Object> = {"queueMode": 0, "speed": 1, "volume": 2, "pitch": 1, "languageContext": 'zh-CN',  
"audioType": "pcm", "soundChannel": 3, "playType": 1 };
let speakParams: textToSpeech.SpeakParams = {requestId: '123456', // requestId在同一实例内仅能用一次,请勿重复设置extraParams: extraParam
};
// 调用播报方法
// 开发者可以通过修改speakParams主动设置播报策略
ttsEngine.speak(originalText, speakParams);

至此,我们整个文字转语音功能就完成了

以下为完成代码示例,代码可以直接复制运行

import { textToSpeech } from '@kit.CoreSpeechKit';
import { BusinessError } from '@kit.BasicServicesKit';let extraParam: Record<string, Object> = {"style": 'interaction-broadcast', "locate": 'CN', "name": 'EngineName'};
let ttsEngine: textToSpeech.TextToSpeechEngine;
// 设置创建引擎参数let initParamsInfo: textToSpeech.CreateEngineParams = {language: 'zh-CN',person: 0,online: 1,extraParams: extraParam
};textToSpeech.createEngine(initParamsInfo, (err: BusinessError, textToSpeechEngine: textToSpeech.TextToSpeechEngine) => {if (!err) {console.info('Succeeded in creating engine');// 接收创建引擎的实例ttsEngine = textToSpeechEngine;} else {console.error(`Failed to create engine. Code: ${err.code}, message: ${err.message}.`);}
});// 设置speak的回调信息
let speakListener: textToSpeech.SpeakListener = {// 开始播报回调onStart(requestId: string, response: textToSpeech.StartResponse) {console.info(`onStart, requestId: ${requestId} response: ${JSON.stringify(response)}`);},// 合成完成及播报完成回调onComplete(requestId: string, response: textToSpeech.CompleteResponse) {console.info(`onComplete, requestId: ${requestId} response: ${JSON.stringify(response)}`);},// 停止播报回调onStop(requestId: string, response: textToSpeech.StopResponse) {console.info(`onStop, requestId: ${requestId} response: ${JSON.stringify(response)}`);},// 返回音频流onData(requestId: string, audio: ArrayBuffer, response: textToSpeech.SynthesisResponse) {console.info(`onData, requestId: ${requestId} sequence: ${JSON.stringify(response)} audio: ${JSON.stringify(audio)}`);},// 错误回调onError(requestId: string, errorCode: number, errorMessage: string) {console.error(`onError, requestId: ${requestId} errorCode: ${errorCode} errorMessage: ${errorMessage}`);}
};
let originalText: string = '恭喜啊,你已经完成了文字转语音的功能';
// 设置播报相关参数let speakParams: textToSpeech.SpeakParams = {requestId: '123456', // requestId在同一实例内仅能用一次,请勿重复设置extraParams: extraParam
};@Entry
@Component
struct Index{build() {Column(){Button("播放").onClick(()=>{ttsEngine.setListener(speakListener);ttsEngine.speak(originalText, speakParams);})}}
}

欢迎大家跟我一起学习鸿蒙开发知识,加入我的班级,参与HarmonyOS赋能资源丰富度建设(第四期)-夏文强,获得HarmonyOS应用开发者认证 每月对前200名学员进行激励,活动期间共计激励1000名

华为开发者学堂

http://www.dtcms.com/a/577403.html

相关文章:

  • 巨鹿企业做网站儋州网站建设培训学校
  • 建站优化收费下载网页图片
  • Docker搭建Ngnix、php5.6、php8、postgresql、redis
  • php基础-系统函数-第15天
  • CSP-J教程——第一阶段——第五课:程序流程控制 - 选择结构
  • 【Go微服务框架深度对比】Kratos、Go-Zero、Go-Micro、GoFrame、Sponge五大框架
  • 基于FPGA实现Mini-LVDS转LVDS
  • 做网站的是如何赚钱的哪个小说网站版权做的好处
  • Cache的基本原理
  • 如何提高外贸网站排名南京高端定制网站建设
  • 建网站需要多久网站模板怎么制作
  • 计算机网络:基于TCP协议的自定义协议实现网络计算器功能
  • SpringBoot3+ApolloClient2.3.0集成Apollo2.4.0示例
  • UDP的recvfrom会返回一个完整的数据报
  • Rust实战教程:做一个UDP聊天软件
  • 基于遥感解译与GIS技术生态环境影响评价图件制作
  • 用asp制作一个简单的网站零基础学电脑培训班
  • 广东如何进行网站制作排名做网站在哪里买空间域名
  • 数据结构(长期更新)第6讲:双向链表
  • Debian系统的多内核共存
  • HTTPS 请求抓包,从原理到落地排查的工程化指南(Charles / tcpdump / Wireshark / Sniffmaster)
  • Debian 12 笔记本合盖不休眠设置指南
  • 线性代数 - 奇异值分解(SVD Singular Value Decomposition)- 奇异值在哪里
  • 商城网站开发的完整流程图视频制作价格明细
  • 如何保证Redis和Mysql数据缓存一致性?
  • 八股-Mysql 基础篇(1)
  • 建设公司网站需要准备什么科目苏州建网站的公司
  • Git 某个分支恢复到某个特定的 commit 状态
  • 【Prompt学习技能树地图】生成知识提示技术的深度解析与应用
  • 家用电脑怎么做网站服务器创意设计之都