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,12 +24,37 @@ Page({
24 const selectedType = this.data.typeList[this.data.selectedIndex]; 24 const selectedType = this.data.typeList[this.data.selectedIndex];
25 const url = selectedType.createUrl(text); 25 const url = selectedType.createUrl(text);
26 const type = selectedType.desc; 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 wx.showLoading({ 54 wx.showLoading({
29 title: '正在合成语音...', 55 title: '正在合成语音...',
30 mask: true 56 mask: true
31 }); 57 });
32 - // 检查文本是否合规  
33 const result = await new Promise((resolve, reject) => { 58 const result = await new Promise((resolve, reject) => {
34 app.request({ 59 app.request({
35 url: `${app.globalData.domain}/open-api/wechat-msg-sec-check`, 60 url: `${app.globalData.domain}/open-api/wechat-msg-sec-check`,
@@ -46,9 +71,9 @@ Page({ @@ -46,9 +71,9 @@ Page({
46 } 71 }
47 }); 72 });
48 }); 73 });
49 - wx.hideLoading();  
50 // 不合规结束 74 // 不合规结束
51 if(result.suggest !== 'pass'){ 75 if(result.suggest !== 'pass'){
  76 + wx.hideLoading();
52 return wx.showToast({ 77 return wx.showToast({
53 title: '内容不合规', 78 title: '内容不合规',
54 icon: 'error', 79 icon: 'error',
@@ -56,29 +81,12 @@ Page({ @@ -56,29 +81,12 @@ Page({
56 duration: 2000 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 wx.downloadFile({ 86 wx.downloadFile({
80 url, 87 url,
81 success (res) { 88 success (res) {
  89 + wx.hideLoading();
82 audio.src = res.tempFilePath; 90 audio.src = res.tempFilePath;
83 app.globalData.setAudioList('add', { 91 app.globalData.setAudioList('add', {
84 // 音频文本 92 // 音频文本