Commit 7498cb66b917351dfde03a5534152dbd72d021f5

Authored by xwenliang
1 parent 269af8d2

feat: 播放过的音频再次播放时挪到列表顶部

Showing 1 changed file with 11 additions and 4 deletions
pages/index/index.js
@@ -57,12 +57,19 @@ Page({ @@ -57,12 +57,19 @@ Page({
57 this.resetAudio(); 57 this.resetAudio();
58 audio = wx.createInnerAudioContext({useWebAudioImplement: true}); 58 audio = wx.createInnerAudioContext({useWebAudioImplement: true});
59 audio.autoplay = true; 59 audio.autoplay = true;
  60 + // 播放失败要移除缓存内容
  61 + audio.onError((res) => {
  62 + console.log(res.errMsg)
  63 + console.log(res.errCode)
  64 + });
60 // 检查是否播放过 65 // 检查是否播放过
61 const audioList = app.globalData.getAudioList(); 66 const audioList = app.globalData.getAudioList();
62 - const playedIndex = audioList.findIndex(v => v.url === url);  
63 - // 播放过,使用本地文件减少请求  
64 - if(playedIndex > -1){  
65 - return audio.src = audioList[playedIndex].path; 67 + const played = audioList.find(v => v.url === url);
  68 + // 播放过,使用本地文件减少请求,且从播放列表中移到最前
  69 + if(played){
  70 + app.globalData.setAudioList('delete', played.path);
  71 + app.globalData.setAudioList('add', played);
  72 + return audio.src = played.path;
66 } 73 }
67 // 未播放过,先下载再播放 74 // 未播放过,先下载再播放
68 wx.downloadFile({ 75 wx.downloadFile({