目錄
一 小程式主體功能介紹
二 常用組件和API介紹
音頻API
常用組件
三 整體布局
資料定義
主體頁面
運行效果
四 播放器頁面
布區域分
樣式部分
運行效果
五 播放串列頁面
布區域分
樣式部分
運行效果
全部功能實作代碼index.js
一 小程式主體功能介紹
本小程式主要實作,音樂的播放、暫停,下一曲的切換,以及播放串列和當前播放歌曲的詳細資訊查看等,
二 常用組件和API介紹
1.音頻API
1.1 介紹
創建音頻時需要先創建一個物件實體,從而參考該物件的方法和屬性,
var audioCtx=wx.createInnerAudioContext()
1.2 常用屬性方法
屬性
src:音瞥澩的地址;
autoplay:是否自動播放,默認false;
loop: 是否回圈播放,默認為false;
startTime: 開始播放的位置,默認為0;
currentTime: 當前播放的位置;
duration: 音頻的長度;
paused:當前暫停或停止的狀態;
方法
play(): 播放;
stop(): 停止,再播放重頭開始;
pause(): 暫停,再播放從當前位置開始;
seek(): 跳到指定的位置;
onError(): 音頻播放錯誤事件;
onEnded(): 音頻自然播放結束事件;
onPlay(): 音頻播放事件;
onTimeUpdate(): 音頻播放進度更新事件;
2.常用組件
2.1 swiper組件
介紹
swiper組件是滑塊視圖容器,經常用于實作輪播圖,在音樂播放器小程式中可以實作標簽頁的切換,
屬性
| 屬性 | 型別 | 說明 |
| indicator-dots | Boolean | 是否顯示頁面的指示點,默認為false |
| indicator-color | Color | 指示點的顏色 |
| indicator-active-color | Color | 選中的指示點顏色 |
| autoplay | Boolean | 是否自動切換,默認為false |
| current | Number | 當前所在滑塊的index,默認為0 |
| current-item-id | String | 當前所在滑塊的item-id |
| interval | Number | 自動切換時間間隔(ms) |
| duration | Number | 滑動影片時長(ms) |
| bindchange | EventHandle | current改變時會觸發change事件 |
| circular | Boolean | 是否采用銜接滑動,默認false |
代碼使用
<swiper current="1">
<swiper-item >0</swiper-item>
<swiper-item >1</swiper-item>
<swiper-item >2</swiper-item>
</swiper>
2.2 include代碼參考
介紹
當一個wxml檔案中代碼過多,或wxml中有部分相同的代碼時,可以將他們分離開,用include進行引入,
代碼使用
<include src="header.wxml"/>
<view>body</view>
<include src="footer.wxml" />
2.3 scroll-view組件
介紹
scroll-view組件用于實作可滾動視圖區域,一般來說,當頁面高度超出了顯示區域的高度時,先設定外層容器高度,使其低于內部容器高度,然后在外層容器樣式中設定滾動方向即可,
屬性
| 屬性 | 說明 |
| scroll-x | 允許橫向滾動,默認為false |
| scroll-y | 允許縱向滾動,默認為false |
| scroll-top | 設定豎向滾動條的位置 |
| scroll-left | 設定橫向滾動條的位置 |
| bindscrolltoupper | 滾動到頂部/左邊觸發的事件 |
| bindscrolltolower | 滾動到底部/右邊觸發的事件 |
代碼使用
<scroll-view scroll-x scroll-y style="height:200px" bindscroll="scroll">
<view style="width:200%; height:400px; background:#ccc"></view>
</scroll-view>
在.js頁面中可以添加scroll處理函式,來查看具體數值
scroll:function(e){
console.log(e.detail)
}
2.4 slider組件
介紹
slider組件是小程式表單組件中的一種,用于滑動選擇某一個值,在本專案中將用來實作播放器的進度條
屬性
| 屬性 | 說明 |
| min | 最小值,默認為0 |
| max | 最大值,默認為100 |
| step | 步長,取值大于0 |
| value | 當前取值,默認為0 |
| activeColor | 已選擇的顏色,默認為#1aad19 |
| backgroundColor | 背景條顏色,默認為#e9e9e9 |
| block-size | 滑塊的大小 |
| block-color | 滑塊的顏色,默認為#ffffff |
| show-value | 是否顯示當前value,默認為false |
| bindchange | 完成一次拖動后觸發的事件 |
| bindchanging | 拖動程序中觸發的事件 |
三 整體布局
1. 資料定義
1.1 路徑
pages/index/index.js 檔案的data物件定義基礎資料playlist
1.2 資料
data: {
item:0,
tab:0,
// 播放串列
playlist:[{
id:1,
title:"紀念",
singer:"雷心雨",
src:"/images/1.mp3",
coverImgUrl:"/images/cover.jpg"
},{
id:2,
title:"雪落下的聲音",
singer:"郁可唯",
src:"/images/2.mp3",
coverImgUrl:"/images/cover2.jpg"
},{
id:3,
title:"只要平凡",
singer:"張杰",
src:"/images/3.mp3",
coverImgUrl:"/images/cover3.jpg"
}
,{
id:4,
title:"我會很勇敢",
singer:"張雅莉",
src:"/images/4.mp3",
coverImgUrl:"/images/cover4.jpg"
} ],
state:"paused",
// 播放的索引值
playIndex:0,
//設定的默認值
play:{
// 當前時間
currentTime:'00.00',
// 總時間
duration:'00.00',
// 播放進度
percent:0,
title:'',
singer:'',
coverImgUrl:"/images/cover.jpg",
}
},
2. 主體頁面
2.1 布局頁面index.wxml
<!--index.wxml-->
<!-- 標簽頁標題 -->
<view class="tab">
<view class="tab-item {{tab==0 ? 'active' : ''}} " bindtap="changeItem" data-item="0">音樂推薦</view>
<view class="tab-item {{tab==1 ? 'active' : ''}}" bindtap="changeItem" data-item="1">播放器</view>
<view class="tab-item {{tab==2 ? 'active' : ''}} " bindtap="changeItem" data-item="2">播放串列</view>
</view>
<!-- 內容區域 -->
<view class="content" >
<swiper current="{{item}}" bindchange="changeTab">
<swiper-item >
<include src="info.wxml"></include>
</swiper-item>
<swiper-item >
<include src="play.wxml"></include>
</swiper-item>
<swiper-item>
<include src="playlist.wxml"></include>
</swiper-item>
</swiper>
</view>
<!-- 底部播放器 -->
<view class="player">
<image class="player-cover" src="{{play.coverImgUrl}}"></image>
<view class="player-info">
<view class="player-info-title">{{play.title}}</view>
<view class="player-info-singer">{{play.singer}}</view>
</view>
<!-- 切換到播放串列 -->
<view class="player-controls">
<image src="/images/play-1.jpg" bindtap="changePage" data-page="2"></image>
<!-- 播放或暫停 -->
<image wx:if="{{state=='paused'}}" src="/images/play-2.jpg"
bindtap="play"></image>
<image wx:else src="/images/play-4.jpg" bindtap="pause"></image>
<!-- 下一曲 -->
<image src="/images/play-3.jpg" bindtap="next"></image>
</view>
</view>
2.2 樣式部分
page{
display: flex;
flex-direction: column;
background-color: #c8f5fd;
color: rgb(22, 17, 13);
height: 100%;
}
.tab{
display: flex;
}
.tab-item{
flex: 1;
font-size: 10pt;
text-align: center;
line-height: 72rpx;
border-bottom: 6rpx solid #eee;
}
.tab-item.active{
color:#584acf;
border-bottom-color:#7c6fee;
}
.content{
flex: 1;
}
.content>swiper{
height:100%;
}
.player{
background: rgb(156, 209, 240);
border-top: 1rpx solid #a8ddf8;
height: 112rpx;
}
/* 底部播放器 */
.player{
display: flex;
align-items: center;
background:rgb(117, 185, 224);
border-top: 1rpx solid #7c8dec;
height: 112rpx;
}
/* 覆寫圖片設定 */
.player-cover{
width:80rpx;
height: 80rpx;
margin-left: 15rpx;
border-radius: 10rpx;
border:2rpx solid rgb(204, 200, 200);
}
/* 歌曲資訊設定 */
.player-info{
flex:1;
font-size: 10pt;
line-height: 38rpx;
margin-left: 20rpx;
padding-bottom:8rpx;
}
.player-info-singer{
color:rgb(189, 214, 245);
margin-top: 5rpx;
font-size:9pt;
}
/* 控制按鈕設定 */
.player-controls image{
width: 80rpx;
height: 80rpx;
margin-right: 10rpx;
}
2.3 底部播放、切換功能函式實作
audioCtx:null,
onReady: function () {
this.audioCtx=wx.createInnerAudioContext()
//默認選中第1曲
this.setMusic(0)
},
setMusic:function(index){
var music=this.data.playlist[index]
this.audioCtx.src=music.src
this.setData({
playIndex:index,
'play.title':music.title,
'play.singer':music.singer,
'play.coverImgUrl':music.coverImgUrl,
'play.currentTime':'00:00',
'play.duration':'00:00',
'play.percent':0
})
},
play:function(){
this.audioCtx.play()
this.setData({state:'running'})
},
pause:function(){
this.audioCtx.pause()
this.setData({state:'paused'})
},
next:function(){
var index=this.data.playlist>=this.data.playlist.length-1?0:this.data.playIndex+1
this.setMusic(index)
if(this.data.state=='running'){
this.play()
}
},
3. 運行效果
四 播放器頁面
1.布區域分
<view class="content-play">
<!-- 顯示音樂資訊 -->
<view class="content-play-info">
<view>{{play.title}}</view>
<view>—— {{play.singer}} ——</view>
</view>
<!-- 顯示專輯封面 -->
<view class="content-play-cover">
<image src="{{play.coverImgUrl}}" style="animation-play-state:{{state}}"></image>
</view>
<!-- 顯示播放進度 -->
<view class="content-play-progress">
<text>{{play.currentTime}}</text>
<view>
<!-- activeColor已選擇的不生效 -->
<slider bindchange="sliderChange" activeColor="#d33a31" block-size="12" backgroundColor="#ccc" value="{{play.percent}}"></slider>
</view>
<text>{{play.duration}}</text>
</view>
</view>
2. 樣式部分
/* 播放器頁面設定 */
.content-play{
display: flex;
flex-direction: column;
justify-content: space-around;
height: 100%;
text-align: center;
background-color: rgb(203, 235, 243);
}
.content-play-info{
color: rgb(7, 59, 102);
font-size: 11pt;
}
.content-play-cover image{
animation:rotateImage 10s linear infinite ;
width: 400rpx;
height:400rpx;
border-radius: 50%;
border:1rpx solid rgb(245, 241, 241);
}
@keyframes rotateImage{
from{
transform: rotate(0deg);
}
to{
transform: rotate(360deg);
}
}
/* 進度條設定 */
.content-play-progress{
display: flex;
align-items: center;
font-size: 9pt;
margin: 0 35rpx;
text-align: center;
}
.content-play-progress >view{
flex: 1;
}
3. 運行效果
五 播放串列頁面
1.布區域分
<scroll-view class="content-playlist" scroll-y>
<view class="playlist-item" wx:for="{{playlist}}" wx:key="id" bindtap="change" data-index="{{index}}">
<image class="playlist-cover" src="{{item.coverImgUrl}}"></image>
<view class="playlist-info">
<view class="playlist-info-title">{{item.title}}</view>
<view class="playlist-info-singer">{{item.singer}}</view>
</view>
<view class="playlist-controls">
<text wx:if="{{index==playIndex}}">正在播放</text>
</view>
</view>
</scroll-view>
2.樣式部分
/* 播放串列 */
.content-playlist{
background-color: rgb(175, 231, 247);
}
.playlist-item{
/* 每遍歷一個會自動換行 */
display: flex;
align-items: center;
border-bottom: 2rpx solid rgb(253, 250, 250);
height: 112rpx;
}
.playlist-cover{
width:80rpx;
height:80rpx;
margin-left: 15rpx;
border-radius: 10rpx;
border: 1rpx solid rgb(248, 245, 245);
}
.playlist-info{
/* 字體為垂直排列 */
flex: 1;
font-size: 10pt;
margin-right: 25rpx;
margin-left: 10rpx;
color: #000;
}
3.運行效果

