123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347 |
- 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
|