gui-audio-player.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="gui-player">
  3. <view class="gui-player-title"
  4. :style="{color:color}">{{list[index].title}}</view>
  5. <view class="gui-player-poster">
  6. <image class="gui-player-poster-img"
  7. :class="[playStatus == 1 ? 'gui-playing' : '']"
  8. :src="list[index].coverImgUrl" mode="aspectFill"></image>
  9. </view>
  10. <!-- 播放进度 -->
  11. <view style="padding:25rpx;">
  12. <gui-single-slider ref="graceSingleSlider"
  13. @change="progressChange" :barWidth="100"
  14. :barText="playTime" barColor="#36395A"
  15. barBgColor="linear-gradient(to right, #FFFFFF,#FFFFFF)"
  16. bglineColor="rgba(255,255,255,0.5)"
  17. bglineAColor="#FFFFFF"></gui-single-slider>
  18. </view>
  19. <!-- 播放控制 -->
  20. <view class="gui-player-console">
  21. <!-- 循环模式 -->
  22. <view class="gui-player-tool"
  23. @tap="changeType">
  24. <text class="gui-player-tool gui-icons"
  25. v-if="dotype == 1">&#xe677;</text>
  26. <text class="gui-player-tool gui-icons"
  27. v-if="dotype == 2" style="font-size:40rpx;">&#xe64a;</text>
  28. </view>
  29. <view class="gui-player-console-c">
  30. <text class="gui-player-tool gui-icons" @tap="prev">&#xe659;</text>
  31. <text class="gui-player-tool gui-icons" style="font-size:66rpx;"
  32. @tap="pause" v-if="playStatus == 1">&#xe64b;</text>
  33. <text class="gui-player-tool gui-icons" style="font-size:66rpx;"
  34. @tap="playi" v-if="playStatus == 2">&#xe649;</text>
  35. <text class="gui-player-tool gui-icons" @tap="next">&#xe65a;</text>
  36. </view>
  37. <text class="gui-player-tool gui-icons" @tap="openList">&#xe648;</text>
  38. </view>
  39. <!-- 歌曲列表 -->
  40. <view class="gui-shade" :style="{left:listShow?'0':'-2000px'}"
  41. @tap.stop.parent="hideList" @touchmove.stop.parent="">
  42. <scroll-view scroll-y="true" class="graceplayer-list"
  43. :style="{background:listBg, height:listHeight}">
  44. <view class="graceplayer-list-item">歌曲列表</view>
  45. <view class="graceplayer-list-item"
  46. :class="[index == idx ? 'graceplayer-list-item-this' : '']"
  47. v-for="(item, idx) in list" :key="idx"
  48. @tap="playList" :data-index="idx">{{item.title}}<text class="graceplayer-list-item-singer"> - {{item.singer}}</text></view>
  49. </scroll-view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. export default{
  55. name : "gui-audio-player",
  56. props : {
  57. color : {type:String, default:'#FFFFFF'},
  58. list : {type:Array , default:function(){return [];}},
  59. listBg : {type:String, default:'#292E35'},
  60. listHeight : {type:String, default:'880rpx'},
  61. autoplay : {type:Boolean, default:true}
  62. },
  63. data(){
  64. return {
  65. playStatus : 1,
  66. player : null,
  67. playTime : '00:00',
  68. timer : null,
  69. dotype : 1,
  70. index : 0,
  71. listShow : false,
  72. audioLength : 1
  73. }
  74. },
  75. mounted:function(){
  76. if(this.autoplay){
  77. this.play();
  78. }
  79. },
  80. created:function(){
  81. // #ifdef H5
  82. return ;
  83. // #endif
  84. this.player = uni.getBackgroundAudioManager();
  85. this.player.onTimeUpdate(()=>{
  86. try{
  87. if(this.playStatus != 1){return ;}
  88. this.audioLength = this.player.duration;
  89. // 调整进度
  90. var progress = this.player.currentTime / this.audioLength;
  91. progress = Math.round(progress * 100);
  92. var ref = this.$refs.graceSingleSlider;
  93. if(ref){
  94. ref.setProgress(progress);
  95. }
  96. this.playTime = this.timeFormat(this.player.currentTime);
  97. }catch(e){};
  98. });
  99. this.player.onPlay(()=>{
  100. this.playStatus = 1;
  101. this.audioLength = this.player.duration;
  102. });
  103. this.player.onPause(()=>{
  104. this.playStatus = 2;
  105. });
  106. this.player.onEnded(()=>{
  107. switch(this.dotype){
  108. case 1 :
  109. this.index++;
  110. if(this.index + 1 > this.list.length){this.index = 0;}
  111. this.play();
  112. break;
  113. case 2 :
  114. this.player.seek(0);
  115. this.play();
  116. break;
  117. }
  118. });
  119. if(this.player.currentTime < 1){
  120. this.play();
  121. }
  122. },
  123. methods:{
  124. play:function () {
  125. var audio = this.list[this.index];
  126. this.player.title = audio.title;
  127. this.player.singer = audio.singer;
  128. this.player.coverImgUrl = audio.coverImgUrl;
  129. this.player.src = audio.src;
  130. this.player.play();
  131. },
  132. progressChange : function(e){
  133. if(this.timer != null){ clearTimeout(this.timer); }
  134. this.player.pause();
  135. var needTime = this.audioLength * e / 100;
  136. needTime = Math.round(needTime);
  137. this.playTime = this.timeFormat(needTime);
  138. this.timer = setTimeout(()=>{
  139. this.player.seek(needTime);
  140. this.player.play();
  141. }, 800);
  142. },
  143. timeFormat : function (s){
  144. s = Math.round(s);
  145. if(s < 60){
  146. if(s < 10){return '00:0'+s;}
  147. return '00:'+s;
  148. }else{
  149. var second = s % 60;
  150. s = s - second;
  151. var minute = s / 60;
  152. if(minute < 10){minute = '0'+minute;}
  153. if(second < 10){second = '0'+second;}
  154. return minute+':'+second;
  155. }
  156. },
  157. changeType : function () {
  158. switch(this.dotype){
  159. case 1 :
  160. this.dotype = 2;
  161. break;
  162. case 2 :
  163. this.dotype = 1;
  164. break;
  165. }
  166. },
  167. pause :function () {this.player.pause();},
  168. playi :function () {this.player.play();},
  169. next : function () {
  170. if(this.index + 1 >= this.list.length){uni.showToast({title:"已经到底了 (:", icon:"none"}); return ;}
  171. this.index++;
  172. this.play();
  173. },
  174. prev : function () {
  175. if(this.index -1 < 0){uni.showToast({title:"已经到头了 (:", icon:"none"}); return ;}
  176. this.index--;
  177. this.play();
  178. },
  179. openList : function () { this.listShow = true; },
  180. hideList : function () { this.listShow = false; },
  181. playList : function (e) {
  182. var idx = e.currentTarget.dataset.index;
  183. this.index = idx;
  184. this.play();
  185. },
  186. setIndex : function (idx) { this.index = idx; }
  187. }
  188. }
  189. </script>
  190. <style scoped>
  191. .gui-player{padding:30rpx;}
  192. .gui-player-title{text-align:center; line-height:50rpx; font-size:30rpx; font-weight:bold;}
  193. .gui-player-poster{padding:30px; line-height:0; text-align:center;}
  194. .gui-player-poster-img{width:380rpx; height:380rpx; border-radius:300rpx; box-shadow:0rpx 2rpx 2rpx #323232;}
  195. @keyframes gui-rotate360{0%{transform:rotate(0deg);} 50%{transform:rotate(180deg);} 100%{transform:rotate(360deg);}}
  196. .gui-playing{animation:gui-rotate360 6000ms infinite linear;}
  197. .gui-player-console{padding:20rpx 10rpx; display:flex; flex-direction:row; align-items:center; justify-content:space-between;}
  198. .gui-player-tool{width:100rpx; line-height:100rpx; text-align:center; font-size:50rpx; display:block; flex-shrink:0; color:#FFFFFF;}
  199. .gui-player-console-c{width:400rpx; display:flex; flex-direction:row; justify-content:space-between;}
  200. .graceplayer-list{width:100%; height:1000rpx; background-color:#FFFFFF; position:absolute; left:0; bottom:0; z-index:9999;}
  201. .gui-shade{position:fixed; width:100%; height:100%; left:0; top:0; background-color:rgba(0,0,0,0.5); bottom:0; overflow:hidden; z-index:9998; display:flex; justify-content:center; align-items:center;}
  202. .graceplayer-list-item{padding:25rpx 0rpx; margin:5rpx 30rpx; border-bottom:1px solid #373A3F; line-height:50rpx; font-size:30rpx; color:#FFFFFF;}
  203. .graceplayer-list-item-singer{color:#888888; font-size:26rpx; margin-left:50rpx;}
  204. .graceplayer-list-item-this{color:#64CDA5 !important; font-weight:bold;}
  205. </style>