小程序实现录音格式为mp3,并上传到云开发环境

⼩程序实现录⾳格式为mp3,并上传到云开发环境
前⾔
⼩程序中可以实现录⾳的主要有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 = pFilePath
4        console.log(res)
5      }
6    })
7    setTimeout(function () {
8      wx.stopRecord() // 结束录⾳
9    }, 3000)
成功录⾳的返回值为⼀个含有⾳频临时路径的对象
1 errMsg: "startRecord:ok"
2 tempFilePath: "tmp/wx88e053d45b28e2cf.o6zAJs-nrru-YZpqRQeb-X8EzBfk.JVhmiR78K4oY2e7522995230f041a81c5967a4e1598c.silk"
这个silk格式为加密格式,在真机上可以播放,上传到服务器以后,其它⽤户⽆法播放,只有上传者可以播放。
如果要分享给别⼈,得先解密,再转换为其它格式,⽹上的教程很多,但是⽐较⿇烦
⼆、关于wx.getRecorderManager()的实战解析
有⼀个项⽬,需要使⽤录⾳,上传到云存储后,分享给其它⼈。
1 const recorderManager = wx.getRecorderManager()
2 const backgroundAudio = wx.getBackgroundAudioManager()
3var 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() {
15var that = this;
16//将计时器赋值给setInter
17    that.data.setInter = setInterval(
18function() {
19var time = dingTimeqwe + 1;
20        that.setData({
21          recordingTimeqwe: time
22        })
23      }, 1000);
24  },
25
26
27//开始录⾳
27//开始录⾳
28  openRecording: function() {
29var 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, //指定录⾳的时长,单位 ms,最⼤为10分钟(600000),默认为1分钟(60000)
40      sampleRate: 16000, //采样率
41      numberOfChannels: 1, //录⾳通道数
42      encodeBitRate: 96000, //编码码率
43      format: 'mp3', //⾳频格式,有效值 aac/mp3
44      frameSize: 50, //指定帧⼤⼩,单位 KB
45    }
46//开始录⾳计时
47    dingTimer();
48//开始录⾳
49    recorderManager.start(options);
50    Start(() => {
51      console.log('。。。开始录⾳。。。')
52    });
53//错误回调
54    Error((res) => {
55      console.log(res);
56    })
57  },
58
59//结束录⾳
60  shutRecording: function() {
61var that = this;
62    wx.getSystemInfo({
63      success: function(res) {
64        that.setData({
65          shutRecordingdis: "none",
66          openRecordingdis: "block"
67        })
68      }
69    })
70    recorderManager.stop();
71    Stop((res) => {
72      const that = this
73      let timestamp = util.formatTime2(new Date());
74      console.log('。。停⽌录⾳。。', pFilePath)
75      const {
76        tempFilePath
77      } = res;
78//结束录⾳计时
79      clearInterval(that.data.setInter);
80      wx.cloud.uploadFile({
81        cloudPath: "sounds/"+timestamp + '-' + this.randomNum(10000, 99999) + '.mp3',
82        filePath: tempFilePath,
83// 成功回调
84        success: res => {
85          console.log('上传成功', res)
86          that.setData({
87            soundUrl: res.fileID,
88// time: util.formatTime1(new Date())
89          })
90        },
91      })
92
92
93    })
94  },
95
96//录⾳播放
97  recordingAndPlaying: function(eve) {
98
99// console.log(eve)
100var tempsound = eve.currentTarget.dataset.soundid
101    tempsound = "b.qcloud.la/sounds"+this.midstr(tempsound) 102// console.log(tempsound)
103    wx.playBackgroundAudio({
104//播放地址
105      dataUrl: tempsound
106    })
107  },
108
109//⽣成从minNum到maxNum的随机数
110  randomNum(minNum, maxNum) {
111switch (arguments.length) {
112case 1:
113return parseInt(Math.random() * minNum + 1, 10);
114break;
115case 2:
116return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
117break;
118default:
119return 0;
120break;
121    }
122  },
123  midstr(str) {
124var strnum = str.lastIndexOf('/')
125var ministr = str.substr(strnum)
126return ministr
127  },
128 })
1.⾸先声明录⾳组件
const recorderManager = wx.getRecorderManager()
2. 开始录⾳的实现
5      success: function(res) {
6        that.setData({
7          shutRecordingdis: "block",
8          openRecordingdis: "none"
9        })
10      }
11    })
12    const options = {
13      duration: 60000, //指定录⾳的时长,单位 ms,最⼤为10分钟(600000),默认为1分钟(60000)
14      sampleRate: 16000, //采样率
15      numberOfChannels: 1, //录⾳通道数
16      encodeBitRate: 96000, //编码码率
17      format: 'mp3', //⾳频格式,有效值 aac/mp3
18      frameSize: 50, //指定帧⼤⼩,单位 KB
19    }
20//开始录⾳计时
21    dingTimer();
22//开始录⾳
23    recorderManager.start(options);
24    Start(() => {
25      console.log('。。。开始录⾳。。。')
26    });
27//错误回调
28    Error((res) => {
29      console.log(res);
30    })
31  },
3. 结束录⾳的实现
5      success: function(res) {
6        that.setData({
7          shutRecordingdis: "none",
8          openRecordingdis: "block"
9        })
10      }
11    })
12    recorderManager.stop();
13    Stop((res) => {
14      const that = this
15      let timestamp = util.formatTime2(new Date());
16      console.log('。。停⽌录⾳。。', pFilePath)
17      const {
18        tempFilePath
19      } = res;
20//结束录⾳计时
21      clearInterval(that.data.setInter);
22      wx.cloud.uploadFile({
23        cloudPath: "sounds/"+timestamp + '-' + this.randomNum(10000, 99999) + '.mp3',
24        filePath: tempFilePath,
25// 成功回调
26        success: res => {
27          console.log('上传成功', res)
28          that.setData({
29            soundUrl: res.fileID,
30// time: util.formatTime1(new Date())
31          })
32        },
33      })
34    })
35  },
第13⾏,录⾳停⽌后,⽣成mp3格式的临时⽂件,以jason格式提⽰,包含时长,⽂件⼤⼩和临时⽂件名
1 {
2 duration: 2532
3 fileSize: 42268
4 tempFilePath: "tmp/wx88e053d45b28e2cf.o6zAJs-nrru-YZpqRQeb-
X8EzBfk.73z3a3qIwC7yc13f32e3d179133ac77ca7851ec7d25b.durationTime=2532.mp3"
5 }
第15⾏,⽣成⼀个时间戳,⽤来⽣成⽂件名,
第22⾏,上传⾄云存储,为了避免出现同时有多个进程上传的极端情况,加了⼀个5位数的随机数,
第29⾏,上传成功后,将⽣成的云⽂件ID返给data变量soundUrl
1 {
2 errMsg: "cloud.uploadFile:ok"
3 fileID: "cloud://newdj-d79af2.6e65-newdj-d79af2-1257790921/sounds/20190731162324-40454.mp3"
4 }
4.播放云存储⾥的录⾳

本文发布于:2024-09-20 15:38:17,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/3/91070.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:格式   播放   开始   时长   图标   传到   进程
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议