#HarmonyOS NEXT 体验官#梅科尔工作室HOS-文字转语音 原创

梅科尔杨灿
发布于 2024-8-31 23:46
浏览
0收藏

#HarmonyOS NEXT 体验官#梅科尔工作室HOS-文字转语音

引入

在这个多元化和包容性日益受到重视的时代,沟通的无障碍性成为了连接不同群体的关键桥梁。

我们的项目致力于通过创新技术打破聋哑人与健听人之间的沟通壁垒,打造了一款集手语识别与翻译于一体的智能APP。

我们借助文字转语音技术,将转化后的文字即时转换为语音输出,从而让聋哑人能够“说”出自己的心声。

这一新增的小功能不仅极大地丰富了APP的功能性,更是在实质上增强了其作为无障碍沟通工具的价值,使得聋哑人在日常生活中能够更加自如地表达自己。

介绍

文字转语音功能是将一段不超过10000字符的文本合成为语音并进行播报。

场景举例:手机在无网状态下,系统应用无障碍(屏幕朗读)接入文本转语音能力。

效果预览

#HarmonyOS NEXT 体验官#梅科尔工作室HOS-文字转语音-鸿蒙开发者社区

实现思路

1.在使用文本转语音时,将实现文本转语音相关的类添加至工程

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

2.调用createEngine接口,创建textToSpeechEngine实例

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
};

// 调用createEngine方法
textToSpeech.createEngine(initParamsInfo, (err: BusinessError, textToSpeechEngine: textToSpeech.TextToSpeechEngine) => {
  if (!err) {
    console.info('Succeeded in creating engine');
    // 接收创建引擎的实例
    ttsEngine = textToSpeechEngine;
  } else {
    // 创建引擎失败时返回错误码1003400005,可能原因:引擎不存在、资源不存在、创建引擎超时
    console.error(`Failed to create engine. Code: ${err.code}, message: ${err.message}.`);
  }
});

3.得到TextToSpeechEngine实例对象后,实例化SpeakParams对象、SpeakListener对象,并传入待合成及播报的文本originalText,调用speak接口进行播报

// 设置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 = '你好,华为';
// 设置播报相关参数
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);

整体代码

import { textToSpeech } from '@kit.CoreSpeechKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { router } from '@kit.ArkUI';

let ttsEngine: textToSpeech.TextToSpeechEngine;
let isEngineInitialized: boolean = false; // 引擎是否已经初始化
let isSpeaking: boolean = false; // 是否正在播放

