From 383d40df3cfbbddae3621bc2961d234396a1637f Mon Sep 17 00:00:00 2001 From: xwenliang Date: Mon, 5 Dec 2022 11:11:26 +0800 Subject: [PATCH] feat: add type for list & move failed from list --- pages/index/index.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/pages/index/index.js b/pages/index/index.js index 142a793..4f55f3c 100644 --- a/pages/index/index.js +++ b/pages/index/index.js @@ -2,7 +2,7 @@ //获取应用实例 const app = getApp(); const selectedIndex = app.globalData.getSelectedTypeIndex(); -const type = app.globalData.getTypeList(); +const typeList = app.globalData.getTypeList(); let audio = null; @@ -10,7 +10,7 @@ Page({ data: { placeholder: '点击此处输入文字', text: '', - type, + typeList, selectedIndex: String(selectedIndex) ? selectedIndex : 5 }, onLoad(){ @@ -21,7 +21,10 @@ Page({ }, async play(){ const text = this.data.text || this.data.placeholder; - const url = this.data.type[this.data.selectedIndex].createUrl(text); + const selectedType = this.data.typeList[this.data.selectedIndex]; + const url = selectedType.createUrl(text); + const type = selectedType.desc; + wx.showLoading({ title: '正在合成语音...', mask: true @@ -57,14 +60,15 @@ Page({ this.resetAudio(); audio = wx.createInnerAudioContext({useWebAudioImplement: true}); audio.autoplay = true; - // 播放失败要移除缓存内容 - audio.onError((res) => { - console.log(res.errMsg) - console.log(res.errCode) - }); // 检查是否播放过 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); @@ -79,6 +83,8 @@ Page({ app.globalData.setAudioList('add', { // 音频文本 text, + // 音频音效 + type, // 音频网络地址 url, // 音频本地地址 -- libgit2 0.22.2