miniprogram-demo/packageAPI/pages/voice/voice.js

66 lines
1.2 KiB
JavaScript
Raw Permalink Normal View History

2020-12-09 16:48:34 +08:00
// pages/API/voice/voice.js
const util = require('../../../utils/util.js')
let playTimeInterval
let recordTimeInterval
Page({
data: {
recording: false, // 录音中
filePath: '',
},
onHide() {
if (this.data.recording) {
wx.stopRecord() // 结束录音
}
},
startRecord() {
this.setData({
recording: true,
})
2020-12-09 17:27:35 +08:00
const that = this;
2020-12-09 16:48:34 +08:00
wx.startRecord({
success (res) {
2020-12-09 17:16:30 +08:00
console.log(res);
2020-12-09 16:48:34 +08:00
const tempFilePath = res.tempFilePath
2020-12-09 17:27:35 +08:00
that.setData({
2020-12-09 16:48:34 +08:00
filePath: tempFilePath,
})
2020-12-09 17:16:30 +08:00
},
fail(res) {
console.log(res);
2020-12-09 16:48:34 +08:00
}
})
},
stopRecord() {
this.setData({
recording: false,
})
wx.stopRecord() // 结束录音
},
playVoice() {
wx.playVoice({
filePath: this.data.filePath,
success: (res) => {
console.log('播放成功', res);
},
fail: (res) =>{
console.log('播放失败', res);
}
})
},
stopVoice() {
wx.stopVoice({
success: (res) => {
console.log('播放成功', res);
},
fail: (res) =>{
console.log('播放失败', res);
}
})
},
pauseVoice() {
wx.pauseVoice();
}
})