@Entry
@Component
struct Index {
  @State createCount: number = 0;
  @State result: boolean = false;
  @State voiceInfo: string = "";
  @State text: string = "";
  @State textContent: string = "";
  @State timetext: number = 4;
  @State originalText: string = "颈椎健康,身心舒畅";
  build() {
    Column() {
      Scroll() {
        Column({space:20}) {
          Text("文字转语音")
            .fontSize(50)
          Text(this.originalText)
          TextInput({ placeholder: this.originalText})
            .width(190)
            .height(50)
            .backgroundColor("#efefef")
            .borderRadius(15)
            .onChange((value: string) => {
              this.originalText = value;
              console.info(`original text: ${this.originalText}`);
            })

          Button() {
            Text("开始")
              .fontColor(Color.White)
              .fontSize(20)
          }
          .type(ButtonType.Capsule)
          .backgroundColor("#0x317AE7")
          .width("80%")
          .height(50)
          .margin(10)
          .onClick(() => {
            this.createCount++;
            if (!isEngineInitialized) {
              this.createByCallback();
            } else {
              this.createCount++;
              console.info(`CreateTtsEngine:createCount:${this.createCount}`);
              this.createByCallback();
              setTimeout(() => {
              }, 10);//等待,延迟10毫秒
              //加载项,打开
              let  intervalID1 = setInterval(
                () => {//循环3次1次1秒
                  this.timetext--//数值减减
                  if (this.timetext == 4) {
                    this.createCount++;
                    this.speak();
                  }
                  if (this.timetext == 0) {
                    clearInterval(intervalID1)
                    ttsEngine.shutdown();
                  }
                }, 1000)
              this.timetext = 5
            }
          })

          Button() {
            Text("暂停")
              .fontColor(Color.White)
              .fontSize(20)
          }
          .type(ButtonType.Capsule)
          .backgroundColor("#0x317AE7")
          .width("80%")
          .height(50)
          .margin(10)
          .onClick(() => {
            if (ttsEngine && !isSpeaking) {
              console.info("Stop button clicked.");
              ttsEngine.stop();
            } else {
              console.warn("TTS engine is not initialized yet or already speaking.");
            }
          })
          Button("语音转文字")
            .onClick(()=>{
              router.pushUrl({
                url:"pages/yuyin_wenben"
              })
            })

        }
        .layoutWeight(1)
      }
      .width('100%')
      .height('100%')
    }
  }
  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
    };

    textToSpeech.createEngine(initParamsInfo, (err: BusinessError, textToSpeechEngine: textToSpeech.TextToSpeechEngine) => {
      if (!err) {
        console.info('Succeeded in creating engine.');
        ttsEngine = textToSpeechEngine;
        isEngineInitialized = true; // 标记引擎已初始化
        this.speak(); // 初始化完成后立即尝试播放
      } else {
        console.error(`Failed to create engine. Code: ${err.code}, message: ${err.message}.`);
      }
    });
  };

  private speak() {
    if (!isEngineInitialized) {
      console.error("TTS engine is not initialized yet.");
      return;
    }

    if (isSpeaking) {
      console.warn("Already speaking. Please wait until the current playback completes.");
      return;
    }

    isSpeaking = true; // 标记为正在播放

    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)}`);
        isSpeaking = false; // 标记为不再播放
      },
      // 停止播报完成回调,调用stop方法并完成时会触发此回调
      onStop(requestId: string, response: textToSpeech.StopResponse) {
        console.info(`onStop, requestId: ${requestId} response: ${JSON.stringify(response)}`);
        isSpeaking = false; // 标记为不再播放
      },
      // 返回音频流
      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}`);
        isSpeaking = false; // 标记为不再播放
      }
    };
    // 设置回调
    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);
  };

}

注意点

1.检查引擎初始化状态:检查 isEngineInitialized 标志,如果引擎还未初始化,则输出错误信息并返回,防止未初始化的情况下尝试使用引擎

let isEngineInitialized: boolean = false; // 引擎是否已经初始化
let isSpeaking: boolean = false; // 是否正在播放
if (!isEngineInitialized) {
      console.error("TTS engine is not initialized yet.");
      return;
    }

2.检查是否正在播放:如果 isSpeaking 为 true,即正在播放中,则输出警告信息并返回,避免重复播放

    if (isSpeaking) {
      console.warn("Already speaking. Please wait until the current playback completes.");
      return;
    }

    isSpeaking = true; // 标记为正在播放

3.调用方法时,要确保引擎创建完成再调用方法(延时);并且调用完方法之后要记得释放引擎(ttsEngine.shutdown())

.onClick(() => {
            this.createCount++;
            if (!isEngineInitialized) {
              this.createByCallback();
            } else {
              this.createCount++;
              console.info(`CreateTtsEngine:createCount:${this.createCount}`);
              this.createByCallback();
              setTimeout(() => {
              }, 10);//等待,延迟10毫秒
              //加载项,打开
              let  intervalID1 = setInterval(
                () => {//循环3次1次1秒
                  this.timetext--//数值减减
                  if (this.timetext == 4) {
                    this.createCount++;
                    this.speak();
                  }
                  if (this.timetext == 0) {
                    clearInterval(intervalID1)
                    ttsEngine.shutdown();
                  }
                }, 1000)
              this.timetext = 5
            }
          })

总结

嘿嘿,猜猜谁成了手语翻译界的超级英雄?就是你啊!现在,你不仅能把手语动作变成屏幕上的文字,还能让这些文字“活”起来,直接“说”出来!

下次有人问起你的APP时,你可以酷酷地来一句:“看好了,这是我的秘密武器——手语翻译加文字转语音,一键启动,沟通无阻!”(记得加一句:这不是普通的聊天工具,这是未来的声音!)

各位小伙伴,准备好变身成手语翻译界的超能力者了吗?打开你的APP,让科技魔法开始吧!

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
已于2024-8-31 23:46:26修改
收藏
回复
举报
回复
    相关推荐