From 3765f9b4871cce0918cc0ce4eb1bef2762543f37 Mon Sep 17 00:00:00 2001 From: xwenliang Date: Mon, 5 Dec 2022 11:22:46 +0800 Subject: [PATCH] chore: move local play to front of text check --- pages/index/index.js | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/pages/index/index.js b/pages/index/index.js index 4f55f3c..eaeab51 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -24,12 +24,37 @@ Page({ const selectedType = this.data.typeList[this.data.selectedIndex]; const url = selectedType.createUrl(text); const type = selectedType.desc; + + this.resetAudio(); + audio = wx.createInnerAudioContext({useWebAudioImplement: true}); + audio.autoplay = true; + // 检查是否播放过 + const audioList = app.globalData.getAudioList(); + const played = audioList.find(v => v.url === url); + // 播放失败要移除缓存内容 + audio.onError(err => { + app.globalData.setAudioList('delete', played.path); + wx.showModal({ + title: '提示', + content: '该语音播放过但缓存失效,点击确定重新播放', + showCancel: false, + success: res => { + this.play(); + } + }); + }); + // 播放过,使用本地文件减少请求,且从播放列表中移到最前 + if(played){ + app.globalData.setAudioList('delete', played.path); + app.globalData.setAudioList('add', played); + return audio.src = played.path; + } + // 检查文本是否合规 wx.showLoading({ title: '正在合成语音...', mask: true }); - // 检查文本是否合规 const result = await new Promise((resolve, reject) => { app.request({ url: `${app.globalData.domain}/open-api/wechat-msg-sec-check`, @@ -46,9 +71,9 @@ Page({ } }); }); - wx.hideLoading(); // 不合规结束 if(result.suggest !== 'pass'){ + wx.hideLoading(); return wx.showToast({ title: '内容不合规', icon: 'error', @@ -56,29 +81,12 @@ Page({ duration: 2000 }); } - - this.resetAudio(); - audio = wx.createInnerAudioContext({useWebAudioImplement: true}); - audio.autoplay = true; - // 检查是否播放过 - const audioList = app.globalData.getAudioList(); - const played = audioList.find(v => v.url === url); - // 播放失败要移除缓存内容 - audio.onError(err => { - if(err.errCode){ - app.globalData.setAudioList('delete', played.path); - } - }); - // 播放过,使用本地文件减少请求,且从播放列表中移到最前 - if(played){ - app.globalData.setAudioList('delete', played.path); - app.globalData.setAudioList('add', played); - return audio.src = played.path; - } + // 未播放过,先下载再播放 wx.downloadFile({ url, success (res) { + wx.hideLoading(); audio.src = res.tempFilePath; app.globalData.setAudioList('add', { // 音频文本 -- libgit2 0.22.2