#星光不负 码向未来#鸿蒙赋能交流会之旅 原创

べ_D
发布于 2025-10-22 14:00
浏览
0收藏

漫溯鸿蒙学习路

作为一个鸿蒙的新手开发者,通过自己的学习能力来深入了解鸿蒙实属有限,最开始学习鸿蒙的时候也总是四处碰壁,华为官方提供的开发者文档对于新手来讲还是有些不容易理解,更多的还是依靠鸿蒙的活动包括线上的激励计划、模版集成创新活动、线下的鸿蒙赋能交流会或者看行内大佬的文章博客来提升自己。这里给大家推荐一位大佬——万少,他是一位资深的鸿蒙技术传播者,发布了许多关于鸿蒙应用开发技术的分享,同时他也作为讲师参加了2025年9月20日 鸿蒙赋能交流会-实操工坊(广州站) 活动,我也非常荣庆的见到了本人。

#星光不负 码向未来#鸿蒙赋能交流会之旅-鸿蒙开发者社区

注:这位大佬的社区博客截图,其中有很多技术的分享。

参加鸿蒙赋能交流会

鸿蒙赋能交流会-实操工坊也是华为考虑到我们作为个人开发者学习鸿蒙技术的不容易,从而举办线下活动来帮助我们学习进步。

在这里也是认识到了许多志同道合的朋友,我们一起分组完成题目,一起拿奖。

#星光不负 码向未来#鸿蒙赋能交流会之旅-鸿蒙开发者社区

这里也是记得当时有一道题目是关于语音播报文本的,要求我们实现点击播放图标能将所填文本转成语音播放出来。

具体实现如下:

封装我们的播放引擎类,这边也是多看官方的文档找到@kit.CoreSpeechKit中的textToSpeech模块来实现。主要也就是对于引擎的创建和部分参数的配置,设置回调信息函数。

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

export class Speaker {
  ttsEngine?: textToSpeech.TextToSpeechEngine;
  extraParam: Record<string, Object> = {
    "queueMode": 0,
    "speed": 1,
    "volume": 2,
    "pitch": 1,
    "languageContext": 'zh-CN',
    "audioType": "pcm",
    "soundChannel": 3,
    "playType": 1
  };
  initParamsInfo: textToSpeech.CreateEngineParams = {
    language: 'zh-CN',
    person: 0,
    online: 1,
    extraParams: { "style": 'interaction-broadcast', "locate": 'CN', "name": 'EngineName' }
  };
  speakListener?: textToSpeech.SpeakListener;

  constructor() {
    this.initListener()
    this.createEngine()
  }

  initListener() {
    this.speakListener = {
      onStart(requestId: string, response: textToSpeech.StartResponse) {
      },

      onComplete(requestId: string, response: textToSpeech.CompleteResponse) {
        if (response.type === 1) {
          emitter.emit("eventId");
        }
      },

      onStop(requestId: string, response: textToSpeech.StopResponse) {
        if (response.type === 1) {
          emitter.emit("eventId");
        }
      },

      onData(requestId: string, audio: ArrayBuffer, response: textToSpeech.SynthesisResponse) {
      },

      onError(requestId: string, errorCode: number, errorMessage: string) {
      }
    };
  }

  createEngine() {
    try {
      textToSpeech.createEngine(this.initParamsInfo,
        (err: BusinessError, textToSpeechEngine: textToSpeech.TextToSpeechEngine) => {
          if (!err) {
            this.ttsEngine = textToSpeechEngine;
            this.ttsEngine.setListener(this.speakListener);
          } else {
          }
        });
    } catch (error) {
      let message = (error as BusinessError).message;
      let code = (error as BusinessError).code;
    }
  }

  startSpeak(content: string) {
    let speakParams: textToSpeech.SpeakParams = {
      requestId: Date.now().toString(),
      extraParams: this.extraParam
    };
    this.ttsEngine?.speak(content, speakParams);
  }

  stopSpeak() {
    this.ttsEngine?.stop();
  }

  shutdownEngine() {
    this.ttsEngine?.shutdown();
  }
}

封装好了,在UI页面中调用方法传入要转换的值就行:

Image(this.isClicked ? $r('app.media.ic_AI_read_on') : $r('app.media.ic_AI_read_normal'))
  .width(40)
  .onClick(() => {
    this.isClicked = !this.isClicked;
    if (this.isClicked === true) {
      this.speaker.startSpeak('你好我是华为鸿蒙开发者!!!')
    } else {
      this.speaker.stopSpeak();
    }
  })

这边现场也是有老师来指导,不懂得问题也是能很快得到解答。最后我这边也是成功解出3道题,拿到了官方送的小礼品:

#星光不负 码向未来#鸿蒙赋能交流会之旅-鸿蒙开发者社区

小组成员也是一起努力拿到了这本鸿蒙开物小礼盒。

#星光不负 码向未来#鸿蒙赋能交流会之旅-鸿蒙开发者社区

对于这次的线下交流会,也是认识了更多的行内大佬交了更多行内的朋友,掌握了更多的鸿蒙技巧。

前路漫漫依旧前进

对于鸿蒙的学习还有很长的一段路要走,但是前进的脚步是不能够停下来的,这边也是参加了更多的官方活动,希望能够与更多小伙伴结伴同行。

#星光不负 码向未来#鸿蒙赋能交流会之旅-鸿蒙开发者社区

#星光不负 码向未来#鸿蒙赋能交流会之旅-鸿蒙开发者社区

©著作权归作者所有,如需转载,请注明出处,否则将追究法律责任
分类
已于2025-10-22 19:33:49修改
2
收藏
回复
举报
1条回复
按时间正序
/
按时间倒序
是个狼灭
是个狼灭

小礼盒真不错

回复
2025-10-22 21:46:56
回复
    相关推荐