137-3385-8848(同微信)

微信小程序音乐播放器,音乐播放器小程序制作步骤(音乐播放页)

2022-04-21 管理员

  

  这个页面分为3个部分:顶部信息栏(歌曲名/专辑名),中间的唱片图,以及底下的控制按钮。

  首先是信息栏:

  1. <view class="song-info">
  2.       <text class="song-title">{{playingMusic.name}}</text>
  3.       <text class="song-subtitle">
  4.         <block wx:for="{{playingMusic.singer}}" wx:key="unique">
  5.           <block wx-if="{{index!=0}}">*</block>{{item.name}}</block>
  6.       </text>
  7.     </view>
复制代码

 

  这部分很简单,与我们前面写过的类似,多个歌手之间用“*”分隔。格式文件为:

  1. .song-info {
  2.     width:100%;
  3.     padding:30rpx;
  4.     box-sizing:border-box;
  5.     text-align:center;
  6. }
  7. .song-title {
  8.     display:block;
  9.     width:100%;
  10.     color:#fff;
  11.     font-size:36rpx;
  12.     line-height:60rpx;
  13.     overflow:hidden;
  14.     white-space:nowrap;
  15.     text-overflow:ellipsis;
  16. }
  17. .song-subtitle {
  18.     display:block;
  19.     width:100%;
  20.     font-size:28rpx;
  21.     line-height:40rpx;
  22.     color:rgba(255,255,255,.6);
  23.     overflow:hidden;
  24.     white-space:nowrap;
  25.     text-overflow:ellipsis;
  26. }
复制代码

 

  然后是中间的图片,这部分我们以动画形式表现,让唱片一直旋转,先看布局文件:

  1. <view class="cd-info">
  2.       <view class="cd-gan"></view>
  3.       <view class="cd-inner cd-animation">
  4.         <image class="cd-img" src="{{playingMusic.img}}"></image>
  5.       </view>
  6.     </view>
  7. “cd-gan”是唱片部分里白色的播放杆部分,“cd-inner”是唱片部分,在这里为它添加表示唱片的黑色背景,然后在里门用小一圈的“cd-img”来加载我们获取的网络图片。最后为整个唱片添加动画“cd-animation”。这些工作全部由格式文件完成。
  8. .cd-info {
  9.   position: relative;
  10.   width: 100%;
  11.   text-align: center;
  12.   padding-top: 120rpx;
  13. }
  14.  
  15. .cd-gan {
  16.   position: absolute;
  17.   left: 50%;
  18.   margin-top: -80rpx;
  19.   margin-left: -150rpx;
  20.   display: block;
  21.   width: 300rpx;
  22.   height: 200rpx;
  23.   background: url('../../resources/images/cd_g.png');
  24.   background-size: cover;
  25.   z-index: 10;
  26. }
  27.  
  28. .cd-inner {
  29.   position: relative;
  30.   display: inline-block;
  31.   z-index: 4;
  32.   height: 500rpx;
  33.   width: 500rpx;
  34.   background: url('../../resources/images/cd_b.png');
  35.   background-size: cover;
  36.   text-align: center;
  37.   padding-top: 100rpx;
  38.   box-sizing: border-box;
  39. }
  40.  
  41. .cd-animation {
  42.   -webkit-animation: circle 10s infinite linear;
  43.   animation: circle 10s infinite linear;
  44. }
  45.  
  46. .cd-img {
  47.   display: inline-block;
  48.   width: 300rpx;
  49.   height: 300rpx;
  50.   border-radius: 50%;
  51. }
  52.  
  53. @keyframes circle {
  54.   0% {
  55.     transform: rotate(0deg);
  56.   }
  57.  
  58.   100% {
  59.     transform: rotate(360deg);
  60.   }
  61. }
