软件创新实验室:微信小程序开发——音频录制与播放

文章目录

声明

1)本次小项目代码来源于杨杨学姐实验室授课内容。

2)博主是萌新上路,文中如有不当之处,请各位大佬指出,共同进步,谢谢。
 

主体

这个微信小程序较为简单,适用于新手入门,话不多说,上代码,

// pages/index/index.js
var tempFilePath = "" //用于保存临时音频文件地址
Page({
    
    
  /**
   * 录制音频
   */
  record:function(){
    
    
    console.log("录制音频开始")
    //录制音频
    wx.startRecord({
    
    
      success: (res) => {
    
    
        tempFilePath = res.tempFilePath
      },
    })
    //自动停止录制,5000ms
    // setTimeout(function(){
    
    
    //   console.log("停止录制事件")
    //   wx.stopRecord({
    
    
    //     success: (res) => {},
    //   })
    // },5000)
  },
  /**
   * 结束录制
   */
  stop:function(){
    
    
    console.log("结束录制事件")
      wx.stopRecord({
    
    
        success: (res) => {
    
    },
      })
  },
  /**
   * 暂停录制
   */
  pauseVoice:function(){
    
    
    console.log("录制暂停")
    wx.pauseVoice({
    
    
      success: (res) => {
    
    },
    })
  },
  /**
   * 播放录制好的音频
   */
  playRecord:function(){
    
       
  	wx.playVoice({
    
    
    filePath: tempFilePath,
  })
}
})

可以发现主要就是四个函数,

wx.startRecord
wx.stopRecord
wx.pauseVoice
wx.playVoice

通过这四个函数,实现了录制,结束,暂停以及播放录音的功能,

很简单,很有趣的微信小程序!

猜你喜欢

转载自blog.csdn.net/weixin_46263782/article/details/111548543