get-background-audio-manager.uvue 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <text>
  6. 注意:1.离开当前页面后背景音乐将保持播放;\n
  7. 2. 硬退出app、调用stop api、播放结束都会清理后台控制中心和锁屏信息显示
  8. </text>
  9. <view class="uni-common-mt">
  10. <slider ref="slider" :value="position" :min="0" :max="duration" @changing="onchanging"
  11. @change="onchange"></slider>
  12. </view>
  13. <view class="page-body-buttons">
  14. <template v-if="playing">
  15. <view class="page-body-button" @tap="stop">
  16. <image class="image" src="/static/stop.png"></image>
  17. </view>
  18. <view class="page-body-button" @tap="pause" style="margin-top: 20px;">
  19. <image class="image" src="/static/pause.png"></image>
  20. </view>
  21. </template>
  22. <template v-if="!playing">
  23. <view class="page-body-button" @tap="play">
  24. <image class="image" src="/static/play.png"></image>
  25. </view>
  26. </template>
  27. <view class="page-body-button"></view>
  28. </view>
  29. </view>
  30. </view>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. return {
  36. title: 'backgroundAudio',
  37. bgAudioMannager: null as BackgroundAudioManager | null,
  38. dataUrl: 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3',
  39. playing: false,
  40. playTime: 0,
  41. formatedPlayTime: '00:00:00',
  42. count: 100,
  43. isPlayEnd: false,
  44. duration: 100,
  45. currentTime: 0,
  46. _isChanging: false,
  47. buffered: 0,
  48. }
  49. },
  50. computed: {
  51. position() {
  52. return this.isPlayEnd ? 0 : this.currentTime;
  53. },
  54. },
  55. onLoad: function () {
  56. let bgAudioMannager = uni.getBackgroundAudioManager();
  57. bgAudioMannager.title = '致爱丽丝' + this.count;
  58. bgAudioMannager.epname = '专辑名:致爱丽丝' + this.count
  59. bgAudioMannager.singer = '歌手:暂无' + this.count;
  60. bgAudioMannager.coverImgUrl = 'https://web-assets.dcloud.net.cn/unidoc/zh/Alice.jpeg';
  61. bgAudioMannager.src = this.dataUrl;
  62. this.currentTime = bgAudioMannager.currentTime
  63. this.duration = bgAudioMannager.duration
  64. bgAudioMannager.onCanplay(() => {
  65. console.log("音频进入可以播放状态事件");
  66. this.buffered = bgAudioMannager.buffered;
  67. this.duration = bgAudioMannager.duration
  68. })
  69. bgAudioMannager.onPlay(() => {
  70. console.log("开始播放");
  71. this.playing = true;
  72. })
  73. bgAudioMannager.onPause(() => {
  74. console.log("暂停播放");
  75. this.playing = false;
  76. })
  77. bgAudioMannager.onStop(() => {
  78. console.log("停止播放");
  79. this.playing = false;
  80. })
  81. bgAudioMannager.onEnded(() => {
  82. console.log("播放结束");
  83. this.playing = false;
  84. this.currentTime = 0;
  85. this.isPlayEnd = true;
  86. (this.$refs["slider"] as UniSliderElement).value = 0
  87. })
  88. bgAudioMannager.onNext(() => {
  89. this.count++
  90. console.log("下一曲", this.count);
  91. this.bgAudioMannager?.stop()
  92. bgAudioMannager.title = '致爱丽丝' + this.count;
  93. bgAudioMannager.singer = '歌手:暂无' + this.count;
  94. this.dataUrl = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'
  95. bgAudioMannager.coverImgUrl = 'https://qiniu-web-assets.dcloud.net.cn/unidoc/zh/music-a.png';
  96. this.bgAudioMannager!.src = this.dataUrl;
  97. this.bgAudioMannager?.play()
  98. })
  99. bgAudioMannager.onPrev(() => {
  100. this.count--
  101. console.log("上一曲", this.count);
  102. this.bgAudioMannager?.stop()
  103. bgAudioMannager.title = '致爱丽丝' + this.count;
  104. bgAudioMannager.singer = '歌手:暂无' + this.count;
  105. this.dataUrl = 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3'
  106. bgAudioMannager.coverImgUrl = 'https://web-assets.dcloud.net.cn/unidoc/zh/Alice.jpeg';
  107. this.bgAudioMannager!.src = this.dataUrl;
  108. this.bgAudioMannager?.play()
  109. })
  110. bgAudioMannager.onSeeking(() => {
  111. console.log('音频进行 seek 操作事件');
  112. })
  113. bgAudioMannager.onSeeked(() => {
  114. console.log('音频完成 seek 操作事件');
  115. })
  116. bgAudioMannager.onWaiting(() => {
  117. console.log('音频加载中事件');
  118. })
  119. bgAudioMannager.onTimeUpdate(() => {
  120. console.log('onTimeUpdate', bgAudioMannager.currentTime)
  121. if (this._isChanging) { return; }
  122. this.currentTime = this.bgAudioMannager!.currentTime;
  123. this.buffered = this.bgAudioMannager!.buffered;
  124. console.log('onTimeUpdateCb', this.currentTime)
  125. // #ifdef MP
  126. // 微信小程序安卓端过早的时机获取的buffered、duration为0,改为在此处获取
  127. if (this.bgAudioMannager!.duration === 0) {
  128. this.buffered = this.bgAudioMannager!.buffered;
  129. this.duration = this.bgAudioMannager!.duration
  130. }
  131. // #endif
  132. if (this.currentTime > this.buffered) {
  133. console.log('缓冲不足');
  134. }
  135. })
  136. bgAudioMannager.onError((err) => {
  137. console.log('播放出错err', err);
  138. })
  139. this.bgAudioMannager = bgAudioMannager;
  140. this.playing = !bgAudioMannager.paused
  141. console.log('currentTime=', this.bgAudioMannager!.currentTime, this.bgAudioMannager!.currentTime == 0)
  142. },
  143. methods: {
  144. play: function () {
  145. console.log('play')
  146. this.isPlayEnd = false;
  147. this.bgAudioMannager!.play()
  148. },
  149. pause: function () {
  150. this.bgAudioMannager?.pause();
  151. },
  152. stop: function () {
  153. this.bgAudioMannager?.stop();
  154. this.playing = false
  155. },
  156. onchanging() {
  157. this._isChanging = true;
  158. },
  159. onchange(e : UniSliderChangeEvent) {
  160. let pos = e.detail.value;
  161. console.log('pos', pos);
  162. this.bgAudioMannager!.seek(pos);
  163. this._isChanging = false;
  164. },
  165. }
  166. }
  167. </script>
  168. <style>
  169. .image {
  170. width: 75px;
  171. height: 75px;
  172. }
  173. .page-body-text {
  174. padding: 0 15px;
  175. }
  176. .page-body-wrapper {
  177. margin-top: 0;
  178. }
  179. .page-body-info {
  180. padding-bottom: 25px;
  181. }
  182. .time-big {
  183. font-size: 30px;
  184. margin: 10px;
  185. }
  186. .slider {
  187. width: 315px;
  188. }
  189. .play-time {
  190. width: 100%;
  191. padding: 10px 0;
  192. display: flex;
  193. justify-content: space-between;
  194. box-sizing: border-box;
  195. }
  196. .page-body-buttons {
  197. display: flex;
  198. justify-content: center;
  199. margin-top: 50px;
  200. flex-direction: column;
  201. }
  202. .page-body-button {
  203. flex-direction: row;
  204. justify-content: center;
  205. }
  206. </style>