2018-05-10 17:48 
DanielWise 
阅读(27708
评论(1
编辑 
收藏 
举报

小程序 录音

  最近在写一个小程序(原创诗词平台,名字叫作“诗人呀”)的功能,需求是需要把用户的录音记录并上传到服务器,然后调用接口来实现播放功能。

翻阅不少资料都发现小程序对wx.startRecord()接口不在维护,

注意:1.6.0 版本开始,本接口不再维护。建使用能力更 wx.getRecorderManager 接口

需要使用新的接口来处理,官方文档又不符合我的需求,所以就决定自己动手来实现一下录音播放功能。

因此我们使用的使用 wx.getRecorderManager 接口:

首先使用方法获取对象

  1. const recorderManager = wx.getRecorderManager()
  2. const innerAudioContext = wx.createInnerAudioContext()

然后写一个button来调用开始录音的方法。

  1. //开始录音的时候
  2. start: function () {
  3. const options = {
  4. duration: 10000,//指定录音的时长,单位 ms
  5. sampleRate: 16000,//采样率
  6. numberOfChannels: 1,//录音通道数
  7. encodeBitRate: 96000,//编码码率
  8. format: \'mp3\',//音频格式,有效值 aac/mp3
  9. frameSize: 50,//指定帧大小,单位 KB
  10. }
  11. //开始录音
  12. recorderManager.start(options);
  13. recorderManager.onStart(() => {
  14. console.log(\'recorder start\')
  15. });
  16. //错误回调
  17. recorderManager.onError((res) => {
  18. console.log(res);
  19. })
  20. },

再写一个button来调用停止录音的方法。

  1. //停止录音
  2. stop: function () {
  3. recorderManager.stop();
  4. recorderManager.onStop((res) => {
  5. this.tempFilePath = res.tempFilePath;
  6. console.log(\'停止录音\', res.tempFilePath)
  7. const { tempFilePath } = res
  8. })
  9. },

最后写一个播放声音的方法

  1. //播放声音
  2. play: function () {
  3. innerAudioContext.autoplay = true
  4. innerAudioContext.src = this.tempFilePath,
  5. innerAudioContext.onPlay(() => {
  6. console.log(\'开始播放\')
  7. })
  8. innerAudioContext.onError((res) => {
  9. console.log(res.errMsg)
  10. console.log(res.errCode)
  11. })
  12. },

这样就完成新API的操作了,WXML代码如下:

  1. <button bindtap="start" class=\'btn\'>开始录音</button>
  2. <button bindtap="stop" class=\'btn\'>停止录音</button>
  3. <button bindtap="play" class=\'btn\'>播放录音</button>

但是我发现点击播放根本播放不出来。这是为什么呢,路径也可以打印出来,原来小程序返回的临时路径根本无法播放,

需要通过wx.uploadFile()方法来从后台获取处理好的mp3文件来进行播放。 

  1. 1. setTimeout(function () {  
  2. 2.      var urls = app.globalData.urls + "/Web/UpVoice";  
  3. 3.      console.log(s.data.recodePath);  
  4. 4.      wx.uploadFile({  
  5. 5.          url: urls,  
  6. 6.          filePath: s.data.recodePath,  
  7. 7.          name: \'file\',  
  8. 8.          header: {  
  9. 9.              \'content-type\': \'multipart/form-data\'  
  10. 10.          },  
  11. 11.          success: function (res) {  
  12. 12.              var str = res.data;  
  13. 13.              var data = JSON.parse(str);  
  14. 14.              if (data.states == 1) {  
  15. 15.                  var cEditData = s.data.editData;  
  16. 16.                  cEditData.recodeIdentity = data.identitys;  
  17. 17.                  s.setData({ editData: cEditData });  
  18. 18.              }  
  19. 19.              else {  
  20. 20.                  wx.showModal({  
  21. 21.                      title: \'提示\',  
  22. 22.                      content: data.message,  
  23. 23.                      showCancel: false,  
  24. 24.                      success: function (res) {  
  25. 25.   
  26. 26.                      }  
  27. 27.                  });  
  28. 28.              }  
  29. 29.              wx.hideToast();  
  30. 30.          },  
  31. 31.          fail: function (res) {  
  32. 32.              console.log(res);  
  33. 33.              wx.showModal({  
  34. 34.                  title: \'提示\',  
  35. 35.                  content: "网络请求失败,请确保网络是否正常",  
  36. 36.                  showCancel: false,  
  37. 37.                  success: function (res) {  
  38. 38.                  }  
  39. 39.              });  
  40. 40.              wx.hideToast();  
  41. 41.          }  
  42. 42.      }); 

将onStop中获取到的临时路径上传到你的服务器当中,进行处理语音识别和语义,将返回的结果放到audio播放组件中就可以播放音频了。

实现完成后的小程序部分截图如下:

如果大家对于其他非技术细节也感兴趣的话,欢迎点击此处访问。

版权声明:本文为danielWise原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/danielWise/p/9020884.html?ivk_sa=1024320u