时间:2021-07-01 10:21:17 帮助过:192人阅读
小程序中提供了两种录音的API
旧版录音功能
首先启动录音,然后停止录音即可拉到音频的临时地址
启动录音:
- var that = this;
- wx.startRecord({
- success: function (res) {
- // 调用了停止录音接口就会触发这个函数,res.tempFilePath为录音文件临时路径
- var tempFilePath = res.tempFilePath
- that.setData({
- src: tempFilePath
- })
- },
- fail: function (res) {
- //录音失败的处理函数
- }
- })
停止录音:
- wx.stopRecord()
播放录音:
- wx.playVoice({
- filePath: src // src可以是录音文件临时路径
- })
新版录音
获取全局唯一的录音管理器,然后录音都依赖他,而播放录音则需要内部 audio 上下文 innerAudioContext 对象。
获取全局唯一的录音管理器:
- var that = this;
- this.recorderManager = wx.getRecorderManager();
- this.recorderManager.onError(function(){
- // 录音失败的回调处理
- });
- this.recorderManager.onStop(function(res){
- // 停止录音之后,把录取到的音频放在res.tempFilePath
- that.setData({
- src: res.tempFilePath
- })
- console.log(res.tempFilePath )
- });
开始录音:
- this.recorderManager.start({
- format: 'mp3' // 如果录制acc类型音频则改成aac
- });
结束录音:
- this.recorderManager.stop()
播放音频:
- this.innerAudioContext = wx.createInnerAudioContext();
- this.innerAudioContext.onError((res) => {
- // 播放音频失败的回调
- })
- this.innerAudioContext.src = this.data.src; // 这里可以是录音的临时路径
- this.innerAudioContext.play()
DEMO地址
github: https://github.com/yubang/appletRecordDemo
总结
以上所述是小编给大家介绍的微信小程序录音与播放录音功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!