需要语音播报,对于语音播报依赖较高,希望提供示例

需要语音播报,对于语音播报依赖较高,希望提供示例。

HarmonyOS
2024-11-08 09:34:19
1025浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
Heiang

​文字转换语音如下demo:

注意:需要真机才能正常使用,模拟器无法创建相关引擎导致程序异常。​

import { textToSpeech } from '@kit.CoreSpeechKit'; 
import { BusinessError } from '@kit.BasicServicesKit'; 
 
let ttsEngine: textToSpeech.TextToSpeechEngine; 
@Entry 
@Component 
struct SpeechDemo { 
  @State createCount: number = 0; 
  @State result: boolean = false; 
  @State voiceInfo: string = ""; 
  @State text: string = ""; 
  @State textContent: string = ""; 
  @State utteranceId: string = "123456"; 
  @State originalText: string = "\n\t\t古人学问无遗力,少壮工夫老始成;\n\t\t" + 
    "纸上得来终觉浅,绝知此事要躬行。\n\t\t"; 
  @State illegalText: string = ""; 
 
  aboutToAppear(): void { 
    this.createByCallback(); 
  } 
 
  build() { 
    Column() { 
      Scroll() { 
        Column() { 
          TextArea({ placeholder: 'Please enter tts original text', text: `${this.originalText}` }) 
            .margin(20) 
            .focusable(false) 
            .border({ width: 5, color: 0x317AE7, radius: 10, style: BorderStyle.Dotted }) 
            .onChange((value: string) => { 
              this.originalText = value; 
              console.info("original text: " + this.originalText); 
            }) 
          Button() { 
            Text("speak") 
              .fontColor(Color.White) 
              .fontSize(20) 
          } 
          .type(ButtonType.Capsule) 
          .backgroundColor("#0x317AE7") 
          .width("80%") 
          .height(50) 
          .margin(10) 
          .onClick(() => { 
            this.createCount++; 
            console.info(`CreateTtsEngine:createCount:${this.createCount}`) 
            this.speak(); 
          }) 
 
          Button() { 
            Text("listVoicesCallback") 
              .fontColor(Color.White) 
              .fontSize(20) 
          } 
          .type(ButtonType.Capsule) 
          .backgroundColor("#0x317AE7") 
          .width("80%") 
          .height(50) 
          .margin(10) 
          .onClick(() => { 
            this.listVoicesCallback(); 
          }) 
 
          Button() { 
            Text("stop") 
              .fontColor(Color.White) 
              .fontSize(20) 
          } 
          .type(ButtonType.Capsule) 
          .backgroundColor("#0x317AE7") 
          .width("80%") 
          .height(50) 
          .margin(10) 
          .onClick(() => { 
            // 停止播报 
            console.info("isSpeaking click:-->"); 
            ttsEngine.stop(); 
          }) 
 
          Button() { 
            Text("isBusy") 
              .fontColor(Color.White) 
              .fontSize(20) 
          } 
          .type(ButtonType.Capsule) 
          .backgroundColor("#0x317AE7") 
          .width("80%") 
          .height(50) 
          .margin(10) 
          .onClick(() => { 
            console.info("isSpeaking click:-->"); 
            // 查询播报状态 
            let isBusy = ttsEngine.isBusy(); 
            console.info('isBusy :' + isBusy); 
          }) 
 
          Button() { 
            Text("shutdown") 
              .fontColor(Color.White) 
              .fontSize(20) 
          } 
          .type(ButtonType.Capsule) 
          .backgroundColor("#0x317AA7") 
          .width("80%") 
          .height(50) 
          .margin(10) 
          .onClick(() => { 
            // 释放引擎 
            ttsEngine.shutdown(); 
          }) 
        } 
        .layoutWeight(1) 
      } 
      .width('100%') 
      .height('100%') 
    } 
  } 
 
  // 创建引擎,通过callback形式返回 
  // 当引擎不存在、引擎资源不存在、初始化超时,返回错误码1003400005,引擎创建失败 
  private createByCallback() { 
    // 设置创建引擎参数 
    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}.`) 
    } 
  }; 
 
  // 调用speak播报方法 
  // 未初始化引擎时调用speak方法,返回错误码1003400007,合成或播报失败 
  private 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)); 
      }, 
      // 停止合成或播报回调,调用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('speakListener onData: ' + ' requestId: ' + requestId + ' sequence: ' + JSON.stringify(response) + ' audio: ' +   audio); 
      }, 
      // 错误回调,播报过程发生错误时触发此回调 
      // 未创建引擎时调用speak方法时返回错误码1003400007,合成或播报失败 
      // 连续调用两次speak,第二次speak会返回错误码1003400006,服务正忙碌 
      onError(requestId: string, errorCode: number, errorMessage: string) { 
        console.error('OnError: ' + 'requestId: ' + requestId + ' errorCode: ' + errorCode + 'errorMessage: ' + errorMessage); 
      } 
    }; 
    // 设置回调 
    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播报方法 
    ttsEngine.speak(this.originalText, speakParams); 
  }; 
 
  // 查询语种音色信息,以callback形式返回 
  private listVoicesCallback() { 
    // 设置查询相关参数 
    let voicesQuery: textToSpeech.VoiceQuery = { 
      requestId: '123456-b', // requestId在同一实例内仅能用一次,请勿重复设置 
      online: 1 
    }; 
 
    // 调用listVoices方法,以callback返回语种音色查询结果 
    ttsEngine.listVoices(voicesQuery, (err: BusinessError, voiceInfo: textToSpeech.VoiceInfo[]) => { 
      if (!err) { 
        // 接收目前支持的语种音色等信息 
        console.info('voiceInfo is succeeded ' + voiceInfo); 
      } else { 
        console.error("errCode is " + err.code); 
        console.error("errMessage is " + err.message); 
      } 
    }); 
  }; 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
分享
微博
QQ
微信
回复
2024-11-08 17:14:11


相关问题
HarmonyOS 文字转语音无法播报
797浏览 • 1回复 待解决
HarmonyOS 语音识别SDK
779浏览 • 1回复 待解决
HarmonyOS 语音播放问题
705浏览 • 1回复 待解决
HarmonyOS 语音识别报错
887浏览 • 1回复 待解决
ArkTS语言支持语音识别吗?
1966浏览 • 1回复 待解决
Sqlite sql 语音支持问题
895浏览 • 0回复 待解决
HarmonyOS 关于语音发送功能
830浏览 • 1回复 待解决
HarmonyOS 实时语音转文本
736浏览 • 1回复 待解决
HarmonyOS 如何实现语音助手的功能?
1162浏览 • 1回复 待解决
HarmonyOS 如何实现语音转成文字
752浏览 • 1回复 待解决
文本转语音的方法有哪些?
1114浏览 • 1回复 待解决
HarmonyOS pcm语音文件播放异常
797浏览 • 1回复 待解决