You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
226 lines
5.5 KiB
226 lines
5.5 KiB
import {
|
|
wxUploadFile,
|
|
wxChooseImage,
|
|
wxScanCode
|
|
} from "../../utils/promise-wx-api";
|
|
const recorderManager = wx.getRecorderManager();
|
|
let timer:any
|
|
Component({
|
|
/**
|
|
* 组件的属性列表
|
|
*/
|
|
properties: {},
|
|
|
|
/**
|
|
* 组件的初始数据
|
|
*/
|
|
data: {
|
|
overDuration: 600000,
|
|
duration: 0,
|
|
url: '',
|
|
hasStop: false,
|
|
hasStart: false, // 录音开始 暂停
|
|
isStart: false, // 是否开启录音
|
|
recordingTime: '00:00',
|
|
recordingLength: 0,
|
|
setInter: null
|
|
},
|
|
|
|
/**
|
|
* 组件的方法列表
|
|
*/
|
|
methods: {
|
|
recordingTimer() {
|
|
|
|
clearInterval(timer)
|
|
timer = null
|
|
|
|
timer = setInterval(() => {
|
|
let { overDuration, recordingLength } = this.data
|
|
if (recordingLength * 1000 >= overDuration) {
|
|
wx.showToast({
|
|
title: '录音已超时,已停止录音',
|
|
icon: 'none',
|
|
duration: 1500
|
|
})
|
|
console.log('lllllllll超时了')
|
|
recorderManager.stop();
|
|
clearInterval(timer)
|
|
timer = null
|
|
this.setData({
|
|
hasStart: false
|
|
})
|
|
return
|
|
}
|
|
let time = this.data.recordingLength + 1;
|
|
this.setData({
|
|
recordingLength: time,
|
|
recordingTime: this.formatTime(time)
|
|
});
|
|
console.log('timer,还在执行')
|
|
}, 1000);
|
|
},
|
|
// 录音开始
|
|
recordStart() {
|
|
this.setData({
|
|
hasStart: true,
|
|
});
|
|
const options = {
|
|
duration: this.data.overDuration,
|
|
format: 'mp3',
|
|
type:'voice'
|
|
};
|
|
this.recordingTimer()
|
|
recorderManager.start(options);
|
|
recorderManager.onStart(res => {
|
|
console.log('recorder start', res);
|
|
});
|
|
recorderManager.onPause(res => {
|
|
console.log('onPause', res)
|
|
})
|
|
recorderManager.onResume(res => {
|
|
console.log('onResume', res)
|
|
})
|
|
recorderManager.onStop((res) => {
|
|
console.log('recorder stop', res);
|
|
const { tempFilePath, duration } = res;
|
|
this.setData({
|
|
hasStop: true,
|
|
url: tempFilePath,
|
|
duration
|
|
})
|
|
//
|
|
});
|
|
recorderManager.onFrameRecorded(res => {
|
|
console.log('onFrameRecorded', res)
|
|
})
|
|
recorderManager.onError((res) => {
|
|
console.log('recorder onError', res);
|
|
wx.showToast({
|
|
title: res.errMsg,
|
|
icon: 'none',
|
|
duration: 1500
|
|
})
|
|
});
|
|
},
|
|
// 录音暂停
|
|
recortPause() {
|
|
recorderManager.pause();
|
|
this.setData({
|
|
hasStart: false
|
|
})
|
|
clearInterval(timer)
|
|
timer = null
|
|
},
|
|
// 录音继续
|
|
recortResume() {
|
|
this.recordingTimer()
|
|
recorderManager.resume();
|
|
this.setData({
|
|
hasStart: true
|
|
})
|
|
|
|
},
|
|
// 录音结束
|
|
recordStop() {
|
|
const { url, duration, hasStop } = this.data
|
|
this.setData({
|
|
hasStart: false
|
|
})
|
|
wx.showLoading({
|
|
title: '录音上传中...'
|
|
})
|
|
|
|
console.log(url, duration, hasStop)
|
|
|
|
if (hasStop) {
|
|
this.uploadRecord(url, 600000)
|
|
console.log('hasStop', hasStop)
|
|
} else {
|
|
console.log('hasStopeee', hasStop)
|
|
recorderManager.stop();
|
|
clearInterval(timer)
|
|
timer = null
|
|
recorderManager.onStop((res) => {
|
|
console.log('recorder stop1111', res);
|
|
const { tempFilePath, duration } = res;
|
|
this.uploadRecord(tempFilePath, duration)
|
|
//
|
|
});
|
|
}
|
|
|
|
},
|
|
async uploadRecord(file, duration) {
|
|
const {
|
|
data: { data },
|
|
msg
|
|
} = await wxUploadFile("oss/file/uploadvoice", file, {
|
|
// isMock: true
|
|
});
|
|
wx.hideLoading()
|
|
console.log('ressss', data, msg)
|
|
if (msg === "success" && data.code === 0) {
|
|
let { recordingLength } = this.data
|
|
if (duration - (recordingLength * 1000) > 1000 ) duration = recordingLength * 1000
|
|
this.setData({
|
|
hasStop: false
|
|
})
|
|
wx.showToast({
|
|
title: '上传成功',
|
|
icon: 'none',
|
|
duration: 1500
|
|
})
|
|
this.triggerEvent('finish', { url: data.data.url, duration})
|
|
} else {
|
|
wx.showToast({
|
|
title: msg,
|
|
icon: 'none',
|
|
duration: 1500
|
|
})
|
|
}
|
|
},
|
|
formatTime(num) {
|
|
let min = parseInt(num/60)
|
|
let second = num%60
|
|
min = min >= 10 ? min : '0' + min
|
|
second = second >= 10 ? second : '0' + second
|
|
return min + ':' + second
|
|
},
|
|
handleRecord() {
|
|
let { hasStart } = this.data
|
|
if (hasStart) this.recortPause()
|
|
else this.recortResume()
|
|
},
|
|
handleOpenRecord() {
|
|
this.setData({
|
|
isStart: true,
|
|
});
|
|
console.log('opppppp')
|
|
this.recordStart()
|
|
},
|
|
handleRecordDel() {
|
|
const { hasStop, isStart } = this.data
|
|
console.log('hasStop---', this.data.hasStop)
|
|
if (isStart && !hasStop) {
|
|
recorderManager.stop()
|
|
}
|
|
// if (!hasStop && hasStart) recorderManager.stop();
|
|
// recorderManager.stop();
|
|
clearInterval(timer)
|
|
timer = null
|
|
this.setData({
|
|
recordingTime: '00:00',
|
|
recordingLength: 0,
|
|
hasStart: false,
|
|
isStart: false,
|
|
hasStop: false,
|
|
url: '',
|
|
duration: 0
|
|
})
|
|
},
|
|
hancleCancle() {
|
|
this.handleRecordDel()
|
|
this.triggerEvent('cancle', false);
|
|
},
|
|
},
|
|
});
|
|
|