2018/3/21 0:00:00
來源:不詳
作者:未知
這個頁面分為3個部分:頂部信息欄(歌曲名/專輯名),中間的唱片圖,以及底下的控制按鈕。
首先是信息欄:
-
<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>
復(fù)制代碼
這部分很簡單,與我們前面寫過的類似,多個歌手之間用“*”分隔。格式文件為:
-
.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;
-
}
復(fù)制代碼
然后是中間的圖片,這部分我們以動畫形式表現(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”來加載我們獲取的網(wǎng)絡(luò)圖片。最后為整個唱片添加動畫“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);
-
}
-
}
復(fù)制代碼
這里面大多數(shù)的代碼相信大家已經(jīng)很熟悉了,重點(diǎn)看一下“cd-animation”這一部分,這里加載了動畫“circle”并設(shè)置了動畫時長與無限循環(huán),這樣就實(shí)現(xiàn)了在音樂播放時,唱片一直旋轉(zhuǎn)的效果。“circle”動畫我們使用關(guān)鍵幀keyframes來實(shí)現(xiàn)。
完成這兩部分后我們以一個view來包裹它們,確定它在頁面的位置。
-
<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);
-
}
復(fù)制代碼
接著我們完成底下的操作部分。
-
<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>
復(fù)制代碼
操作控制部分由最上邊的進(jìn)度條部分“slider-box”和底下的操作按鈕“mucis-ctr”組成。進(jìn)度條我們使用slider組件,兩段用兩個text組件來顯示當(dāng)前播放時間與總時長。操作按鈕部分,首先是播放模式的按鈕,根據(jù)playType的值,改變順序/隨機(jī)/單曲的播放模式并對應(yīng)加載不同的圖片。然后是3個按鈕,分別表示前一首/播放/下一首。最后是顯示播放列表的按鈕。
格式文件為:
最后寫一下播放列表的布局:
-
<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>
復(fù)制代碼
格式文件為:
-
.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;
-
}
復(fù)制代碼
這里使用“z-index: 99;background: rgba(255, 255, 255, 0.95);”使播放列表覆蓋部分音樂播放頁面且背景半透明。
最后我們使用一個view來為整個頁面加載背景,這個背景為我們獲取的圖片加上模糊效果。最后用一個view包裹所有布局。
-
<view class="play-view">
-
...
-
<view class="bg blur" style="background-image:url('{{playingMusic.img}}');"></view>
-
</view>
復(fù)制代碼
使用格式文件添加模糊效果:
-
.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;
-
}
復(fù)制代碼
最后我們加載數(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) {
-
// 頁面初始化 options為頁面跳轉(zhuǎn)所帶來的參數(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
-
})
-
},
-
//三個按鈕的點(diǎn)擊事件
-
pauseMusic: function () {
-
},
-
playNextMusic: function () {
-
},
-
playPreMusic:function(){
-
},
-
})
復(fù)制代碼
上一節(jié):微信小程序小白項(xiàng)目開發(fā)案例之音樂播放器-完成相似頁面
下一節(jié):微信小程序小白項(xiàng)目開發(fā)案例之音樂播放器-總結(jié)
【本站聲明】
1、本站文章中所選用的圖片及文字來源于網(wǎng)絡(luò)以及用戶投稿,由于未聯(lián)系到知識產(chǎn)權(quán)人或未發(fā)現(xiàn)有關(guān)知識產(chǎn)權(quán)的登記,如有知識產(chǎn)權(quán)人并不愿意我們使用,如果有侵權(quán)請立即聯(lián)系。
2、本網(wǎng)站不對文章中所涉及的內(nèi)容真實(shí)性、準(zhǔn)確性、可靠性負(fù)責(zé),僅系客觀性描述,如您需要了解該類商品/服務(wù)詳細(xì)的資訊,請您直接與該類商品/服務(wù)的提供者聯(lián)系。
KESION 科汛軟件
KESION 科汛軟件是國內(nèi)領(lǐng)先的在線教育軟件及私域社交電商軟件服務(wù)提供商,長期專注于為企業(yè)提供在線教育軟件及社交電商SaaS平臺解決方案。
公司核心產(chǎn)品云開店SaaS社交電商服務(wù)平臺、在線教育SaaS服務(wù)平臺、教育企業(yè)數(shù)字化SaaS云平臺、企微營銷助手、私有化獨(dú)立部署品牌網(wǎng)校和在線教育咨詢等。KESION 不斷通過技術(shù)創(chuàng)新,提供產(chǎn)品和服務(wù),助力企業(yè)向數(shù)字化轉(zhuǎn)型,通過科技驅(qū)動商業(yè)革新,讓商業(yè)變得更智慧!