微信小程序录音Record

本文介绍了如何在微信小程序中实现录音功能,包括录音的布局设置、样式应用,以及开始和停止录音的操作,配合示例效果图展示整个流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

布局

<!--pages/record/record.wxml-->
<view>
	<button 
		class="tui-menu-list"
		bindtap="startRecordAac" 
		type="primary">录音开始(aac)</button>
	<button 
		class="tui-menu-list"
		bindtap="startRecordMp3" 
		type="primary">录音开始(mp3)</button>
	<button 
		class="tui-menu-list" 
		bindtap="stopRecord" 
		type="primary">录音结束</button>
	<button 
		class="tui-menu-list"
		bindtap="playRecord" 
		type="primary">播放录音</button>
</view>

样式:

/* pages/record/record.wxss */

.tui-menu-list{
  flex-direction: row;
  margin: 20rpx;
  padding: 20rpx;
}

开始录音和停止录音

// pages/record/record.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  onLoad:function (options) {
    var that = this
    this.recorderManager = wx.getRecorderManager();
    this.recorderManager.onError(function () {
      that.tip("录音失败!");
    })
    this.recorderManager.onStop(function (res) {
      that.setData({
        src:res.tempFilePath
      })
      console.log(res.tempFilePath)
      that.tip("录音完成!")
    })
    this.innerAudioContext = wx.createInnerAudioContext()
    this.innerAudioContext.onError((res) =>{
      that.tip("播放录音失败!")
    })
  },

  //提示
  tip:function (msg) {
    wx.showModal({
      cancelColor: 'cancelColor',
      title:'提示',
      content:msg,
      showCancel:false
    })
  },

  //录制aac
  startRecordAac:function () {
    this.recorderManager.start({
      format:'aac'
    })
  },

  //录制mp3
  startRecordMp3:function () {
    this.recorderManager.start({
      format:'mp3'
    })
  },

  //停止录音
  stopRecord:function () {
    this.recorderManager.stop()
  },

  //播放录音
  playRecord:function () {
    var that = this
    var src = this.data.src
    if (src='') {
      this.tip('请先录音')
      return
    }
    this.innerAudioContext.src = this.data.src
    this.innerAudioContext.play()
  }

  
})

 

效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值