background-audio.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <template>
  2. <view>
  3. <page-head :title="title"></page-head>
  4. <view class="uni-padding-wrap">
  5. <view class="uni-center">
  6. <text class="time-big">{{formatedPlayTime}}</text>
  7. </view>
  8. <view class="uni-common-mt">
  9. <slider class="slider" min="0" max="21" step="1" :value="playTime" @change="seek"></slider>
  10. </view>
  11. <view class="play-time">
  12. <text>00:00</text>
  13. <text>00:21</text>
  14. </view>
  15. <view class="uni-hello-text">注意:离开当前页面后背景音乐将保持播放,但退出uni-app将停止</view>
  16. <view class="page-body-buttons">
  17. <block v-if="playing">
  18. <view class="page-body-button" @tap="stop">
  19. <image src="/static/stop.png"></image>
  20. </view>
  21. <view class="page-body-button" @tap="pause">
  22. <image src="/static/pause.png"></image>
  23. </view>
  24. </block>
  25. <block v-if="!playing">
  26. <view class="page-body-button"></view>
  27. <view class="page-body-button" @tap="play">
  28. <image src="/static/play.png"></image>
  29. </view>
  30. </block>
  31. <view class="page-body-button"></view>
  32. </view>
  33. </view>
  34. </view>
  35. </template>
  36. <script>
  37. import * as util from '../../../common/util.js';
  38. export default {
  39. data() {
  40. return {
  41. title: 'backgroundAudio',
  42. bgAudioMannager: null,
  43. dataUrl: 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-hello-uniapp/2cc220e0-c27a-11ea-9dfb-6da8e309e0d8.mp3',
  44. playing: false,
  45. playTime: 0,
  46. formatedPlayTime: '00:00:00'
  47. }
  48. },
  49. onLoad: function () {
  50. this.playing = this.$backgroundAudioData.playing;
  51. this.playTime = this.$backgroundAudioData.playTime;
  52. this.formatedPlayTime = this.$backgroundAudioData.formatedPlayTime;
  53. let bgAudioMannager = uni.getBackgroundAudioManager();
  54. if(!bgAudioMannager.title){
  55. bgAudioMannager.title = '致爱丽丝';
  56. }
  57. if(!bgAudioMannager.singer) {
  58. bgAudioMannager.singer = '暂无';
  59. }
  60. if(!bgAudioMannager.coverImgUrl){
  61. bgAudioMannager.coverImgUrl = 'https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/c517b410-5184-11eb-b997-9918a5dda011.jpeg';
  62. }
  63. bgAudioMannager.onPlay(() => {
  64. console.log("开始播放");
  65. this.$backgroundAudioData.playing = this.playing = true;
  66. })
  67. bgAudioMannager.onPause(() => {
  68. console.log("暂停播放");
  69. this.$backgroundAudioData.playing = this.playing = false;
  70. })
  71. bgAudioMannager.onEnded(() => {
  72. this.playing = false;
  73. this.$backgroundAudioData.playing = false;
  74. this.$backgroundAudioData.playTime = this.playTime = 0;
  75. this.$backgroundAudioData.formatedPlayTime = this.formatedPlayTime = util.formatTime(0);
  76. })
  77. bgAudioMannager.onTimeUpdate((e) => {
  78. if (Math.floor(bgAudioMannager.currentTime) > Math.floor(this.playTime)) {
  79. this.$backgroundAudioData.formatedPlayTime = this.formatedPlayTime = util.formatTime(Math.floor(bgAudioMannager.currentTime));
  80. }
  81. this.$backgroundAudioData.playTime = this.playTime = bgAudioMannager.currentTime;
  82. })
  83. this.bgAudioMannager = bgAudioMannager;
  84. },
  85. methods: {
  86. play: function (res) {
  87. if (!this.bgAudioMannager.src) {
  88. this.bgAudioMannager.startTime = this.playTime;
  89. this.bgAudioMannager.src = this.dataUrl;
  90. } else {
  91. this.bgAudioMannager.seek(this.playTime);
  92. this.bgAudioMannager.play();
  93. }
  94. },
  95. seek: function (e) {
  96. this.bgAudioMannager.seek(e.detail.value);
  97. },
  98. pause: function () {
  99. this.bgAudioMannager.pause();
  100. },
  101. stop: function () {
  102. this.bgAudioMannager.stop();
  103. this.$backgroundAudioData.playing = this.playing = false;
  104. this.$backgroundAudioData.playTime = this.playTime = 0;
  105. this.$backgroundAudioData.formatedPlayTime = this.formatedPlayTime = util.formatTime(0);
  106. }
  107. }
  108. }
  109. </script>
  110. <style>
  111. image {
  112. width: 150rpx;
  113. height: 150rpx;
  114. }
  115. .page-body-text {
  116. padding: 0 30rpx;
  117. }
  118. .page-body-wrapper {
  119. margin-top: 0;
  120. }
  121. .page-body-info {
  122. padding-bottom: 50rpx;
  123. }
  124. .time-big {
  125. font-size: 60rpx;
  126. margin: 20rpx;
  127. }
  128. .slider {
  129. width:630rpx;
  130. }
  131. .play-time {
  132. font-size: 28rpx;
  133. width:100%;
  134. padding: 20rpx 0;
  135. display: flex;
  136. justify-content: space-between;
  137. box-sizing: border-box;
  138. }
  139. .page-body-buttons {
  140. display: flex;
  141. justify-content: space-around;
  142. margin-top: 100rpx;
  143. }
  144. .page-body-button {
  145. width: 250rpx;
  146. text-align: center;
  147. }
  148. </style>