纯血鸿蒙APP实战开发——语音录制和声音动效实现

介绍

本示例使用AVrecord录制音频和AVrecord的getAudioCapturerMaxAmplitude接口获取振幅实现UI动效;使用AVplayer播放音频

效果图预览

使用说明

  1. 按住按钮开始录音。
  2. 上划取消录制。
  3. 录制完成后点击消息框可播放录音。

实现思路

  1. 利用组合手势来实现音频录制与取消录制。
build() {
   
  Column() {
   
    Button($r('app.string.voice_record_dynamic_effect_button')) 
      .gesture(
        GestureGroup(GestureMode.Sequence,
          LongPressGesture()
            .onAction( () => {
   
              this.AVrecord.startRecordingProcess();
            })
            .onActionEnd( () => {
   
              this.AVrecord.stopRecordingProcess();
            }),
          PanGesture()
            .onActionStart( () => {
   
              clearInterval(this.count);
            })
            .onActionEnd( () => {
   
              this.AVrecord.stopRecordingProcess();
            })
        )
          .onCancel( () => {
   
            this.AVrecord.startRecordingProcess();
          })
      )
    }
}

  1. 在音频录制的时候通过getAudioCapturerMaxAmplitude获取声音振幅使UI变化。
    (1)在录制音频的时候通过getAudioCapturerMaxAmplitude获取振幅;
    (2)对振幅做占比运算,随机取最大值与最小值之间的值(Math.floor(Math.random())来使column的高度随机变化;
async
startRecordingProcess()
{
   
  if (this.avRecorder !== undefined) {
   
    await this.avRecorder.release();
    this.avRecorder = undefined;
  }
  // 1.创建录制实例
  this.avRecorder 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值