background-audio.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // const app = getApp()
  2. const util = require('../../../../util/util.js')
  3. const backgroundAudioManager = wx.getBackgroundAudioManager()
  4. let updateInterval
  5. Page({
  6. onShareAppMessage() {
  7. return {
  8. title: '背景音乐',
  9. path: 'packageAPI/pages/media/background-audio/background-audio'
  10. }
  11. },
  12. onShow() {
  13. if (!backgroundAudioManager.paused && backgroundAudioManager.paused !== undefined) {
  14. this._enableInterval()
  15. this.setData({
  16. playing: true
  17. })
  18. }
  19. },
  20. data: {
  21. theme: 'light',
  22. playing: false, // 播放状态
  23. pause: false,
  24. playTime: 0, // 播放时长
  25. formatedPlayTime: '00:00:00' // 格式化后的播放时长
  26. },
  27. play() {
  28. backgroundAudioManager.title = '此时此刻'
  29. backgroundAudioManager.epname = '此时此刻'
  30. backgroundAudioManager.singer = '许巍'
  31. backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000'
  32. const that = this
  33. if (that.data.pause) {
  34. backgroundAudioManager.play()
  35. this.setData({
  36. playing: true,
  37. })
  38. } else {
  39. that.setData({
  40. playing: true,
  41. }, () => {
  42. // 设置src后会自动播放
  43. backgroundAudioManager.src = 'https://dldir1.qq.com/music/release/upload/t_mm_file_publish/2339610.m4a'
  44. })
  45. }
  46. },
  47. seek(e) {
  48. backgroundAudioManager.seek(e.detail.value)
  49. },
  50. pause() {
  51. clearInterval(updateInterval)
  52. backgroundAudioManager.pause()
  53. },
  54. stop() {
  55. clearInterval(updateInterval)
  56. backgroundAudioManager.stop()
  57. },
  58. _enableInterval() {
  59. const that = this
  60. function update() {
  61. that.setData({
  62. playTime: backgroundAudioManager.currentTime + 1,
  63. formatedPlayTime: util.formatTime(backgroundAudioManager.currentTime + 1)
  64. })
  65. }
  66. updateInterval = setInterval(update, 1000)
  67. },
  68. onUnload() {
  69. clearInterval(updateInterval)
  70. },
  71. onLoad() {
  72. this.setData({
  73. theme: wx.getSystemInfoSync().theme || 'light'
  74. })
  75. if (wx.onThemeChange) {
  76. wx.onThemeChange(({theme}) => {
  77. this.setData({theme})
  78. })
  79. }
  80. const that = this
  81. // 监听播放事件
  82. backgroundAudioManager.onPlay(() => {
  83. // 刷新播放时间
  84. that._enableInterval()
  85. that.setData({
  86. pause: false,
  87. })
  88. })
  89. // 监听暂停事件
  90. backgroundAudioManager.onPause(() => {
  91. clearInterval(updateInterval)
  92. that.setData({
  93. playing: false,
  94. pause: true,
  95. })
  96. })
  97. backgroundAudioManager.onEnded(() => {
  98. clearInterval(updateInterval)
  99. that.setData({
  100. playing: false,
  101. playTime: 0,
  102. formatedPlayTime: util.formatTime(0)
  103. })
  104. })
  105. backgroundAudioManager.onStop(() => {
  106. clearInterval(updateInterval)
  107. that.setData({
  108. playing: false,
  109. playTime: 0,
  110. formatedPlayTime: util.formatTime(0)
  111. })
  112. })
  113. },
  114. })