前言
微信小程序中可以实现录音的主要有wx.startRecord()和wx.getRecorderManager(),其中wx.startRecord()从微信开发者工具基础库1.6后停止维护,开始启用更加强大的wx.getRecorderManager()。
一、关于wx.startRecord()的坑
wx.startRecord()使用还是相当容易的,微信官方示例便是使用wx.startRecord()。
1 wx.startRecord({2 success(res) {3 const tempFilePath =res.tempFilePath4 console.log(res)5 }6 })7 setTimeout(function() {8 wx.stopRecord() //结束录音
9 }, 3000)
成功录音的返回值为一个含有音频临时路径的对象
1 errMsg: "startRecord:ok"
2 tempFilePath: "http://tmp/wx88e053d45b28e2cf.o6zAJs-nrru-YZpqRQeb-X8EzBfk.JVhmiR78K4oY2e7522995230f041a81c5967a4e1598c.silk"
这个silk格式为加密格式,在真机上可以播放,上传到服务器以后,其它用户无法播放,只有上传者可以播放。
如果要分享给别人,得先解密,再转换为其它格式,网上的教程很多,但是比较麻烦
二、关于wx.getRecorderManager()的实战解析
有一个项目,需要使用录音,上传到云存储后,分享给其它人。
1 const recorderManager =wx.getRecorderManager()2 const backgroundAudio =wx.getBackgroundAudioManager()3 var util = require('../../utils/util.js');4 Page({5 data: {6 openRecordingdis: "block", //显示录机图标
7 shutRecordingdis: "none", //隐藏停止图标
8 recordingTimeqwe: 0, //录音计时
9 setInter: "", //录音名称
10 soundUrl: ""
11 },12
13 //录音计时器
14 recordingTimer: function() {15 var that = this;16 //将计时器赋值给setInter
17 that.data.setInter =setInterval(18 function() {19 var time = that.data.recordingTimeqwe + 1;20 that.setData({21 recordingTimeqwe: time22 })23 }, 1000);24 },25
26
27 //开始录音
28 openRecording: function() {29 var that = this;30 wx.getSystemInfo({31 success: function(res) {32 that.setData({33 shutRecordingdis: "block",34 openRecordingdis: "none"
35 })36 }37 })38 const options ={39 duration: 60000, //指定录音的时长&#