DBMNG数据库管理与应用

抓住自己最有兴趣的东西,由浅入深,循序渐进地学……
当前位置:首页 > 移动应用 > 微信开发

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

小程序中提供了两种录音的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




链接:https://www.jianshu.com/p/2c297ed3ed56


本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号