微信小程序简易录音机

首先先创建一个项目(想必大家都会啦那就直接开干)

首先上html结构

<view class="wx-container">
  <view id="title">录音机</view>
  <view id="time">{{hours}}:{{minute}}:{{second}}</view>
  <view class="btngroup">
    <view hover-class="change" catch:tap="play">播放</view>
    <view class="play" hover-class="change" catch:tap="start"></view>
    <view hover-class="change" catch:tap="stop">结束</view>
  </view>
</view>

css样式

/* pages/radio/index.wxss */
.wx-container {
  width: 100vw;
  height: 100vh;
  margin: 0 auto;
  text-align: center;
  box-sizing: border-box;
}

#title {
  margin: 100rpx auto;
  text-align: center;
  font-size: 50rpx;

}

#time {
  font-size: 150rpx;
}

.btngroup {
  height: 180rpx;
  margin: 100rpx auto;
  display: flex;
  align-items: center;
  justify-content: space-around;
}

.btngroup>view:not(.play) {
  width: 120rpx;
  height: 120rpx;
  border-radius: 50%;
  line-height: 120rpx;
  background-color: #eee;
}

.play {
  width: 150rpx;
  height: 150rpx;
  border: solid 50rpx red;
  box-sizing: border-box;
  border-radius: 50%;
  transition: .2s;
  background-color: transparent;
}

.change {
  transition: .2s;
  box-shadow: 0 0 8rpx 8rpx rgb(0, 0, 0);

}

样式的效果:
在这里插入图片描述
接下来js代码部分:
通过wx.createInnerAudioContext()方法获取到

// 获取录音管理器
var tape = wx.getRecorderManager()
// 创建全局音频
var audio = wx.createInnerAudioContext()
Page({
  data: {
    time: 0,
    cleartime: '',
    state: 0, // 0 为停止录音  1 为正在录音   2 为暂停录音
    timer: null,
    hours: '0' + 0, // 时
    minute: '0' + 0, // 分
    second: '0' + 0, // 秒
    tempFilePath: null
  },

计时开始

  start() {
    let _this = this;
    switch (this.data.state) {
      case 0:
        _this.setInterval();
        // 开始录音
        tape.start()
        console.log('开始录音');
        this.setData({
          state: 1 // 把state设置为1 (暂停录音状态)
        })
        // audio.destroy() // 释放音频资源
        break
      case 1:
        clearInterval(_this.data.timer);
        // 暂停录音
        tape.pause()
        console.log('暂停录音');
        this.setData({
          state: 2 // 把state设置为2 (继续录音状态)
        })
        break
      case 2:
        _this.setInterval();
        // 继续录音
        tape.resume()
        console.log('继续录音');
        this.setData({
          state: 1 // 把state设置为1 (暂停录音状态)
        })
        break
    }
    // 为了性能考虑 20 秒后自动结束录音  
    setTimeout(() => {
      clearInterval(_this.data.timer);
      tape.stop()
      // 监听结束录音事件
      tape.onStop((res) => {
        this.data.tempFilePath = res.tempFilePath
        console.log('自动保存录音');
        wx.showToast({
          title: '自动保存录音成功',
          mask: true,
          duration: 500,
        })
      })
    }, 20000)
  },

播放

play() {
    // 如果音频路径为空,弹出提示
    if (!this.data.tempFilePath) {
      wx.showModal({
        title: '没有录音',
        content: '请开始录音或保存录音'
      })
      return
    }
    audio.src = this.data.tempFilePath
    // 播放录音的音频
    audio.play()
    wx.showToast({
      title: '播放录音',
      mask: true,
      duration: 500
    })
  },

结束

 stop() {
    // 如果state处于0(未录音状态)弹出提示
    if (this.data.state == 0) {
      wx.showToast({
        title: '请先开始录音',
        mask: true,
        duration: 500,
        icon: 'error'
      })
      return
    }
    let _this = this;
    this.setData({
      hours: '0' + 0,
      minute: '0' + 0,
      second: '0' + 0,
      state: 0 // 点击结束按钮之后 把 state(状态)初始化为0(未录音状态)
    })
    clearInterval(_this.data.timer);
    // 结束录音 保存录音
    tape.stop()
    // 监听结束录音事件
    tape.onStop((res) => {
      this.data.tempFilePath = res.tempFilePath
      console.log('保存录音');
      wx.showToast({
        title: '保存录音成功',
        mask: true,
        duration: 500,
      })
    })
  },

最终效果:在主页视频

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序源码(含截图)录音机微信小程序
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值