微信小程序?qū)崿F(xiàn)音樂播放器
今天繼續(xù)玩小程序的api,看著別人例子跟著做一個小程序,留下一個腳印吧。末尾附上github源碼地址。實現(xiàn)以下微信小程序的音樂播放器,先看下效果圖

界面做的確實挺丑的,先上wxss文件
//index.wxss
.button-style{
background-color: #eee;
border-radius: 8rpx;
margin: 20rpx;
}
只是頂一個簡單的按鈕的圓角和間距,顏色這個我還是用primary這個小綠色。
下面是index.wxml文件
//index.wxml <button class="button-style" type="primary" bindtap="listenerButtonPlay">播放</button> <button class="button-style" type="primary" bindtap="listenerButtonPause">暫停</button> <button class="button-style" type="primary" bindtap="listenerButtonSeek">設(shè)置播放進度</button> <button class="button-style" type="primary" bindtap="listenerButtonStop">停止播放</button> <button class="button-style" type="primary" bindtap="listenerButtonGetPlayState">獲取播放狀態(tài)</button>
沒辦法,用開發(fā)者工具打出來就是這樣的丑格式
下面是重點index.js
//index.js
//獲取應(yīng)用實例
var app = getApp()
Page({
data:{
},
//播放
listenerButtonPlay:function(){
wx.playBackgroundAudio({
dataUrl: 'http://ac-5g9r20ds.clouddn.com/e54ad7f0a834b9c07ec6.mp3',
title:'李宗盛',
//圖片地址地址
coverImgUrl:'http://ac-5g9r20ds.clouddn.com/63bedb5f584234b6827c.jpg'
})
},
//監(jiān)聽button暫停按鈕
listenerButtonPause:function(){
wx.pauseBackgroundAudio({
});
console.log('暫停播放')
},
/**
* 播放狀態(tài)
*/
listenerButtonGetPlayState:function(){
wx.getBackgroundAudioPlayerState({
success: function(res){
// success
//duration 選定音頻的長度(單位:s),只有在當前有音樂播放時返回
console.log('duration:' + res.duration)
console.log('currentPosition:' + res.currentPosition)
//status 播放狀態(tài)(2:沒有音樂在播放,1:播放中,0:暫停中)
console.log('status:' + res.status)
console.log('downloadPercent:' + res.downloadPercent)
//dataUrl 歌曲數(shù)據(jù)鏈接,只有在當前有音樂播放時返回
console.log('dataUrl:' + res.dataUrl)
},
fail: function() {
// fail
},
complete: function() {
// complete
}
})
},
/**
* 設(shè)置進度
*/
listenerButtonSeek:function(){
wx.seekBackgroundAudio({
position: 40
})
},
/**
* 停止播放
*/
listenerButtonStop:function(){
wx.stopBackgroundAudio({
})
console.log('停止播放')
},
onLoad:function(options){
// 頁面初始化 options為頁面跳轉(zhuǎn)所帶來的參數(shù)
/**
* 監(jiān)聽音樂播放
*/
wx.onBackgroundAudioPlay(function() {
// callback
console.log('onBackgroundAudioPlay')
})
/**
* 監(jiān)聽音樂暫停
*/
wx.onBackgroundAudioPause(function() {
// callback
console.log('onBackgroundAudioPause')
})
/**
* 監(jiān)聽音樂停止
*/
wx.onBackgroundAudioStop(function() {
// callback
console.log('onBackgroundAudioStop')
})
}
})
里面可以先按照順序來看onLoad函數(shù),里面定義了三個監(jiān)聽函數(shù),可以看到console里面效果如圖

其實里面的api使用不是很難,在button標簽里面寫好bindtap事件名,在js方法中對應(yīng)相應(yīng)的處理function,像wx.playBackgroundAudio這個只需要你去填充一些參數(shù)即可,不懂得可以參考api文檔(API入口)。
附上github源碼地址
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
webpack中的filename 和 chunkFilename 的區(qū)別實例解析
filename 指列在 entry 中,打包后輸出的文件的名稱,chunkFilename 指未列在 entry 中,卻又需要被打包出來的文件的名稱,這篇文章主要介紹了webpack中的filename 和 chunkFilename 的區(qū)別實例解析,需要的朋友可以參考下2023-11-11
layui 地區(qū)三級聯(lián)動 form select 渲染的實例
今天小編就為大家分享一篇layui 地區(qū)三級聯(lián)動 form select 渲染的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
微信小程序開發(fā)之實現(xiàn)食堂點餐系統(tǒng)
這篇文章主要為大家詳細介紹了如何通過微信小程序開發(fā)一個簡單的食堂點餐系統(tǒng),文中的示例代碼講解詳細,感興趣的小伙伴可以和小編一起學習一下2023-01-01
JavaScript將當前時間轉(zhuǎn)換成UTC標準時間的方法
這篇文章主要介紹了JavaScript將當前時間轉(zhuǎn)換成UTC標準時間的方法,涉及javascript中Date及toUTCString方法的使用技巧,需要的朋友可以參考下2015-04-04

