Small program to realize recording function
- 2021-08-28 18:49:39
- OfStack
This article example for everyone to share the small program to achieve the recording function of the specific code, for your reference, the specific content is as follows
First judge the authority
getPermission: function() {
var that = this;
wx.getSetting({
success(res) {
console.log(res.authSetting)
if (res.authSetting["scope.record"] === false) {
wx.showModal({
title: ' Recording or not ',
content: ' Recording or not ',
success: function (tip) {
if (tip.confirm) {
wx.openSetting({
success: function (data) {
if (data.authSetting["scope.record"] === true) {
wx.showToast({
title: ' Authorization succeeded ',
icon: 'success',
duration: 1000
})
that.startLuYin()
// After the authorization is successful, call the chooseLocation Choose a place
} else {
wx.showToast({
title: ' Authorization failure ',
icon: 'success',
duration: 1000
})
}
}
})
}
}
})
}else{
that.startLuYin()
}
}
})
},
Start recording after successful authorization
startLuYin(){
const options = {
duration: 10000 * 6 * 10, // Specify the duration and unit of recording ms
sampleRate: 16000, // Sampling rate
numberOfChannels: 1, // Number of recording channels
encodeBitRate: 96000, // Coding rate
format: 'mp3', // Audio format, valid value aac/mp3
frameSize: 50, // Specify frame size, unit KB
}
// Start recording
recorderManager.start(options);
recorderManager.onStart(() => {
console.log('recorder start');
Countdown(this); // Start timing
});
// Error callback
recorderManager.onError((res) => {
console.log('recorder Error :' + res);
console.log(res);
clearTimeout(timer); // Stop timing when an error occurs
})
},
Pause recording
// Pause recording
pause: function() {
var that = this;
recorderManager.pause()
recorderManager.onPause((res) => {
console.log(res)
console.log(' Pause recording ')
clearTimeout(timer);
})
},
Continue recording
// Continue recording
jixu: function() {
var that = this;
recorderManager.resume()
Countdown(that); // Start timing
recorderManager.onResume((res) => {
})
},
Stop recording
// Stop recording
stop: function() {
recorderManager.stop();
recorderManager.onStop((res) => {
this.tempFilePath = res.tempFilePath;
console.log(' Stop recording ', res.tempFilePath)
clearTimeout(timer);
})
},
Play sound
// Play sound
play: function() {
innerAudioContext.autoplay = true
innerAudioContext.src = this.tempFilePath,
innerAudioContext.onPlay(() => {
console.log(' Start playing ')
})
innerAudioContext.onError((res) => {
console.log(res.errMsg)
console.log(res.errCode)
})
},
// Countdown
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);
};
// Time display
function formatSeconds(value) {
var secondTime = parseInt(value); // Seconds
var minuteTime = 0; // Points
var hourTime = 0; // Hours
if (secondTime > 60) { // If the number of seconds is greater than 60 Converts the number of seconds to an integer
// Gets minutes, divided by 60 Take an integer to get an integer minute
minuteTime = parseInt(secondTime / 60);
// Get the number of seconds, take the number of seconds, and get the integer number of seconds
secondTime = parseInt(secondTime % 60);
// If minutes are greater than 60 Converts minutes into hours
if (minuteTime > 60) {
// Get hours, get minutes divided by 60 Get an integer hour
hourTime = parseInt(minuteTime / 60);
// Get the minutes after the hour, and get the minutes divided by 60 Take the points of She
minuteTime = parseInt(minuteTime % 60);
}
}
var result;
// The display mode of time is as follows 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;
}
// Because the limit duration is at most 3 Minutes , Less than an hour
if (hourTime > 0) {
result = "" + parseInt(hourTime) + ":" + result;
}
return result;
}