1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
| Page({ data: { content: '', // 存储文本内容 recordState: 0, // 0: 未录音, 1: 录音中 recorderManager: null, // 录音管理器 innerAudioContext: null, // 音频播放管理器 recordTimer: null, // 录音计时器 recordDuration: 0, // 录音时长(秒) maxDuration: 600, // 最大录音时长(10分钟=600秒) tempFilePath: '', // 录音临时文件路径 hasRecord: false, // 是否有录音文件 playing: false, // 是否正在播放 uploading: false, // 是否正在上传 uploadResult: '' // 上传结果 },
onLoad: function () { // 初始化录音管理器和音频播放管理器 this.initRecorderManager(); this.initInnerAudioContext(); },
// 初始化录音管理器 initRecorderManager: function () { const recorderManager = wx.getRecorderManager(); this.setData({ recorderManager: recorderManager });
// 录音开始回调 recorderManager.onStart(() => { console.log('录音开始'); // 开始计时 this.startRecordTimer(); });
// 录音停止回调 recorderManager.onStop((res) => { console.log('录音停止', res); // 停止计时 this.stopRecordTimer(); // 重置录音时长 this.setData({ recordDuration: 0, recordState: 0, tempFilePath: res.tempFilePath, hasRecord: true }); });
// 录音错误回调 recorderManager.onError((err) => { console.error('录音错误', err); // 停止计时 this.stopRecordTimer(); // 重置录音时长和状态 this.setData({ recordDuration: 0, recordState: 0 }); wx.showToast({ title: '录音失败', icon: 'none' }); }); },
// 初始化音频播放管理器 initInnerAudioContext: function () { const innerAudioContext = wx.createInnerAudioContext(); this.setData({ innerAudioContext: innerAudioContext });
// 播放结束回调 innerAudioContext.onEnded(() => { console.log('播放结束'); this.setData({ playing: false }); });
// 播放错误回调 innerAudioContext.onError((err) => { console.error('播放错误', err); this.setData({ playing: false }); wx.showToast({ title: '播放失败', icon: 'none' }); }); },
// 开始录音计时 startRecordTimer: function () { this.setData({ recordTimer: setInterval(() => { this.setData({ recordDuration: this.data.recordDuration + 1 }); // 检查是否达到最大时长 if (this.data.recordDuration >= this.data.maxDuration) { // 自动停止录音 this.data.recorderManager.stop(); } }, 1000) }); },
// 暂停录音计时 pauseRecordTimer: function () { if (this.data.recordTimer) { clearInterval(this.data.recordTimer); this.setData({ recordTimer: null }); } },
// 停止录音计时 stopRecordTimer: function () { this.pauseRecordTimer(); },
// 开始录音 startRecord: function () { // 设置录音状态为录音中 this.setData({ recordState: 1 });
// 开始录音 const options = { duration: this.data.maxDuration * 1000, // 最大录音时长(毫秒) sampleRate: 16000, numberOfChannels: 1, encodeBitRate: 96000, format: 'mp3', frameSize: 50 }; this.data.recorderManager.start(options); },
// 结束录音 stopRecord: function () { // 停止录音 this.data.recorderManager.stop(); },
// 播放录音 playRecord: function () { if (this.data.playing) { // 如果正在播放,停止播放 this.data.innerAudioContext.stop(); this.setData({ playing: false }); } else { // 如果没有播放,开始播放 this.data.innerAudioContext.src = this.data.tempFilePath; this.data.innerAudioContext.play(); this.setData({ playing: true }); } },
// 上传录音到云服务器 uploadRecord: function () { if (!this.data.tempFilePath) { wx.showToast({ title: '没有录音文件', icon: 'none' }); return; }
// 设置上传状态 this.setData({ uploading: true, uploadResult: '' });
// 上传录音文件到云存储 wx.cloud.uploadFile({ cloudPath: `voice/${Date.now()}.mp3`, // 云存储路径 filePath: this.data.tempFilePath, // 本地临时文件路径 success: (res) => { console.log('文件上传成功,fileID:', res.fileID); this.setData({ uploadResult: '上传成功', uploading: false }); wx.showToast({ title: '上传成功', icon: 'success' }); }, fail: (err) => { console.error('文件上传失败', err); this.setData({ uploadResult: '上传失败:' + err.errMsg, uploading: false }); wx.showToast({ title: '上传失败', icon: 'none' }); } }); },
// 保存内容到本地存储 saveContent: function () { wx.setStorageSync('fastTestContent', this.data.content); },
// 生命周期函数--监听页面显示 onShow: function () { // 从本地存储获取内容 const content = wx.getStorageSync('fastTestContent') || ''; this.setData({ content: content }); },
// 生命周期函数--监听页面卸载 onUnload: function () { // 停止录音计时 this.stopRecordTimer(); // 停止录音 if (this.data.recorderManager) { this.data.recorderManager.stop(); } // 停止播放 if (this.data.innerAudioContext) { this.data.innerAudioContext.stop(); } } });
|