复制代码

 

  这里面大多数的代码相信大家已经很熟悉了,重点看一下“cd-animation”这一部分,这里加载了动画“circle”并设置了动画时长与无限循环,这样就实现了在音乐播放时,唱片一直旋转的效果。“circle”动画我们使用关键帧keyframes来实现。

  

  完成这两部分后我们以一个view来包裹它们,确定它在页面的位置。

  1. <view class="main-box">
  2.     <view class="song-info">
  3.       <text class="song-title">{{playingMusic.name}}</text>
  4.       <text class="song-subtitle">
  5.         <block wx:for="{{playingMusic.singer}}" wx:key="unique">
  6.           <block wx-if="{{index!=0}}">*</block>{{item.name}}</block>
  7.       </text>
  8.     </view>
  9.     <view class="cd-info">
  10.       <view class="cd-gan"></view>
  11.       <view class="cd-inner cd-animation">
  12.         <image class="cd-img" src="{{playingMusic.img}}"></image>
  13.       </view>
  14.     </view>
  15.   </view>
  16. .main-box {
  17.   position: absolute;
  18.   top: 0;
  19.   bottom: 308rpx;
  20.   z-index: 3;
  21.   width: 100%;
  22.   background: rgba(0, 0, 0, 0.2);
  23. }
复制代码

 

  接着我们完成底下的操作部分。

  1. <view class="ctre-box">
  2.     <view class="slider-box">
  3.       <text class="slider-text st-l">{{currTimeStr}}</text>
  4.       <slider class="slider-inner"></slider>
  5.       <text class="slider-text st-r">{{musicTimeStr}}</text>
  6.     </view>
  7.     <view class="music-ctr">
  8.       <block wx-if="{{playType==0}}">
  9.         <view class="music-sort ms-loop" data-type="{{playType}}" bindtap="changePlayType"></view>
  10.       </block>
  11.       <block wx-if="{{playType==1}}">
  12.         <view class="music-sort ms-shuffle" data-type="{{playType}}" bindtap="changePlayType"></view>
  13.       </block>
  14.       <block wx-if="{{playType==2}}">
  15.         <view class="music-sort ms-one" data-type="{{playType}}" bindtap="changePlayType"></view>
  16.       </block>
  17.       <view class="mc-inner">
  18.         <view class="mci-icon mci-prev"></view>
  19.         <view class="mci-icon mci-play"></view>
  20.         <view class="mci-icon mci-next"></view>
  21.       </view>
  22.       <view class="music-list-btn" bindtap="showPlayList"></view>
  23.     </view>
  24.   </view>
复制代码

 

  操作控制部分由最上边的进度条部分“slider-box”和底下的操作按钮“mucis-ctr”组成。进度条我们使用slider组件,两段用两个text组件来显示当前播放时间与总时长。操作按钮部分,首先是播放模式的按钮,根据playType的值,改变顺序/随机/单曲的播放模式并对应加载不同的图片。然后是3个按钮,分别表示前一首/播放/下一首。最后是显示播放列表的按钮。

  格式文件为:

  1. .slider-box {
  2.   box-sizing: border-box;
  3.   padding: 20rpx 130rpx;
  4. }
  5.  
  6. .slider-text {
  7.   position: absolute;
  8.   display: block;
  9.   width: 100rpx;
  10.   height: 40rpx;
  11.   line-height: 40rpx;
  12.   font-size: 24rpx;
  13.   color: #fff;
  14. }
  15.  
  16. .st-l {
  17.   left: 60rpx;
  18. }
  19.  
  20. .st-r {
  21.   top: 20rpx;
  22.   right: 40rpx;
  23.   text-align: right;
  24. }
  25.  
  26. .slider-inner {
  27.   width: 100%;
  28. }
  29. .ctre-box {
  30.   height: 308rpx;
  31.   position: absolute;
  32.   bottom: 0;
  33.   z-index: 3;
  34.   width: 100%;
  35.   background: rgba(0, 0, 0, 0.2);
  36. }
  37.  
  38. .music-ctr {
  39.   position: relative;
  40.   padding: 20rpx 120rpx;
  41. }
  42.  
  43. .music-sort {
  44.   position: absolute;
  45.   left: 20rpx;
  46.   width: 108rpx;
  47.   height: 108rpx;
  48. }
  49.  
  50. .ms-loop {
  51.   background: url("../../resources/images/play_icn_loop.png");
  52.   background-size: cover;
  53. }
  54.  
  55. .ms-one {
  56.   background: url("../../resources/images/play_icn_one.png");
  57.   background-size: cover;
  58. }
  59.  
  60. .ms-shuffle {
  61.   background: url("../../resources/images/play_icn_shuffle.png");
  62.   background-size: cover;
  63. }
  64.  
  65. .music-list-btn {
  66.   position: absolute;
  67.   top: 36rpx;
  68.   right: 20rpx;
  69.   width: 108rpx;
  70.   height: 108rpx;
  71.   background: url("../../resources/images/play_icn_src.png");
  72.   background-size: cover;
  73. }
  74.  
  75. .mc-inner {
  76.   text-align: center;
  77. }
  78.  
  79. .mci-icon {
  80.   display: inline-block;
  81.   width: 142rpx;
  82.   height: 142rpx;
  83. }
  84.  
  85. .mci-prev {
  86.   background: url("../../resources/images/play_btn_prev.png");
  87.   background-size: cover;
  88. }
  89.  
  90. .mci-play {
  91.   background: url("../../resources/images/play_btn_play.png");
  92.   background-size: cover;
  93. }
  94.  
  95. .mci-pause {
  96.   background: url("../../resources/images/play_btn_pause.png");
  97.   background-size: cover;
  98. }
  99.  
  100. .mci-next {
  101.   background: url("../../resources/images/play_btn_next.png");
  102.   background-size: cover;
  103. }
