HarmonyOS 鸿蒙Next开发中,如何实现文本合成语音

HarmonyOS 鸿蒙Next开发中,如何实现文本合成语音 Android开发的同学都知道,在Android当中,实现一段文字合成语音播放,可以使用系统提供的对象TextToSpeech来很快的实现,如果不用系统自带的,也可以使用三方提供的,比如讯飞的语音合成等等,总之,实现起来多种多样,那么,在鸿蒙当中,如何实现根据指定的文本进行合成语音合成播放呢,其实也是非常的简单,因为鸿蒙当中也有textToSpeech。

3 回复

实现步骤

第一步:创建引擎得到文本转语音类

使用系统自带的Api textToSpeech,调用createEngine方法来创建引擎,接收的参数,用来设置创建引擎实例的相关参数,比如配置的语种、模式、音色和风格等。

private createTextToSpeech() {
    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
    }

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

关于参数,需要注重说明一下,首先是language,也就是支持的语言,目前只支持中文,也就是“zh-CN”;online字段是支持的模式,0为在线模式,目前不支持;1为离线,当前仅支持离线模式;person,音色,0为聆小珊女声音色,当前仅支持聆小珊女声音色;extraParams为风格设置,比如设置广播风格,是否支持后台播放等。

可以发现,虽然有很多参数,但是目前都仅仅支持一种,基本上固定就行,extraParams中有一个isBackStage,也就是是否支持后台播放,为true时,支持后台播报。

第二步,语音播放

文字合成语音,我们只需要调用speak方法即可,两个参数,第一个是需要合成语音的文本,要求是不超过10000字符的中文文本,这个是重点,第二个是,合成播报音频的相关参数,用于配置语速、音量、音调、合成类型等,其中有一个参数requestId需要知道,它在同一实例内仅能用一次,重复设置是不起作用的,如果多次调用,建议每次进行更换,比如用时间戳,随机数等等。

 // 调用speak播报方法
  private speak(message:string) {
    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)}`);
      },
      // 停止播报完成回调,调用stop方法并完成时会触发此回调
      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}`);
      }
    };
    // 设置回调
    this.ttsEngine?.setListener(speakListener);
    // 设置播报相关参数
    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-a", // requestId在同一实例内仅能用一次,请勿重复设置
      extraParams: extraParam
    }
    // 调用speak播报方法
    this.ttsEngine?.speak(message, speakParams)
  }

停止播放

直接调用stop即可。

ttsEngine.stop()

关闭引擎,释放引擎资源

// 调用shutdown接口
ttsEngine.shutdown()

语音识别回调

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)}`);
      },
      // 停止播报完成回调,调用stop方法并完成时会触发此回调
      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}`);
      }
    };
    // 设置回调
    this.ttsEngine?.setListener(speakListener);

播报策略

在不同的场景下,比如停顿,单词连读,数字分开读,等等,不同的场景就会有不同的播放策略。

单词播报方式

文本格式:[hN] (N=0/1/2),首先h是固定的,N可以选择012三个数字,0是智能判断单词播放方式。默认值即为0,1是逐个字母进行播报,2是以单词方式进行播报。

举例:

"hello[h1] world"

hello使用单词发音,world及后续单词将会逐个字母进行发音。

数字播报策略

格式和以上一样,[nN] (N=0/1/2),N可以选择012三个数字,0是智能判断数字处理策略,默认值为0;1是作为号码逐个数字播报,2是作为数值播报,超过18位数字不支持,自动按逐个数字进行播报。

举例:

"[n2]123[n1]456[n0]"

其中,123将会按照数值播报,456则会按照号码播报,而后的文本中的数字,均会自动判断。

插入静音停顿

格式为[pN],N为无符号整数,单位为ms。

举例:

"你好[p1000]程序员一鸣"

以上的语句播报时,将会在“你好”后插入1000ms的静音停顿。

指定汉字发音

汉字声调用后接一位数字1~5分别表示阴平、阳平、上声、去声和轻声5个声调,格式为:[=MN],其中M表示拼音,N表示声调,取值范围为,1表示阴平,2表示阳平,3表示上声,4表示去声,5表示轻声。

举例:

"着[=zhao2]火"

“着”字将读作“zhaó”。

相关总结

文本合成语音的能力目前只能在真机上进行测试,不支持模拟器。

更多关于HarmonyOS 鸿蒙Next开发中,如何实现文本合成语音的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS Next中,实现文本合成语音(TTS)主要使用@ohos.tty模块。首先在module.json5中声明ohos.permission.USE_TTY权限。核心步骤包括:创建TtsPlayer实例,调用speak()方法传入文本,通过on('play')on('finish')监听播放状态。支持设置语速、音调等参数。

在HarmonyOS Next中,实现文本合成语音(TTS)功能主要依靠系统提供的TextToSpeech引擎,其设计理念与Android类似但API属于HarmonyOS自有体系。以下是核心实现步骤:

1. 申请权限

module.json5配置文件中添加网络和音频输出权限:

{
  "module": {
    "requestPermissions": [
      {
        "name": "ohos.permission.INTERNET"
      },
      {
        "name": "ohos.permission.WRITE_AUDIO"
      }
    ]
  }
}

2. 初始化TTS引擎

import textToSpeech from '@ohos.textToSpeech';
import common from '@ohos.app.ability.common';

// 获取上下文
let context = getContext(this) as common.UIAbilityContext;

// 创建TTS实例
let ttsEngine = await textToSpeech.createTextToSpeech(context);

// 检查引擎支持的语言
let voices = await ttsEngine.getVoices();
console.log('支持的语言:', voices);

3. 基础语音合成

// 设置参数
let ttsParams: textToSpeech.TtsParams = {
  volume: 0.8,    // 音量 0.0-1.0
  speed: 1.0,     // 语速 0.5-2.0
  pitch: 1.0      // 音高 0.5-2.0
};

// 合成并播放
await ttsEngine.speak('你好,鸿蒙系统', ttsParams);

4. 高级控制功能

// 暂停/恢复播放
ttsEngine.pause();
ttsEngine.resume();

// 停止播放
ttsEngine.stop();

// 监听播放状态
ttsEngine.on('stateChange', (state: textToSpeech.PlaybackState) => {
  console.log('当前状态:', state);
});

// 释放资源
ttsEngine.release();

5. 离线语音合成(需预置语音包)

// 设置离线语音参数
let offlineConfig: textToSpeech.OfflineConfig = {
  language: 'zh-CN',      // 中文普通话
  voiceName: 'female1'    // 预置女声音源
};

await ttsEngine.setOfflineConfig(offlineConfig);

关键特性说明:

  • 多语言支持:通过getVoices()可查询系统支持的语音包
  • 实时控制:支持播放过程中动态调整语速、音量
  • 事件回调:提供完整的播放状态监听(开始/暂停/恢复/停止)
  • 资源管理:需在页面销毁时调用release()释放引擎资源

注意事项:

  1. 首次使用需要网络下载语音数据包(仅首次)
  2. 离线语音功能依赖设备预置的语音包
  3. 合成长文本时建议分段处理,避免内存占用过高

实际开发中可直接参考官方文档中的TextToSpeech API,上述代码基于HarmonyOS Next的ArkTS语法,与Android的Java/Kotlin实现方式有差异但功能逻辑相似。

回到顶部