当前位置: 首页 > news >正文

封装一个录音声音振动效果的组件

目标:根据声音的大小实现声音振动特效

实现步骤:

  • 通过 getAudioCapturerMaxAmplitude 观察音频区间
  • 封装振动组件,通过声音振幅数据实现振动效果

落地代码:

1)获取振幅数据,出入振动组件 AudioPage.ets

  timer?: number
  @State maxAmplitude: number = 0

    // startRecord 4. 每100ms获取一下声音振幅
    this.timer = setInterval(async () => {
      this.maxAmplitude = await avRecorder.getAudioCapturerMaxAmplitude()
      logger.debug('startRecord', this.maxAmplitude.toString())
    }, 100)

    // stopRecord 清理定时器
    clearInterval(this.timer)

AudioBoComp({ maxAmplitude: this.maxAmplitude })

2)实现振动组件 Audio/AudioBoComp.ets

@Component
struct AudioBoComp {
  @Prop @Watch('onChange') maxAmplitude: number
  @State per: number = 0
  onChange() {
    animateTo({ duration: 100 }, () => {
      if (this.maxAmplitude < 500) {
        this.per = 0
      } else if (this.maxAmplitude > 30000) {
        this.per = 1
      } else {
        this.per = this.maxAmplitude / 30000
      }
    })
  }

  build() {
    Row({ space: 5 }) {
      ForEach(Array.from({ length: 30 }), () => {
        Column()
          .layoutWeight(1)
          .height(this.per * 100 * Math.random())
          .backgroundColor($r('app.color.common_blue'))
      })
    }
    .width('100%')
    .height(100)
  }
}

http://www.lryc.cn/news/442145.html

相关文章:

  • Java、JS与Go的扩展操作符,揭秘它们的‘魔法’!
  • ROS学习笔记13——rosbag功能包的简单使用
  • Python Flask网页开发基本框架
  • Mybatis-plus进阶篇(五)
  • 交通标志识别系统Python+卷积神经网络算法+深度学习人工智能+TensorFlow模型训练+计算机课设项目+Django网页界面
  • 【QT】定时器使用
  • 虚拟机:3、(待更)WSL2安装Ubuntu系统+实现GPU直通
  • CSP-J2024年全真模拟题 阅读程序篇2
  • 几种手段mfc140u.dll丢失的解决方法,了解mfc140u.dll
  • Scrapy爬虫框架 Spider Middleware 爬虫页中间件
  • localectl 命令:系统语言、键盘布局和区域设置
  • 《微信小程序实战(3) · 推广海报制作》
  • SS-MUSIC
  • Spring Cloud Gateway组件
  • 激发AI创造力:掌握Prompt提示词的高效提问方法
  • 江科大笔记—STM32课程简介
  • 使用 nvm 管理 node 版本:如何在 macOS 和 Windows 上安装使用nvm
  • 【项目开发 | Python】基于“羊了个羊“风格的消除类小游戏
  • 云服务器使用
  • sqli-lab靶场学习(四)——Less11-14(post方法)
  • GBDT算法原理及其公式推导过程
  • 网络:UDP协议
  • linux与unix
  • 计算机网络29——Linux基本命令vim,gcc编译命令
  • uniapp离线(本地)打包
  • 如何编写一个爬虫以实时获取某平台商品价格
  • 声网SDK脚本运行错误
  • Docker + Win 10 学习记录
  • GlusterFS分布式存储
  • ARM/Linux嵌入式面经(三八):绿盟科技