小程序实现录音功能
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
⼩程序实现录⾳功能本⽂实例为⼤家分享了⼩程序实现录⾳功能的具体代码,供⼤家参考,具体内容如下⾸先判断权限
getPermission: function() {
var that = this;
wx.getSetting({
success(res) {
console.log(res.authSetting)
if (res.authSetting["scope.record"] === false) {
wx.showModal({
title: '是否录⾳',
content: '是否录⾳',
success: function (tip) {
if (tip.confirm) {
wx.openSetting({
success: function (data) {
if (data.authSetting["scope.record"] === true) {
wx.showToast({
title: '授权成功',
icon: 'success',
duration: 1000
})
that.startLuYin()
//授权成功之后,再调⽤chooseLocation选择地⽅
} else {
wx.showToast({
title: '授权失败',
icon: 'success',
duration: 1000
})
}
}
})
}
}
})
}else{
that.startLuYin()
}
}
})
},
授权成功后开始录⾳
startLuYin(){
const options = {
duration: 10000 * 6 * 10, //指定录⾳的时长,单位 ms
sampleRate: 16000, //采样率
numberOfChannels: 1, //录⾳通道数
encodeBitRate: 96000, //编码码率
format: 'mp3', //⾳频格式,有效值 aac/mp3
frameSize: 50, //指定帧⼤⼩,单位 KB
}
//开始录⾳
recorderManager.start(options);
recorderManager.onStart(() => {
console.log('recorder start');
Countdown(this); //开始计时
});
//错误回调
recorderManager.onError((res) => {
console.log('recorder出错:' + res);
console.log(res);
clearTimeout(timer); //出错时停⽌计时
})
},
暂停录⾳
// 暂停录⾳
pause: function() {
var that = this;
recorderManager.pause()
recorderManager.onPause((res) => {
console.log(res)
console.log('暂停录⾳')
clearTimeout(timer);
})
},
继续录⾳
//继续录⾳
jixu: function() {
var that = this;
recorderManager.resume()
Countdown(that); //开始计时
recorderManager.onResume((res) => {
})
},
停⽌录⾳
//停⽌录⾳
stop: function() {
recorderManager.stop();
recorderManager.onStop((res) => {
this.tempFilePath = res.tempFilePath;
console.log('停⽌录⾳', res.tempFilePath)
clearTimeout(timer);
})
},
播放声⾳
//播放声⾳
play: function() {
innerAudioContext.autoplay = true
innerAudioContext.src = this.tempFilePath,
innerAudioContext.onPlay(() => {
console.log('开始播放')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
},
// 倒计时
function Countdown(that) {
timer = setTimeout(function() {
console.log("----secondes----" + formatSeconds(secondes)); secondes++;
if (secondes >= 600) {
recorderManager.stop();
clearTimeout(timer);
}
that.setData({
times: formatSeconds(secondes)
});
Countdown(that);
}, 1000);
};
// 时间展⽰
function formatSeconds(value) {
var secondTime = parseInt(value); // 秒
var minuteTime = 0; // 分
var hourTime = 0; // ⼩时
if (secondTime > 60) { //如果秒数⼤于60,将秒数转换成整数 //获取分钟,除以60取整数,得到整数分钟
minuteTime = parseInt(secondTime / 60);
//获取秒数,秒数取佘,得到整数秒数
secondTime = parseInt(secondTime % 60);
//如果分钟⼤于60,将分钟转换成⼩时
if (minuteTime > 60) {
//获取⼩时,获取分钟除以60,得到整数⼩时
hourTime = parseInt(minuteTime / 60);
//获取⼩时后取佘的分,获取分钟除以60取佘的分
minuteTime = parseInt(minuteTime % 60);
}
}
var result;
//时间的展⽰⽅式为00:00
if (secondTime < 10) {
result = "0" + parseInt(secondTime);
} else {
result = "" + parseInt(secondTime);
}
if (minuteTime > 0) {
if (minuteTime < 10) {
result = "0" + parseInt(minuteTime) + ":" + result;
} else {
result = "" + parseInt(minuteTime) + ":" + result;
}
} else {
result = "00:" + result;
}
//由于限制时长最多为三分钟,⼩时⽤不到
if (hourTime > 0) {
result = "" + parseInt(hourTime) + ":" + result;
}
return result;
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。