#鸿蒙学习大百科#如何实现文本转语音功能?

如何实现文本转语音功能?

HarmonyOS
2024-10-22 16:05:00
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
努力向前进
import { textToSpeech } from '@kit.CoreSpeechKit';
import { BusinessError } from '@kit.BasicServicesKit';
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
};
try {
  // 调用createEngine方法
  textToSpeech.createEngine(initParamsInfo, (err: BusinessError, textToSpeechEngine: textToSpeech.TextToSpeechEngine) => {
    if (!err) {
      console.info('createEngine is succeeded');
      // 接收创建引擎的实例
      ttsEngine = textToSpeechEngine;
    } else {
      // 创建引擎失败时返回错误码1003400005,可能原因:引擎不存在、资源不存在、创建引擎超时
      console.error("errCode is " + err.code);
      console.error("errMessage is " + err.message);
    }
  });
} catch (error) {
  let message = (error as BusinessError).message;
  let code = (error as BusinessError).code;
  console.error(`createEngine failed, error code: ${code}, message: ${message}.`)
}
// 回调对象
let speakListener: textToSpeech.SpeakListener = {
  // 开始合成或播报回调
  onStart(requestId: string, response: textToSpeech.StartResponse) {
    console.info('onStart: ' + 'requestId: ' + requestId + ' response: ' + response.audioType);
  },
  // 合成完成或播报完成回调
  onComplete(requestId: string, response: textToSpeech.CompleteResponse) {
    console.info('onComplete: ' + 'requestId: ' + requestId + ' response: ' + response.type);
  },
  // 停止合成或播报回调
  onStop(requestId: string, response: textToSpeech.StopResponse) {
    console.info('onStop: ' + 'requestId: ' + requestId + ' response: ' + response.type);
  },
  // 返回音频流回调
  onData(requestId: string, audio: ArrayBuffer, response: textToSpeech.SynthesisResponse) {
    console.info('speakListener onData: ' + ' requestId: ' + requestId + ' sequence: ' + response.sequence + ' audio: ' + audio);
  },
  // 错误回调
  onError(requestId: string, errorCode: number, errorMessage: string) {
    console.error('OnError: ' + 'requestId: ' + requestId + ' errorCode: ' + errorCode + 'errorMessage: ' + errorMessage);
  }
};


@Entry
@Component
struct Index {
  build() {
    Row() {
      Column() {
        Text("点击播放")
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
          .onClick(() => {
            // 设置回调
            ttsEngine.setListener(speakListener);
            let originalText: string = '你好,华为';;
            // 设置播报相关参数
            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
            }
            // 调用播报方法
            ttsEngine.speak(originalText, speakParams);
          })
      }
      .width('100%')
    }
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-10-22 22:26:28
相关问题
#鸿蒙学习大百科#如何实现ui优化?
259浏览 • 1回复 待解决
#鸿蒙学习大百科#ArkTS如何生成xml?
326浏览 • 1回复 待解决
#鸿蒙学习大百科#什么是AppStorage?
240浏览 • 1回复 待解决
#鸿蒙学习大百科#什么是LocalStorage?
259浏览 • 1回复 待解决