audio.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. var app = getApp()
  2. const audioNameStorageKey = 'audioNameStorageKey'
  3. const audioSwitchStorageKey = 'audioSwitchStorageKey'
  4. class Audio {
  5. constructor() {
  6. this.pk = 'pk'
  7. this.friend = 'friend'
  8. this.room = 'room'
  9. this.personalRoom = 'personalRoom'
  10. this.playing = false
  11. }
  12. /**
  13. * 播放音频方法
  14. * obj参数:
  15. * type比赛类型
  16. * key缓存key,如不需本地保存可不传
  17. * loop 是否循环
  18. * srcs:[] 音频网络路劲
  19. * storage 是否需要本地保存
  20. */
  21. play(obj) {
  22. if (!this.getSwitchStorage(obj.type) || !obj.srcs) {
  23. return
  24. }
  25. this.playing = true
  26. if (!obj.storage) {
  27. var src = this.getUrl(obj.srcs)
  28. this.playAudio(obj, src)
  29. return
  30. } else {
  31. if (!obj.key) {
  32. this.playing = false
  33. return
  34. }
  35. this.judgeKeyStorage()
  36. this.judgeStorage({
  37. key: obj.key,
  38. urls: obj.srcs,
  39. success: url => {
  40. this.playAudio(obj, url)
  41. }
  42. })
  43. }
  44. }
  45. //停止音频方法
  46. stop() {
  47. this.playing = false
  48. if (this.innerAudioContext) {
  49. this.innerAudioContext.stop()
  50. this.innerAudioContext.destroy()
  51. this.innerAudioContext = null
  52. }
  53. }
  54. //t:比赛类型
  55. //v:bool
  56. setSwitchStorage(t, v) {
  57. this.judgeKeyStorage()
  58. if (this.audioSwitchStorage) {
  59. this.audioSwitchStorage[t] = v
  60. this.saveAudioSwitchStorage()
  61. this.debugLog('audioSwitchStorage')
  62. this.debugLog(this.audioSwitchStorage)
  63. }
  64. }
  65. //t:比赛类型
  66. getSwitchStorage(t) {
  67. this.judgeKeyStorage(true)
  68. return this.audioSwitchStorage[t]
  69. }
  70. isPlay() {
  71. if (this.innerAudioContext == null) {
  72. return false
  73. }
  74. return !this.innerAudioContext.paused()
  75. }
  76. playAudio(obj, url) {
  77. if (!this.playing) {
  78. return
  79. }
  80. if (this.innerAudioContext) {
  81. this.innerAudioContext.stop()
  82. this.innerAudioContext.destroy()
  83. this.innerAudioContext = null
  84. }
  85. this.innerAudioContext = wx.createInnerAudioContext()
  86. this.innerAudioContext.loop = obj.loop
  87. this.innerAudioContext.obeyMuteSwitch = false
  88. this.innerAudioContext.src = url
  89. this.innerAudioContext.play()
  90. }
  91. getUrl(urls) {
  92. return urls[Math.floor(Math.random() * urls.length)]
  93. }
  94. //syn:是否需要同步switch的数据
  95. judgeKeyStorage(syn) {
  96. if (syn || !this.audioSwitchStorage) {
  97. try {
  98. this.audioSwitchStorage = wx.getStorageSync(audioSwitchStorageKey)
  99. if (!this.audioSwitchStorage) {
  100. this.initAudioSwitchStorage()
  101. }
  102. } catch (e) {
  103. this.initAudioSwitchStorage()
  104. }
  105. }
  106. if (!this.audioNameStorage) {
  107. try {
  108. this.audioNameStorage = wx.getStorageSync(audioNameStorageKey)
  109. if (!this.audioNameStorage) {
  110. this.initAudioNameStorage()
  111. return false
  112. }
  113. return true
  114. } catch (e) {
  115. this.initAudioNameStorage()
  116. return false
  117. }
  118. }
  119. }
  120. initAudioSwitchStorage() {
  121. this.audioSwitchStorage = {
  122. pk: true,
  123. friend: true,
  124. room: true,
  125. personalRoom: true
  126. }
  127. this.saveAudioSwitchStorage()
  128. this.debugLog('audioSwitchStorage')
  129. this.debugLog(this.audioSwitchStorage)
  130. }
  131. initAudioNameStorage() {
  132. this.audioNameStorage = {
  133. keys: {},
  134. values: {}
  135. }
  136. this.saveAudioNameStorage()
  137. this.debugLog('audioNameStorage')
  138. this.debugLog(this.audioNameStorage)
  139. }
  140. saveAudioSwitchStorage() {
  141. try {
  142. wx.setStorageSync(audioSwitchStorageKey, this.audioSwitchStorage)
  143. } catch (e) {
  144. }
  145. }
  146. saveAudioNameStorage() {
  147. try {
  148. wx.setStorageSync(audioNameStorageKey, this.audioNameStorage)
  149. } catch (e) {
  150. }
  151. }
  152. judgeStorage(obj) {
  153. var urls = this.audioNameStorage.keys[obj.key] || null
  154. if (urls == null) {
  155. this.debugLog('新场景')
  156. this.judgeSong(obj)
  157. return
  158. }
  159. var judge = this.judgeArray(urls, obj.urls)
  160. this.debugLog('旧场景')
  161. this.debugLog(judge)
  162. if (judge.r.length != 0) {
  163. this.judgeSong(obj, judge)
  164. return
  165. }
  166. var url = this.getUrl(obj.urls)
  167. var values = this.audioNameStorage.values[url] || null
  168. if (values) {
  169. wx.getSavedFileInfo({
  170. filePath: values[1],
  171. success: res => {
  172. this.debugLog('原歌曲')
  173. obj.success(values[1])
  174. },
  175. fail: e => {
  176. this.debugLog('原歌曲失效')
  177. this.downloadSong(obj, url, null, values[0])
  178. }
  179. })
  180. } else {
  181. this.debugLog('缓存问题-----原歌曲失效')
  182. this.downloadSong(obj, url, null, 1)
  183. }
  184. }
  185. judgeArray(oa, na) {
  186. var r = []
  187. var o = []
  188. for (var i in oa) {
  189. if (na.indexOf(oa[i]) == -1) {
  190. r.push(oa[i])
  191. } else {
  192. o.push(oa[i])
  193. }
  194. }
  195. this.debugLog('judge')
  196. this.debugLog(o)
  197. this.debugLog(r)
  198. return {
  199. r: r,
  200. o: o
  201. }
  202. }
  203. removeValues(urls) {
  204. for (var i in urls) {
  205. var url = urls[i]
  206. var values = this.audioNameStorage.values[url] || null
  207. if (values) {
  208. values[0]--
  209. }
  210. }
  211. }
  212. //judge: 旧场景的时候需要传
  213. judgeSong(obj, judge) {
  214. var url = this.getUrl(obj.urls)
  215. var values = this.audioNameStorage.values[url] || null
  216. if (values == null) {
  217. this.debugLog('新歌曲')
  218. this.downloadSong(obj, url, judge, 1)
  219. return
  220. }
  221. wx.getSavedFileInfo({
  222. filePath: values[1],
  223. success: res => {
  224. this.debugLog('旧歌曲')
  225. if (judge) {
  226. this.removeValues(judge.r)
  227. }
  228. this.audioNameStorage.keys[obj.key] = obj.urls
  229. if (judge.o.indexOf(url) == -1) {
  230. values[0]++
  231. }
  232. this.saveAudioNameStorage()
  233. this.debugLog('audioNameStorage')
  234. this.debugLog(this.audioNameStorage)
  235. obj.success(values[1])
  236. },
  237. fail: e => {
  238. this.debugLog('旧歌曲失效')
  239. var num = this.audioNameStorage.values[url][0] + 1
  240. this.downloadSong(obj, url, judge, num)
  241. }
  242. })
  243. }
  244. //judge: 旧场景更新歌曲的时候需要传
  245. //num: 歌曲文件被删除重新下载需要传被引用次数
  246. downloadSong(obj, url, judge, num) {
  247. var that = this
  248. wx.downloadFile({
  249. url: url,
  250. success: function (res) {
  251. if (res.statusCode === 200) {
  252. wx.saveFile({
  253. tempFilePath: res.tempFilePath,
  254. success: function (res) {
  255. var LAC = res.savedFilePath
  256. if (judge) {
  257. that.removeValues(judge.r)
  258. }
  259. that.audioNameStorage.keys[obj.key] = obj.urls
  260. that.audioNameStorage.values[url] = [num, LAC]
  261. that.saveAudioNameStorage()
  262. that.debugLog(that.audioNameStorage)
  263. obj.success(LAC)
  264. }
  265. })
  266. }
  267. }
  268. })
  269. }
  270. clearStorage() {
  271. var r = this.judgeKeyStorage()
  272. if (r) {
  273. this.debugLog('有缓存')
  274. var values = this.audioNameStorage.values
  275. var deletes = []
  276. for (var key in values) {
  277. if (values[key][0] <= 0) {
  278. deletes.push(key)
  279. }
  280. }
  281. this.debugLog('deletes')
  282. this.debugLog(deletes)
  283. for (var i in deletes) {
  284. var key = deletes[i]
  285. this.debugLog('deletepath')
  286. this.debugLog(values[key][1])
  287. wx.removeSavedFile({
  288. filePath: values[key][1]
  289. })
  290. delete values[key]
  291. }
  292. this.saveAudioNameStorage()
  293. this.debugLog('audioNameStorage')
  294. this.debugLog(this.audioNameStorage)
  295. } else {
  296. this.debugLog('没缓存')
  297. wx.getSavedFileList({
  298. success: function (res) {
  299. if (res.fileList.length > 0) {
  300. for (var i in res.fileList) {
  301. wx.removeSavedFile({
  302. filePath: res.fileList[i].filePath,
  303. })
  304. }
  305. }
  306. }
  307. })
  308. }
  309. }
  310. debugLog(info) {
  311. if (app) {
  312. app.log(info)
  313. } else {
  314. app = getApp()
  315. console.log(info)
  316. }
  317. }
  318. }
  319. export default Audio