复制代码

 

  最后写一下播放列表的布局:

  1. <view class="play-list" hidden="{{showPlayList}}">
  2.     <view class="play-list-header">
  3.       <text>播放列表(185)</text>
  4.       <text class="play-list-clear">清空</text>
  5.     </view>
  6.     <view class="play-list-inner">
  7.       <block wx:for="{{playList}}" wx:key="unique">
  8.         <view class="play-item">
  9.           {{item.name}}
  10.         </view>
  11.       </block>
  12.     </view>
  13.     <view class="play-list-bottom" bindtap="closePlayList">关闭</view>
  14.   </view>
复制代码

 

  格式文件为:

  1. .play-list {
  2.   position: absolute;
  3.   top: 20%;
  4.   bottom: 0;
  5.   left: 0;
  6.   width: 100%;
  7.   z-index: 99;
  8.   background: rgba(255, 255, 255, 0.95);   
  9. }
  10.  
  11. .play-list-header {
  12.   line-height: 88rpx;
  13.   font-size: 32rpx;
  14.   text-align: center;
  15.   border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
  16. }
  17.  
  18. .play-list-clear {
  19.   position: absolute;
  20.   right: 30rpx;
  21.   font-size: 28rpx;
  22.   color: rgba(0, 0, 0, 0.6);
  23. }
  24.  
  25. .play-list-bottom {
  26.   position: absolute;
  27.   width: 100%;
  28.   bottom: 0;
  29.   height: 100rpx;
  30.   line-height: 100rpx;
  31.   text-align: center;
  32.   font-size: 32rpx;
  33.   border-top: 2rpx solid rgba(0, 0, 0, 0.1);
  34. }
  35.  
  36. .play-list-inner {
  37.   position: absolute;
  38.   top: 90rpx;
  39.   bottom: 102rpx;
  40.   width: 100%;
  41.   overflow-x: hidden;
  42.   overflow-y: auto;
  43.   padding-left: 20rpx;
  44. }
  45.  
  46. .play-item {
  47.   position: relative;
  48.   widows: 100%;
  49.   box-sizing: border-box;
  50.   padding-right: 90rpx;
  51.   height: 88rpx;
  52.   line-height: 88rpx;
  53.   font-size: 30rpx;
  54.   border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
  55.   white-space: nowrap;
  56.   overflow: hidden;
  57.   text-overflow: ellipsis;
  58. }