4.全部功能實作代碼index.js
// index.js
// 獲取應用實體
const app = getApp()
Page({
// 頁面初始資料
data: {
item:0,
//記錄當前頁的索引
tab:0,
// 播放串列
playlist:[{
id:1,
title:"紀念",
singer:"雷心雨",
src:"/images/1.mp3",
coverImgUrl:"/images/cover.jpg"
},{
id:2,
title:"雪落下的聲音",
singer:"郁可唯",
src:"/images/2.mp3",
coverImgUrl:"/images/cover2.jpg"
},{
id:3,
title:"只要平凡",
singer:"張杰",
src:"/images/3.mp3",
coverImgUrl:"/images/cover3.jpg"
}
,{
id:4,
title:"我會很勇敢",
singer:"張雅莉",
src:"/images/4.mp3",
coverImgUrl:"/images/cover4.jpg"
} ],
state:"paused",
// 播放的索引值
playIndex:0,
//設定的默認值
play:{
// 當前時間
currentTime:'00.00',
// 歌曲總時間
duration:'00.00',
// 播放進度
percent:0,
title:'',
singer:'',
coverImgUrl:"/images/cover.jpg",
}
},
// 保存在page里面了,音頻物件
audioCtx:null,
changeItem:function(e){
//設定獲取item的值,來實作頁面切換
this.setData({
item:e.target.dataset.item
})
},
changeTab:function(e){
this.setData({
//當前頁的索引
tab:e.detail.current
})
},
// 手動控制進度
sliderChange:function(e){
var second=e.detail.value *this.audioCtx.duration / 100
//跳到指定位置
this.audioCtx.seek(second)
},
onReady:function(){
//獲取音頻播放物件
this.audioCtx=wx.createInnerAudioContext()
var that=this
// 播放失敗檢測
this.audioCtx.onError(function(){
console.log("播放失敗:"+that.audioCtx.src)
})
// 播放完成自動轉為下一曲
this.audioCtx.onEnded(function(){
that.next()
})
// 格式化時間
function formatTime(time){
var minute=Math.floor(time/60)%60;
var second=Math.floor(time)%60;
return(minute<10? '0'+minute:minute)+":"+
(second<10?'0'+second:second)
}
//自動更新播放進度
// 舊版本里面需要先呼叫onplay,要不無法啟動onTimeUpdate
this.audioCtx.onPlay(function(){})
this.audioCtx.onTimeUpdate(function(){
that.setData({
//獲取總時間
'play.duration':formatTime(that.audioCtx.duration),
//當前歌曲播放的時長
'play.currentTime':formatTime(that.audioCtx.currentTime),
'play.percent':that.audioCtx.currentTime/that.audioCtx.duration*100
})
})
this.setMusic(0)
},
setMusic:function(index){
var music=this.data.playlist[index]
this.audioCtx.src=music.src
this.setData({
playIndex:index,
'play.title':music.title,
'play.singer':music.singer,
"play.coverImgUrl":music.coverImgUrl,
"play.currentTime":'00:00',
"play.duration":'00:00',
"play.percent":0
})
},
play:function(){
this.audioCtx.play()
this.setData({
state:"running"
})
},
pause:function(){
this.audioCtx.pause()
this.setData({
state:"paused"
})
},
next:function(){
var index=this.data.playIndex >=this.data.playlist.length-1 ? 0 : this.data.playIndex+1
this.setMusic(index)
if(this.data.state=="running"){
this.play()
}
},
// 播放串列中的換曲功能
change:function(e){
this.setMusic(e.currentTarget.dataset.index);
this.play();
}
})
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/339200.html
標籤:其他
上一篇:TCP三次握手四次揮手
下一篇:初識C語言——階段性小結(一)


