var app = getApp() const audioNameStorageKey = 'audioNameStorageKey' const audioSwitchStorageKey = 'audioSwitchStorageKey' class Audio { constructor() { this.pk = 'pk' this.friend = 'friend' this.room = 'room' this.personalRoom = 'personalRoom' this.playing = false } /** * 播放音频方法 * obj参数: * type比赛类型 * key缓存key,如不需本地保存可不传 * loop 是否循环 * srcs:[] 音频网络路劲 * storage 是否需要本地保存 */ play(obj) { if (!this.getSwitchStorage(obj.type) || !obj.srcs) { return } this.playing = true if (!obj.storage) { var src = this.getUrl(obj.srcs) this.playAudio(obj, src) return } else { if (!obj.key) { this.playing = false return } this.judgeKeyStorage() this.judgeStorage({ key: obj.key, urls: obj.srcs, success: url => { this.playAudio(obj, url) } }) } } //停止音频方法 stop() { this.playing = false if (this.innerAudioContext) { this.innerAudioContext.stop() this.innerAudioContext.destroy() this.innerAudioContext = null } } //t:比赛类型 //v:bool setSwitchStorage(t, v) { this.judgeKeyStorage() if (this.audioSwitchStorage) { this.audioSwitchStorage[t] = v this.saveAudioSwitchStorage() this.debugLog('audioSwitchStorage') this.debugLog(this.audioSwitchStorage) } } //t:比赛类型 getSwitchStorage(t) { this.judgeKeyStorage(true) return this.audioSwitchStorage[t] } isPlay() { if (this.innerAudioContext == null) { return false } return !this.innerAudioContext.paused() } playAudio(obj, url) { if (!this.playing) { return } if (this.innerAudioContext) { this.innerAudioContext.stop() this.innerAudioContext.destroy() this.innerAudioContext = null } this.innerAudioContext = wx.createInnerAudioContext() this.innerAudioContext.loop = obj.loop this.innerAudioContext.obeyMuteSwitch = false this.innerAudioContext.src = url this.innerAudioContext.play() } getUrl(urls) { return urls[Math.floor(Math.random() * urls.length)] } //syn:是否需要同步switch的数据 judgeKeyStorage(syn) { if (syn || !this.audioSwitchStorage) { try { this.audioSwitchStorage = wx.getStorageSync(audioSwitchStorageKey) if (!this.audioSwitchStorage) { this.initAudioSwitchStorage() } } catch (e) { this.initAudioSwitchStorage() } } if (!this.audioNameStorage) { try { this.audioNameStorage = wx.getStorageSync(audioNameStorageKey) if (!this.audioNameStorage) { this.initAudioNameStorage() return false } return true } catch (e) { this.initAudioNameStorage() return false } } } initAudioSwitchStorage() { this.audioSwitchStorage = { pk: true, friend: true, room: true, personalRoom: true } this.saveAudioSwitchStorage() this.debugLog('audioSwitchStorage') this.debugLog(this.audioSwitchStorage) } initAudioNameStorage() { this.audioNameStorage = { keys: {}, values: {} } this.saveAudioNameStorage() this.debugLog('audioNameStorage') this.debugLog(this.audioNameStorage) } saveAudioSwitchStorage() { try { wx.setStorageSync(audioSwitchStorageKey, this.audioSwitchStorage) } catch (e) { } } saveAudioNameStorage() { try { wx.setStorageSync(audioNameStorageKey, this.audioNameStorage) } catch (e) { } } judgeStorage(obj) { var urls = this.audioNameStorage.keys[obj.key] || null if (urls == null) { this.debugLog('新场景') this.judgeSong(obj) return } var judge = this.judgeArray(urls, obj.urls) this.debugLog('旧场景') this.debugLog(judge) if (judge.r.length != 0) { this.judgeSong(obj, judge) return } var url = this.getUrl(obj.urls) var values = this.audioNameStorage.values[url] || null if (values) { wx.getSavedFileInfo({ filePath: values[1], success: res => { this.debugLog('原歌曲') obj.success(values[1]) }, fail: e => { this.debugLog('原歌曲失效') this.downloadSong(obj, url, null, values[0]) } }) } else { this.debugLog('缓存问题-----原歌曲失效') this.downloadSong(obj, url, null, 1) } } judgeArray(oa, na) { var r = [] var o = [] for (var i in oa) { if (na.indexOf(oa[i]) == -1) { r.push(oa[i]) } else { o.push(oa[i]) } } this.debugLog('judge') this.debugLog(o) this.debugLog(r) return { r: r, o: o } } removeValues(urls) { for (var i in urls) { var url = urls[i] var values = this.audioNameStorage.values[url] || null if (values) { values[0]-- } } } //judge: 旧场景的时候需要传 judgeSong(obj, judge) { var url = this.getUrl(obj.urls) var values = this.audioNameStorage.values[url] || null if (values == null) { this.debugLog('新歌曲') this.downloadSong(obj, url, judge, 1) return } wx.getSavedFileInfo({ filePath: values[1], success: res => { this.debugLog('旧歌曲') if (judge) { this.removeValues(judge.r) } this.audioNameStorage.keys[obj.key] = obj.urls if (judge.o.indexOf(url) == -1) { values[0]++ } this.saveAudioNameStorage() this.debugLog('audioNameStorage') this.debugLog(this.audioNameStorage) obj.success(values[1]) }, fail: e => { this.debugLog('旧歌曲失效') var num = this.audioNameStorage.values[url][0] + 1 this.downloadSong(obj, url, judge, num) } }) } //judge: 旧场景更新歌曲的时候需要传 //num: 歌曲文件被删除重新下载需要传被引用次数 downloadSong(obj, url, judge, num) { var that = this wx.downloadFile({ url: url, success: function (res) { if (res.statusCode === 200) { wx.saveFile({ tempFilePath: res.tempFilePath, success: function (res) { var LAC = res.savedFilePath if (judge) { that.removeValues(judge.r) } that.audioNameStorage.keys[obj.key] = obj.urls that.audioNameStorage.values[url] = [num, LAC] that.saveAudioNameStorage() that.debugLog(that.audioNameStorage) obj.success(LAC) } }) } } }) } clearStorage() { var r = this.judgeKeyStorage() if (r) { this.debugLog('有缓存') var values = this.audioNameStorage.values var deletes = [] for (var key in values) { if (values[key][0] <= 0) { deletes.push(key) } } this.debugLog('deletes') this.debugLog(deletes) for (var i in deletes) { var key = deletes[i] this.debugLog('deletepath') this.debugLog(values[key][1]) wx.removeSavedFile({ filePath: values[key][1] }) delete values[key] } this.saveAudioNameStorage() this.debugLog('audioNameStorage') this.debugLog(this.audioNameStorage) } else { this.debugLog('没缓存') wx.getSavedFileList({ success: function (res) { if (res.fileList.length > 0) { for (var i in res.fileList) { wx.removeSavedFile({ filePath: res.fileList[i].filePath, }) } } } }) } } debugLog(info) { if (app) { app.log(info) } else { app = getApp() console.log(info) } } } export default Audio