您好,登錄后才能下訂單哦!
這篇文章主要介紹“微信小程序如何制作音樂播放器”,在日常操作中,相信很多人在微信小程序如何制作音樂播放器問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”微信小程序如何制作音樂播放器”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
這個頁面分為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>
復制代碼
這部分很簡單,與我們前面寫過的類似,多個歌手之間用“*”分隔。格式文件為:
.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;
}
復制代碼
然后是中間的圖片,這部分我們以動畫形式表現(xià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)絡圖片。最后為整個唱片添加動畫“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)很熟悉了,重點看一下“cd-animation”這一部分,這里加載了動畫“circle”并設置了動畫時長與無限循環(huán),這樣就實現(xiàn)了在音樂播放時,唱片一直旋轉的效果?!癱ircle”動畫我們使用關鍵幀keyframes來實現(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);
}
復制代碼
接著我們完成底下的操作部分。
<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>
復制代碼
操作控制部分由最上邊的進度條部分“slider-box”和底下的操作按鈕“mucis-ctr”組成。進度條我們使用slider組件,兩段用兩個text組件來顯示當前播放時間與總時長。操作按鈕部分,首先是播放模式的按鈕,根據(jù)playType的值,改變順序/隨機/單曲的播放模式并對應加載不同的圖片。然后是3個按鈕,分別表示前一首/播放/下一首。最后是顯示播放列表的按鈕。
格式文件為:
.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">關閉</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);”使播放列表覆蓋部分音樂播放頁面且背景半透明。
最后我們使用一個view來為整個頁面加載背景,這個背景為我們獲取的圖片加上模糊效果。最后用一個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) {
// 頁面初始化 options為頁面跳轉所帶來的參數(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
})
},
//三個按鈕的點擊事件
pauseMusic: function () {
},
playNextMusic: function () {
},
playPreMusic:function(){
},
})
到此,關于“微信小程序如何制作音樂播放器”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。