微信小程序音樂(lè)播放器,音樂(lè)播放器小程序制作步驟(音樂(lè)播放頁(yè))
這個(gè)頁(yè)面分為3個(gè)部分:頂部信息欄(歌曲名/專輯名),中間的唱片圖,以及底下的控制按鈕。
首先是信息欄:
- <view class="song-info">
- <text class="song-title">{{playingMusic.name}}</text>
- <text class="song-subtitle">
- <block wx:for="{{playingMusic.singer}}" wx:key="unique">
- <block wx-if="{{index!=0}}">*</block>{{item.name}}</block>
- </text>
- </view>
這部分很簡(jiǎn)單,與我們前面寫過(guò)的類似,多個(gè)歌手之間用“*”分隔。格式文件為:
- .song-info {
- width:100%;
- padding:30rpx;
- box-sizing:border-box;
- text-align:center;
- }
- .song-title {
- display:block;
- width:100%;
- color:#fff;
- font-size:36rpx;
- line-height:60rpx;
- overflow:hidden;
- white-space:nowrap;
- text-overflow:ellipsis;
- }
- .song-subtitle {
- display:block;
- width:100%;
- font-size:28rpx;
- line-height:40rpx;
- color:rgba(255,255,255,.6);
- overflow:hidden;
- white-space:nowrap;
- text-overflow:ellipsis;
- }
然后是中間的圖片,這部分我們以動(dòng)畫(huà)形式表現(xiàn),讓唱片一直旋轉(zhuǎn),先看布局文件:
- <view class="cd-info">
- <view class="cd-gan"></view>
- <view class="cd-inner cd-animation">
- <image class="cd-img" src="{{playingMusic.img}}"></image>
- </view>
- </view>
- “cd-gan”是唱片部分里白色的播放桿部分,“cd-inner”是唱片部分,在這里為它添加表示唱片的黑色背景,然后在里門用小一圈的“cd-img”來(lái)加載我們獲取的網(wǎng)絡(luò)圖片。最后為整個(gè)唱片添加動(dòng)畫(huà)“cd-animation”。這些工作全部由格式文件完成。
- .cd-info {
- position: relative;
- width: 100%;
- text-align: center;
- padding-top: 120rpx;
- }
- .cd-gan {
- position: absolute;
- left: 50%;
- margin-top: -80rpx;
- margin-left: -150rpx;
- display: block;
- width: 300rpx;
- height: 200rpx;
- background: url('../../resources/images/cd_g.png');
- background-size: cover;
- z-index: 10;
- }
- .cd-inner {
- position: relative;
- display: inline-block;
- z-index: 4;
- height: 500rpx;
- width: 500rpx;
- background: url('../../resources/images/cd_b.png');
- background-size: cover;
- text-align: center;
- padding-top: 100rpx;
- box-sizing: border-box;
- }
- .cd-animation {
- -webkit-animation: circle 10s infinite linear;
- animation: circle 10s infinite linear;
- }
- .cd-img {
- display: inline-block;
- width: 300rpx;
- height: 300rpx;
- border-radius: 50%;
- }
- @keyframes circle {
- 0% {
- transform: rotate(0deg);
- }
- 100% {
- transform: rotate(360deg);
- }
- }
這里面大多數(shù)的代碼相信大家已經(jīng)很熟悉了,重點(diǎn)看一下“cd-animation”這一部分,這里加載了動(dòng)畫(huà)“circle”并設(shè)置了動(dòng)畫(huà)時(shí)長(zhǎng)與無(wú)限循環(huán),這樣就實(shí)現(xiàn)了在音樂(lè)播放時(shí),唱片一直旋轉(zhuǎn)的效果。“circle”動(dòng)畫(huà)我們使用關(guān)鍵幀keyframes來(lái)實(shí)現(xiàn)。
完成這兩部分后我們以一個(gè)view來(lái)包裹它們,確定它在頁(yè)面的位置。
- <view class="main-box">
- <view class="song-info">
- <text class="song-title">{{playingMusic.name}}</text>
- <text class="song-subtitle">
- <block wx:for="{{playingMusic.singer}}" wx:key="unique">
- <block wx-if="{{index!=0}}">*</block>{{item.name}}</block>
- </text>
- </view>
- <view class="cd-info">
- <view class="cd-gan"></view>
- <view class="cd-inner cd-animation">
- <image class="cd-img" src="{{playingMusic.img}}"></image>
- </view>
- </view>
- </view>
- .main-box {
- position: absolute;
- top: 0;
- bottom: 308rpx;
- z-index: 3;
- width: 100%;
- background: rgba(0, 0, 0, 0.2);
- }
接著我們完成底下的操作部分。
- <view class="ctre-box">
- <view class="slider-box">
- <text class="slider-text st-l">{{currTimeStr}}</text>
- <slider class="slider-inner"></slider>
- <text class="slider-text st-r">{{musicTimeStr}}</text>
- </view>
- <view class="music-ctr">
- <block wx-if="{{playType==0}}">
- <view class="music-sort ms-loop" data-type="{{playType}}" bindtap="changePlayType"></view>
- </block>
- <block wx-if="{{playType==1}}">
- <view class="music-sort ms-shuffle" data-type="{{playType}}" bindtap="changePlayType"></view>
- </block>
- <block wx-if="{{playType==2}}">
- <view class="music-sort ms-one" data-type="{{playType}}" bindtap="changePlayType"></view>
- </block>
- <view class="mc-inner">
- <view class="mci-icon mci-prev"></view>
- <view class="mci-icon mci-play"></view>
- <view class="mci-icon mci-next"></view>
- </view>
- <view class="music-list-btn" bindtap="showPlayList"></view>
- </view>
- </view>
操作控制部分由最上邊的進(jìn)度條部分“slider-box”和底下的操作按鈕“mucis-ctr”組成。進(jìn)度條我們使用slider組件,兩段用兩個(gè)text組件來(lái)顯示當(dāng)前播放時(shí)間與總時(shí)長(zhǎng)。操作按鈕部分,首先是播放模式的按鈕,根據(jù)playType的值,改變順序/隨機(jī)/單曲的播放模式并對(duì)應(yīng)加載不同的圖片。然后是3個(gè)按鈕,分別表示前一首/播放/下一首。最后是顯示播放列表的按鈕。
格式文件為:
- .slider-box {
- box-sizing: border-box;
- padding: 20rpx 130rpx;
- }
- .slider-text {
- position: absolute;
- display: block;
- width: 100rpx;
- height: 40rpx;
- line-height: 40rpx;
- font-size: 24rpx;
- color: #fff;
- }
- .st-l {
- left: 60rpx;
- }
- .st-r {
- top: 20rpx;
- right: 40rpx;
- text-align: right;
- }
- .slider-inner {
- width: 100%;
- }
- .ctre-box {
- height: 308rpx;
- position: absolute;
- bottom: 0;
- z-index: 3;
- width: 100%;
- background: rgba(0, 0, 0, 0.2);
- }
- .music-ctr {
- position: relative;
- padding: 20rpx 120rpx;
- }
- .music-sort {
- position: absolute;
- left: 20rpx;
- width: 108rpx;
- height: 108rpx;
- }
- .ms-loop {
- background: url("../../resources/images/play_icn_loop.png");
- background-size: cover;
- }
- .ms-one {
- background: url("../../resources/images/play_icn_one.png");
- background-size: cover;
- }
- .ms-shuffle {
- background: url("../../resources/images/play_icn_shuffle.png");
- background-size: cover;
- }
- .music-list-btn {
- position: absolute;
- top: 36rpx;
- right: 20rpx;
- width: 108rpx;
- height: 108rpx;
- background: url("../../resources/images/play_icn_src.png");
- background-size: cover;
- }
- .mc-inner {
- text-align: center;
- }
- .mci-icon {
- display: inline-block;
- width: 142rpx;
- height: 142rpx;
- }
- .mci-prev {
- background: url("../../resources/images/play_btn_prev.png");
- background-size: cover;
- }
- .mci-play {
- background: url("../../resources/images/play_btn_play.png");
- background-size: cover;
- }
- .mci-pause {
- background: url("../../resources/images/play_btn_pause.png");
- background-size: cover;
- }
- .mci-next {
- background: url("../../resources/images/play_btn_next.png");
- background-size: cover;
- }
最后寫一下播放列表的布局:
- <view class="play-list" hidden="{{showPlayList}}">
- <view class="play-list-header">
- <text>播放列表(185)</text>
- <text class="play-list-clear">清空</text>
- </view>
- <view class="play-list-inner">
- <block wx:for="{{playList}}" wx:key="unique">
- <view class="play-item">
- {{item.name}}
- </view>
- </block>
- </view>
- <view class="play-list-bottom" bindtap="closePlayList">關(guān)閉</view>
- </view>
格式文件為:
- .play-list {
- position: absolute;
- top: 20%;
- bottom: 0;
- left: 0;
- width: 100%;
- z-index: 99;
- background: rgba(255, 255, 255, 0.95);
- }
- .play-list-header {
- line-height: 88rpx;
- font-size: 32rpx;
- text-align: center;
- border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
- }
- .play-list-clear {
- position: absolute;
- right: 30rpx;
- font-size: 28rpx;
- color: rgba(0, 0, 0, 0.6);
- }
- .play-list-bottom {
- position: absolute;
- width: 100%;
- bottom: 0;
- height: 100rpx;
- line-height: 100rpx;
- text-align: center;
- font-size: 32rpx;
- border-top: 2rpx solid rgba(0, 0, 0, 0.1);
- }
- .play-list-inner {
- position: absolute;
- top: 90rpx;
- bottom: 102rpx;
- width: 100%;
- overflow-x: hidden;
- overflow-y: auto;
- padding-left: 20rpx;
- }
- .play-item {
- position: relative;
- widows: 100%;
- box-sizing: border-box;
- padding-right: 90rpx;
- height: 88rpx;
- line-height: 88rpx;
- font-size: 30rpx;
- border-bottom: 2rpx solid rgba(0, 0, 0, 0.1);
- white-space: nowrap;
- overflow: hidden;
- text-overflow: ellipsis;
- }
這里使用“z-index: 99;background: rgba(255, 255, 255, 0.95);”使播放列表覆蓋部分音樂(lè)播放頁(yè)面且背景半透明。
最后我們使用一個(gè)view來(lái)為整個(gè)頁(yè)面加載背景,這個(gè)背景為我們獲取的圖片加上模糊效果。最后用一個(gè)view包裹所有布局。
- <view class="play-view">
- ...
- <view class="bg blur" style="background-image:url('{{playingMusic.img}}');"></view>
- </view>
使用格式文件添加模糊效果:
- .paly-view {
- display: block;
- position: relative;
- width: 100%;
- height: 100%;
- overflow: hidden;
- }
- .blur {
- filter: blur(80rpx);
- }
- .bg {
- position: absolute;
- left: 0;
- right: 0;
- top: 0;
- bottom: 0;
- background-size: cover;
- background-position: bottom center;
- }
最后我們加載數(shù)據(jù):
- var app = getApp()
- Page({
- data: {
- playList: [],
- playIndex: 0,
- showPlayList: true,
- playingMusic: {},
- musicTime: 0,
- currTime: 0,
- musicTimeStr: 0,
- currTimeStr: 0,
- isPlay: false,
- playInv: 0,
- playPro: '',
- playType: 1
- },
- onLoad: function (options) {
- // 頁(yè)面初始化 options為頁(yè)面跳轉(zhuǎn)所帶來(lái)的參數(shù)
- var self = this;
- var list = app.globalData.playList;
- var playingMusic = null;
- if (list.length) {
- var index = app.globalData.playIndex;
- index = (list.length - 1 < index) ? list.length - 1 : index;
- playingMusic = list[index];
- this.setData({
- playList: list,
- playIndex: index,
- playingMusic: playingMusic
- });
- }
- wx.playBackgroundAudio({
- dataUrl: list[index].url,
- title: list[index].title,
- coverImgUrl: list[index].img,
- success: function () {
- },
- fail: function () {
- console.log('播放失敗!');
- }
- });
- },
- changePlayType: function (e) {
- var dataSet = e.currentTarget.dataset;
- if (dataSet.type == 1) {
- this.setData({
- playType: 2
- });
- }
- if (dataSet.type == 2) {
- this.setData({
- playType: 0
- });
- }
- if (dataSet.type == 0) {
- this.setData({
- playType: 1
- });
- }
- },
- closePlayList: function (e) {
- this.setData({
- showPlayList: true
- })
- },
- showPlayList: function (e) {
- this.setData({
- showPlayList: false
- })
- },
- //三個(gè)按鈕的點(diǎn)擊事件
- pauseMusic: function () {
- },
- playNextMusic: function () {
- },
- playPreMusic:function(){
- },
- })
上一節(jié):微信小程序小白項(xiàng)目開(kāi)發(fā)案例之音樂(lè)播放器-完成相似頁(yè)面
下一節(jié):微信小程序小白項(xiàng)目開(kāi)發(fā)案例之音樂(lè)播放器-總結(jié)
第二部分:如何開(kāi)通一個(gè)小商店