audio.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. var Audio = {
  2. //音乐列表
  3. list: [],
  4. //音频管理器
  5. manage: false,
  6. //当前播放第几首
  7. current: 0,
  8. //是否正在播放
  9. running: false,
  10. //是否绑定播放进度更新事件
  11. bindProcess: false,
  12. //绑定的主事件
  13. obj:false,
  14. //最大秒数
  15. max:0,
  16. loading:false,
  17. init: function (obj) {
  18. if (this.manage) {
  19. return
  20. }
  21. if (obj) {
  22. this.obj = obj
  23. }
  24. this.setRunning(false)
  25. var audio = wx.getStorageSync('audioaudo')
  26. if (!audio) {
  27. audio = wx.getBackgroundAudioManager()
  28. wx.setStorageSync('audio', audio)
  29. }
  30. this.manage = audio
  31. this.manage.stop()
  32. }
  33. , add: function (data) {
  34. this.list.push(data)
  35. }
  36. , play: function (index) {
  37. this.init()
  38. if (!index) {
  39. index = 0
  40. }
  41. if (this.loading == false) {
  42. this.current = index
  43. var data = this.list[index]
  44. this.setManageAttr(data)
  45. } else {
  46. this.manage.play()
  47. }
  48. this.setRunning(true)
  49. return this.running
  50. }
  51. , pause: function () {
  52. this.init()
  53. this.manage.pause()
  54. this.setRunning(false)
  55. }
  56. , stop: function () {
  57. this.init()
  58. this.manage.stop()
  59. this.setRunning(false)
  60. }
  61. , setRunning: function (value) {
  62. this.running = value
  63. wx.setStorageSync('audoRunning', this.running)
  64. if (this.obj) {
  65. this.obj.setData({
  66. audioRunning: this.running
  67. })
  68. }
  69. }
  70. , getManage: function () {
  71. this.init()
  72. return this.manage
  73. }
  74. //跳转播放到多少秒
  75. , seek: function (value) {
  76. this.init()
  77. this.manage.seek(this.duration() * value)
  78. }
  79. //根据百分比值,计算总秒数
  80. , duration: function () {
  81. this.init()
  82. return this.manage.duration / 100
  83. }
  84. //根据当前秒数,计算当前进度
  85. , currentTime: function () {
  86. this.init()
  87. if (this.manage.currentTime >= this.manage.duration) {
  88. return 100
  89. }
  90. return (this.manage.currentTime / this.manage.duration) * 100
  91. }
  92. //播放进度更新
  93. , process: function () {
  94. this.init()
  95. if (!this.obj) {
  96. return
  97. }
  98. var that = this
  99. this.manage.onTimeUpdate(function () {
  100. var cur = that.currentTime()
  101. if (cur >= 100) {
  102. cur = 0
  103. that.running = false
  104. }
  105. if (that.max <= 0) {
  106. that.max = that.manage.duration
  107. }
  108. that.obj.setData({
  109. audioProcess: cur,
  110. audioCurTime: that.time(that.manage.currentTime),
  111. audioTotalTime: that.time(that.max)
  112. })
  113. })
  114. this.bindProcess = true
  115. }
  116. , setManageAttr : function (data) {
  117. this.loading = true
  118. this.manage.title = data.title
  119. this.manage.epname = data.epname
  120. this.manage.singer = data.singer
  121. this.manage.coverImgUrl = data.coverImgUrl
  122. this.manage.src = data.src
  123. this.setOn()
  124. }
  125. , time : function(value) {
  126. var secondTime = parseInt(value);// 秒
  127. var minuteTime = 0;// 分
  128. var hourTime = 0;// 小时
  129. if (secondTime > 60) {//如果秒数大于60,将秒数转换成整数
  130. //获取分钟,除以60取整数,得到整数分钟
  131. minuteTime = parseInt(secondTime / 60);
  132. //获取秒数,秒数取佘,得到整数秒数
  133. secondTime = parseInt(secondTime % 60);
  134. }
  135. if (secondTime < 10) {
  136. secondTime = '0' + secondTime
  137. }
  138. var result = "" + secondTime + "";
  139. if (minuteTime > 0) {
  140. if (minuteTime < 10) {
  141. minuteTime = '0' + minuteTime
  142. }
  143. result = "" + minuteTime + ":" + result;
  144. } else {
  145. result = "00:" + result;
  146. }
  147. return result
  148. }
  149. , setOn : function () {
  150. var that = this
  151. this.manage.onPause(function () {
  152. that.setRunning(false)
  153. })
  154. this.manage.onStop(function () {
  155. that.loading = false
  156. that.setRunning(false)
  157. })
  158. this.manage.onPlay(function () {
  159. that.loading = true
  160. that.setRunning(true)
  161. })
  162. this.manage.onEnded(function () {
  163. that.loading = false
  164. that.setRunning(false)
  165. })
  166. }
  167. }
  168. module.exports = {
  169. load: Audio
  170. }