Commit 383d40df3cfbbddae3621bc2961d234396a1637f
1 parent
7498cb66
feat: add type for list & move failed from list
Showing
1 changed file
with
14 additions
and
8 deletions
pages/index/index.js
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | //获取应用实例 |
3 | 3 | const app = getApp(); |
4 | 4 | const selectedIndex = app.globalData.getSelectedTypeIndex(); |
5 | -const type = app.globalData.getTypeList(); | |
5 | +const typeList = app.globalData.getTypeList(); | |
6 | 6 | |
7 | 7 | let audio = null; |
8 | 8 | |
... | ... | @@ -10,7 +10,7 @@ Page({ |
10 | 10 | data: { |
11 | 11 | placeholder: '点击此处输入文字', |
12 | 12 | text: '', |
13 | - type, | |
13 | + typeList, | |
14 | 14 | selectedIndex: String(selectedIndex) ? selectedIndex : 5 |
15 | 15 | }, |
16 | 16 | onLoad(){ |
... | ... | @@ -21,7 +21,10 @@ Page({ |
21 | 21 | }, |
22 | 22 | async play(){ |
23 | 23 | const text = this.data.text || this.data.placeholder; |
24 | - const url = this.data.type[this.data.selectedIndex].createUrl(text); | |
24 | + const selectedType = this.data.typeList[this.data.selectedIndex]; | |
25 | + const url = selectedType.createUrl(text); | |
26 | + const type = selectedType.desc; | |
27 | + | |
25 | 28 | wx.showLoading({ |
26 | 29 | title: '正在合成语音...', |
27 | 30 | mask: true |
... | ... | @@ -57,14 +60,15 @@ Page({ |
57 | 60 | this.resetAudio(); |
58 | 61 | audio = wx.createInnerAudioContext({useWebAudioImplement: true}); |
59 | 62 | audio.autoplay = true; |
60 | - // 播放失败要移除缓存内容 | |
61 | - audio.onError((res) => { | |
62 | - console.log(res.errMsg) | |
63 | - console.log(res.errCode) | |
64 | - }); | |
65 | 63 | // 检查是否播放过 |
66 | 64 | const audioList = app.globalData.getAudioList(); |
67 | 65 | const played = audioList.find(v => v.url === url); |
66 | + // 播放失败要移除缓存内容 | |
67 | + audio.onError(err => { | |
68 | + if(err.errCode){ | |
69 | + app.globalData.setAudioList('delete', played.path); | |
70 | + } | |
71 | + }); | |
68 | 72 | // 播放过,使用本地文件减少请求,且从播放列表中移到最前 |
69 | 73 | if(played){ |
70 | 74 | app.globalData.setAudioList('delete', played.path); |
... | ... | @@ -79,6 +83,8 @@ Page({ |
79 | 83 | app.globalData.setAudioList('add', { |
80 | 84 | // 音频文本 |
81 | 85 | text, |
86 | + // 音频音效 | |
87 | + type, | |
82 | 88 | // 音频网络地址 |
83 | 89 | url, |
84 | 90 | // 音频本地地址 | ... | ... |