微信小程序录音与播放录音功能

小程序中提供了两种录音的API

旧版录音功能

首先启动录音,然后停止录音即可拉到音频的临时地址

启动录音:

rush:js;"> var that = this; wx.startRecord({ success: function (res) { // 调用了停止录音接口就会触发这个函数,res.tempFilePath为录音文件临时路径 var tempFilePath = res.tempFilePath that.setData({ src: tempFilePath }) },fail: function (res) { //录音失败的处理函数 } })

停止录音:

rush:js;"> wx.stopRecord()

播放录音:

rush:js;"> wx.playVoice({ filePath: src // src可以是录音文件临时路径 })

新版录音

获取全局唯一的录音管理器,然后录音都依赖他,而播放录音则需要内部 audio 上下文 innerAudioContext 对象。

获取全局唯一的录音管理器:

rush:js;"> 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 ) });

开始录音:

rush:js;"> this.recorderManager.start({ format: 'mp3' // 如果录制acc类型音频则改成aac });

结束录音:

rush:js;"> this.recorderManager.stop()

播放音频

{ // 播放音频失败的回调 }) this.innerAudioContext.src = this.data.src; // 这里可以是录音的临时路径 this.innerAudioContext.play()

DEMO地址

github: nofollow" href="https://github.com/yubang/appletRecordDemo">https://github.com/yubang/appletRecordDemo

总结

以上所述是小编给大家介绍的微信小程序录音与播放录音功能。编程之家 jb51.cc 收集整理的教程希望能对你有所帮助,如果觉得编程之家不错,可分享给好友!感谢支持

相关文章

开发微信小程序的用户授权登录功能
小程序开发页面如何实现跳转?
浅谈小程序开发中蓝牙连接错误分析及解决方法
什么是小程序?它有哪些功能?
如何配置小程序开发项目结构?(教程)
怎么把自己的店加入小程序