复制代码

 

  这里使用“z-index: 99;background: rgba(255, 255, 255, 0.95);”使播放列表覆盖部分音乐播放页面且背景半透明。

  

  最后我们使用一个view来为整个页面加载背景,这个背景为我们获取的图片加上模糊效果。最后用一个view包裹所有布局。

  1. <view class="play-view">
  2.    ...
  3.    <view class="bg blur" style="background-image:url('{{playingMusic.img}}');"></view>
  4. </view>
复制代码

 

  使用格式文件添加模糊效果:

  1. .paly-view {
  2.   display: block;
  3.   position: relative;
  4.   width: 100%;
  5.   height: 100%;
  6.   overflow: hidden;
  7. }
  8.  
  9. .blur {
  10.   filter: blur(80rpx);
  11. }
  12.  
  13. .bg {
  14.   position: absolute;
  15.   left: 0;
  16.   right: 0;
  17.   top: 0;
  18.   bottom: 0;
  19.   background-size: cover;
  20.   background-position: bottom center;
  21. }
复制代码

 

  最后我们加载数据:

  1. var app = getApp()
  2.  
  3. Page({
  4.     data: {
  5.         playList: [],
  6.         playIndex: 0,
  7.         showPlayList: true,
  8.         playingMusic: {},
  9.         musicTime: 0,
  10.         currTime: 0,
  11.         musicTimeStr: 0,
  12.         currTimeStr: 0,
  13.         isPlay: false,
  14.         playInv: 0,
  15.         playPro: '',
  16.         playType: 1
  17.     },
  18.     onl oad: function (options) {
  19.         // 页面初始化 options为页面跳转所带来的参数
  20.         var self = this;
  21.         var list = app.globalData.playList;
  22.         var playingMusic = null;
  23.         if (list.length) {
  24.             var index = app.globalData.playIndex;
  25.             index = (list.length - 1 < index) ? list.length - 1 : index;
  26.             playingMusic = list[index];
  27.             this.setData({
  28.                 playList: list,
  29.                 playIndex: index,
  30.                 playingMusic: playingMusic
  31.             });
  32.         }
  33.         wx.playBackgroundAudio({
  34.             dataUrl: list[index].url,
  35.             title: list[index].title,
  36.             coverImgUrl: list[index].img,
  37.             success: function () {
  38.             },
  39.             fail: function () {
  40.                 console.log('播放失败!');
  41.             }
  42.         });
  43.     },
  44.     changePlayType: function (e) {
  45.         var dataSet = e.currentTarget.dataset;
  46.         if (dataSet.type == 1) {
  47.             this.setData({
  48.                 playType: 2
  49.             });
  50.         }
  51.         if (dataSet.type == 2) {
  52.             this.setData({
  53.                 playType: 0
  54.             });
  55.         }
  56.         if (dataSet.type == 0) {
  57.             this.setData({
  58.                 playType: 1
  59.             });
  60.         }
  61.     },
  62.     closePlayList: function (e) {
  63.         this.setData({
  64.             showPlayList: true
  65.         })
  66.     },
  67.     showPlayList: function (e) {
  68.         this.setData({
  69.             showPlayList: false
  70.         })
  71.     },
  72.     //三个按钮的点击事件
  73.     pauseMusic: function () {
  74.     },
  75.     playNextMusic: function () {
  76.     },
  77.     playPreMusic:function(){
  78.     },
  79. })
复制代码

 

  上一节:微信小程序小白项目开发案例之音乐播放器-完成相似页面

  下一节:微信小程序小白项目开发案例之音乐播放器-总结

  

上一篇:微信小程序音乐播放器,音乐播放器小程序制作步骤(相似页面)
下一篇:微信小程序音乐播放器,音乐播放器小程序制作步骤(项目需求)
相关资讯 Releva ntnews
解决方案 Solutions
相关热点 Hot spot
郑州做排名中正确使用关键词优化的方法
  1. 我们的优势
  2. 我们的实力
  3. 选择我们的理由
咨询电话(微信同号)

137-3385-8848(同微信)

豫ICP备16014343号-1

Copyright © 2017-2022 版权所有 酷微科技 Rights Reserved

电话咨询 在线咨询 服务项目 SEO优化