Commit 3765f9b4871cce0918cc0ce4eb1bef2762543f37

Authored by xwenliang
1 parent 383d40df

chore: move local play to front of text check

Showing 1 changed file with 29 additions and 21 deletions
pages/index/index.js
... ... @@ -24,12 +24,37 @@ Page({
24 24 const selectedType = this.data.typeList[this.data.selectedIndex];
25 25 const url = selectedType.createUrl(text);
26 26 const type = selectedType.desc;
  27 +
  28 + this.resetAudio();
  29 + audio = wx.createInnerAudioContext({useWebAudioImplement: true});
  30 + audio.autoplay = true;
  31 + // 检查是否播放过
  32 + const audioList = app.globalData.getAudioList();
  33 + const played = audioList.find(v => v.url === url);
  34 + // 播放失败要移除缓存内容
  35 + audio.onError(err => {
  36 + app.globalData.setAudioList('delete', played.path);
  37 + wx.showModal({
  38 + title: '提示',
  39 + content: '该语音播放过但缓存失效,点击确定重新播放',
  40 + showCancel: false,
  41 + success: res => {
  42 + this.play();
  43 + }
  44 + });
  45 + });
  46 + // 播放过,使用本地文件减少请求,且从播放列表中移到最前
  47 + if(played){
  48 + app.globalData.setAudioList('delete', played.path);
  49 + app.globalData.setAudioList('add', played);
  50 + return audio.src = played.path;
  51 + }
27 52  
  53 + // 检查文本是否合规
28 54 wx.showLoading({
29 55 title: '正在合成语音...',
30 56 mask: true
31 57 });
32   - // 检查文本是否合规
33 58 const result = await new Promise((resolve, reject) => {
34 59 app.request({
35 60 url: `${app.globalData.domain}/open-api/wechat-msg-sec-check`,
... ... @@ -46,9 +71,9 @@ Page({
46 71 }
47 72 });
48 73 });
49   - wx.hideLoading();
50 74 // 不合规结束
51 75 if(result.suggest !== 'pass'){
  76 + wx.hideLoading();
52 77 return wx.showToast({
53 78 title: '内容不合规',
54 79 icon: 'error',
... ... @@ -56,29 +81,12 @@ Page({
56 81 duration: 2000
57 82 });
58 83 }
59   -
60   - this.resetAudio();
61   - audio = wx.createInnerAudioContext({useWebAudioImplement: true});
62   - audio.autoplay = true;
63   - // 检查是否播放过
64   - const audioList = app.globalData.getAudioList();
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   - });
72   - // 播放过,使用本地文件减少请求,且从播放列表中移到最前
73   - if(played){
74   - app.globalData.setAudioList('delete', played.path);
75   - app.globalData.setAudioList('add', played);
76   - return audio.src = played.path;
77   - }
  84 +
78 85 // 未播放过,先下载再播放
79 86 wx.downloadFile({
80 87 url,
81 88 success (res) {
  89 + wx.hideLoading();
82 90 audio.src = res.tempFilePath;
83 91 app.globalData.setAudioList('add', {
84 92 // 音频文本
... ...