background-audio.uvue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-hello-text">注意:请确保通知权限开启,离开当前页面后背景音乐将保持播放,但退出uni-app将停止</view>
  6. <view class="page-body-buttons">
  7. <block v-if="playing">
  8. <view class="page-body-button" @tap="stop">
  9. <image class="image" src="/static/stop.png"></image>
  10. </view>
  11. <view class="page-body-button" @tap="pause">
  12. <image class="image" src="/static/pause.png"></image>
  13. </view>
  14. </block>
  15. <block v-if="!playing">
  16. <view class="page-body-button" @tap="play">
  17. <image class="image" src="/static/play.png"></image>
  18. </view>
  19. </block>
  20. <view class="page-body-button"></view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. data() {
  28. return {
  29. title: 'backgroundAudio',
  30. bgAudioMannager: null as BackgroundAudioManager | null,
  31. dataUrl: 'https://web-ext-storage.dcloud.net.cn/uni-app/ForElise.mp3',
  32. playing: false,
  33. playTime: 0,
  34. formatedPlayTime: '00:00:00',
  35. count: 100,
  36. }
  37. },
  38. onLoad: function () {
  39. let bgAudioMannager = uni.getBackgroundAudioManager();
  40. bgAudioMannager.title = '致爱丽丝';
  41. bgAudioMannager.singer = '暂无';
  42. bgAudioMannager.coverImgUrl = 'https://web-assets.dcloud.net.cn/unidoc/zh/Alice.jpeg';
  43. bgAudioMannager.onPlay(() => {
  44. console.log("开始播放");
  45. this.playing = true;
  46. })
  47. bgAudioMannager.onPause(() => {
  48. console.log("暂停播放");
  49. this.playing = false;
  50. })
  51. bgAudioMannager.onEnded(() => {
  52. this.playing = false;
  53. // this.playTime = this.playTime = 0;
  54. // thi.formatedPlayTime = this.formatedPlayTime
  55. })
  56. bgAudioMannager.onNext(() => {
  57. console.log("下一曲");
  58. this.bgAudioMannager?.stop()
  59. bgAudioMannager.title = '致爱丽丝' + this.count++;
  60. bgAudioMannager.singer = '暂无2' + this.count++;
  61. bgAudioMannager.coverImgUrl = 'https://web-assets.dcloud.net.cn/unidoc/zh/Alice.jpeg';
  62. this.bgAudioMannager!.src = this.dataUrl;
  63. this.bgAudioMannager?.play()
  64. })
  65. bgAudioMannager.onPrev(() => {
  66. console.log("上一曲");
  67. this.bgAudioMannager?.stop()
  68. bgAudioMannager.title = '致爱丽丝' + this.count--;
  69. bgAudioMannager.singer = '暂无' + this.count--;
  70. this.bgAudioMannager!.src = this.dataUrl;
  71. this.bgAudioMannager?.play()
  72. })
  73. // bgAudioMannager.onTimeUpdate((e) => {
  74. // if (Math.floor(bgAudioMannager.currentTime) > Math.floor(this.playTime)) {
  75. // this.$backgroundAudioData.formatedPlayTime = this.formatedPlayTime = util.formatTime(Math.floor(bgAudioMannager.currentTime));
  76. // }
  77. // this.$backgroundAudioData.playTime = this.playTime = bgAudioMannager.currentTime;
  78. // })
  79. this.bgAudioMannager = bgAudioMannager;
  80. },
  81. methods: {
  82. play: function () {
  83. console.log('play')
  84. this.bgAudioMannager!.src = this.dataUrl;
  85. this.bgAudioMannager!.play()
  86. },
  87. pause: function () {
  88. this.bgAudioMannager?.pause();
  89. },
  90. stop: function () {
  91. this.bgAudioMannager?.stop();
  92. this.playing = false
  93. }
  94. }
  95. }
  96. </script>
  97. <style>
  98. .image {
  99. width: 150rpx;
  100. height: 150rpx;
  101. }
  102. .page-body-text {
  103. padding: 0 30rpx;
  104. }
  105. .page-body-wrapper {
  106. margin-top: 0;
  107. }
  108. .page-body-info {
  109. padding-bottom: 50rpx;
  110. }
  111. .time-big {
  112. font-size: 60rpx;
  113. margin: 20rpx;
  114. }
  115. .slider {
  116. width: 630rpx;
  117. }
  118. .play-time {
  119. width: 100%;
  120. padding: 20rpx 0;
  121. display: flex;
  122. justify-content: space-between;
  123. box-sizing: border-box;
  124. }
  125. .page-body-buttons {
  126. display: flex;
  127. justify-content: center;
  128. margin-top: 100rpx;
  129. }
  130. .page-body-button {
  131. flex-direction: row;
  132. justify-content: center;
  133. }
  134. </style>