const audio = wx.createInnerAudioContext()
|
const recorderManager = wx.getRecorderManager()
|
Page({
|
data: {
|
isRefreshing: false,
|
currentTab: 0,
|
recordingSrc: '',
|
isRecording: false,
|
isshowVoiceMask: false, //是否显示录音中
|
isShowVoiceView: false,
|
voiceTime: 0, //录音时长
|
maxVoiceTime: 60, //最长录音时间
|
contDownTime: 0,
|
line2Opcity: 1,
|
line3Opcity: 1,
|
gridConfig: {
|
column: 3,
|
width: 160,
|
height: 160,
|
},
|
originFiles: [{
|
url: 'https://tdesign.gtimg.com/mobile/demos/example4.png',
|
name: 'uploaded1.png',
|
type: 'image',
|
},
|
{
|
url: 'https://tdesign.gtimg.com/mobile/demos/example6.png',
|
name: 'uploaded2.png',
|
type: 'image',
|
}
|
],
|
feedBackList: Array(19).fill({
|
createTime: "2023-05-06 12:36:25",
|
responseTime: "2023-05-07 12:36:25"
|
})
|
},
|
|
onLoad() {
|
wx.getSetting({
|
success: (res) => {
|
if (!res.authSetting['scope.record']) {
|
// 如果用户没有授权录音权限,发起授权窗口
|
wx.authorize({
|
scope: 'scope.record',
|
success() {
|
// 用户已经同意小程序使用录音功能,可以继续操作
|
console.log('用户已授权录音');
|
},
|
fail() {
|
// 用户拒绝授权,可以给出提示或再次发起授权窗口
|
console.log('用户拒绝了授权录音');
|
}
|
});
|
} else {
|
// 用户已经授权录音,可以直接进行录音操作
|
console.log('用户已授权录音');
|
}
|
}
|
});
|
var that = this;
|
recorderManager.onStop((res) => {
|
console.log('录音停止', voiceTime);
|
const voiceTime = Math.floor(res.duration / 1000)
|
if (voiceTime < 2) {
|
this.setData({
|
isShowVoiceMask: false,
|
contDownTime: this.data.maxVoiceTime,
|
isShowVoiceView: false
|
})
|
wx.showToast({
|
title: '录音时间过短',
|
icon: 'error',
|
time: 3000
|
})
|
} else {
|
|
that.setData({
|
voiceTime: voiceTime,
|
isShowVoiceMask: false,
|
contDownTime: this.data.maxVoiceTime,
|
isShowVoiceView: true
|
})
|
console.log('res', res);
|
const {
|
tempFilePath
|
} = res; //这里松开按钮 会返回录音本地路径
|
audio.src = tempFilePath
|
console.log(tempFilePath);
|
}
|
|
//上传录制的音频到服务器
|
// wx.uploadFile({
|
// url: '接口地址' + api.voice, //接口地址
|
// name: 'file', //上传文件名
|
// filePath: tempFilePath,
|
// success: function (res) { //后台返回给前端识别后的文字
|
// var model = res.data
|
// var modeljson = JSON.parse(model)
|
// if (modeljson.status_code == 500) {
|
// wx.showToast({
|
// title: '语音转换失败',
|
// image: '/assets/image/icon/fail@2x.png'
|
// })
|
// return false;
|
// }
|
// if (modeljson.meta.status_code === 200 && !modeljson.data.err_msg) {
|
// var saymessage = modeljson.data.message;
|
// wx.setStorageSync('sayinfo', saymessage)
|
// that.setData({
|
// inpvalue: saymessage
|
// })
|
// setTimeout(() =>{
|
// wx.navigateTo({
|
// url: '../loding/loding'
|
// })
|
|
// },2000)
|
// setTimeout(() => {
|
// wx.hideLoading();
|
// }, 100)
|
// } else if (modeljson.data.err_msg) {
|
// wx.showToast({
|
// title: '请大声说话',
|
// image: '/assets/image/icon/fail@2x.png'
|
// })
|
// return false;
|
// }
|
// }
|
// })
|
});
|
recorderManager.onStart(() => {
|
console.log('录音开始');
|
});
|
recorderManager.onError((err) => {
|
console.log('录音错误', err);
|
});
|
},
|
handleRemove(e) {
|
console.log("handleRemove");
|
const {
|
index
|
} = e.detail;
|
const {
|
originFiles
|
} = this.data;
|
originFiles.splice(index, 1);
|
this.setData({
|
originFiles,
|
});
|
},
|
handleClick() {
|
console.log("handleClick");
|
},
|
handleAdd(e) {
|
console.log("handleAdd");
|
const {
|
files
|
} = e.detail;
|
const {
|
originFiles
|
} = this.data;
|
this.setData({
|
originFiles: [...originFiles, ...files], // 此时设置了 fileList 之后才会展示选择的图片
|
});
|
},
|
/**
|
* 上传图片
|
*/
|
onUpload() {
|
wx.uploadFile({
|
url: 'https://example.weixin.qq.com/upload', // 仅为示例,非真实的接口地址
|
filePath: file.url,
|
name: 'file',
|
formData: {
|
user: 'test'
|
},
|
success: () => {
|
this.setData({
|
[`fileList[${length}].status`]: 'done',
|
});
|
},
|
});
|
},
|
//按住按钮
|
startHandel() {
|
this.setData({
|
isShowVoiceMask: true,
|
contDownTime: this.data.maxVoiceTime,
|
voiceTime: 0,
|
isShowVoiceView: false
|
})
|
console.log("开始录音")
|
recorderManager.start({
|
duration: 0
|
})
|
this.startRecordingCountdown();
|
},
|
//松开按钮
|
endHandle() {
|
this.stopRecordingCountdown();
|
console.log("结束")
|
//触发录音停止
|
recorderManager.stop()
|
},
|
// 播放
|
handlePlay(e) {
|
// 倒计时
|
let time = this.data.voiceTime
|
audio.play()
|
let timer1 = -1,
|
timer2 = -1,
|
timer3 = -1,
|
timer4 = -1,
|
timer5 = -1,
|
timer6 = -1;
|
// 第一次播放为0 第二次播放2秒钟
|
timer6 = setInterval(() => {
|
console.log('时间', time);
|
if (time <= 0) {
|
this.setData({
|
line2Opcity: 1,
|
line3Opcity: 1
|
})
|
clearInterval(timer6)
|
clearTimeout(timer2)
|
clearTimeout(timer3)
|
clearTimeout(timer4)
|
clearTimeout(timer5)
|
return
|
}
|
timer2 = setTimeout(() => {
|
console.log('timer2');
|
this.setData({
|
line2Opcity: 0,
|
line3Opcity: 0
|
})
|
}, 200)
|
timer3 = setTimeout(() => {
|
console.log('timer3');
|
this.setData({
|
line2Opcity: 1,
|
line3Opcity: 0
|
})
|
}, 400)
|
timer4 = setTimeout(() => {
|
console.log('timer4');
|
this.setData({
|
line2Opcity: 1,
|
line3Opcity: 1
|
})
|
}, 600)
|
timer5 = setTimeout(() => {
|
console.log('timer5');
|
this.setData({
|
line2Opcity: 0,
|
line3Opcity: 0
|
})
|
}, 800)
|
}, 800)
|
// 倒计时
|
timer1 = setInterval(() => {
|
time--;
|
if (time <= 0) {
|
clearInterval(timer1)
|
return
|
}
|
}, 1000)
|
|
},
|
//开启定时器
|
startRecordingCountdown() {
|
console.log("startRecordingCountdown");
|
const that = this;
|
this.recordingInterval = setInterval(() => {
|
const newVoiceTime = that.data.voiceTime + 1;
|
const newContDownTime = that.data.contDownTime - 1;
|
that.setData({
|
voiceTime: newVoiceTime,
|
contDownTime: newContDownTime
|
});
|
if (this.data.voiceTime >= this.data.maxVoiceTime) {
|
that.stopRecordingCountdown();
|
that.endHandle();
|
}
|
}, 1000);
|
},
|
|
stopRecordingCountdown() {
|
console.log("stopRecordingCountdown");
|
if (this.recordingInterval) {
|
clearInterval(this.recordingInterval);
|
this.recordingInterval = null;
|
console.log("Recording countdown stopped");
|
}
|
}, // 切换 Tabs
|
switchTab: function (e) {
|
const tab = parseInt(e.currentTarget.dataset.tab);
|
this.setData({
|
currentTab: tab
|
});
|
},
|
onPullDownRefresh() {
|
this.setData({
|
isRefreshing: false
|
});
|
}
|
|
});
|