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

界面做的確實(shí)挺丑的,先上wxss文件
//index.wxss
.button-style{
background-color: #eee;
border-radius: 8rpx;
margin: 20rpx;
}
只是頂一個(gè)簡(jiǎn)單的按鈕的圓角和間距,顏色這個(gè)我還是用primary這個(gè)小綠色。
下面是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è)置播放進(jìn)度</button> <button class="button-style" type="primary" bindtap="listenerButtonStop">停止播放</button> <button class="button-style" type="primary" bindtap="listenerButtonGetPlayState">獲取播放狀態(tài)</button>
沒辦法,用開發(fā)者工具打出來(lái)就是這樣的丑格式
下面是重點(diǎn)index.js
//index.js
//獲取應(yīng)用實(shí)例
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 選定音頻的長(zhǎng)度(單位:s),只有在當(dāng)前有音樂播放時(shí)返回
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ù)鏈接,只有在當(dāng)前有音樂播放時(shí)返回
console.log('dataUrl:' + res.dataUrl)
},
fail: function() {
// fail
},
complete: function() {
// complete
}
})
},
/**
* 設(shè)置進(jìn)度
*/
listenerButtonSeek:function(){
wx.seekBackgroundAudio({
position: 40
})
},
/**
* 停止播放
*/
listenerButtonStop:function(){
wx.stopBackgroundAudio({
})
console.log('停止播放')
},
onLoad:function(options){
// 頁(yè)面初始化 options為頁(yè)面跳轉(zhuǎn)所帶來(lái)的參數(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')
})
}
})
里面可以先按照順序來(lái)看onLoad函數(shù),里面定義了三個(gè)監(jiān)聽函數(shù),可以看到console里面效果如圖

其實(shí)里面的api使用不是很難,在button標(biāo)簽里面寫好bindtap事件名,在js方法中對(duì)應(yīng)相應(yīng)的處理function,像wx.playBackgroundAudio這個(gè)只需要你去填充一些參數(shù)即可,不懂得可以參考api文檔(API入口)。
附上github源碼地址
為大家推薦現(xiàn)在關(guān)注度比較高的微信小程序教程一篇:《微信小程序開發(fā)教程》小編為大家精心整理的,希望喜歡。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript 精美貪吃蛇實(shí)現(xiàn)流程
看起來(lái)好像很復(fù)雜的貪吃蛇,到底是怎么用JavaScript去實(shí)現(xiàn)的?下面就來(lái)一步一步地,剖析怎么用JavaScript,放在任意一個(gè)瀏覽器中,把貪吃蛇搞起來(lái)2021-11-11
JavaScript如何實(shí)現(xiàn)監(jiān)聽鍵盤輸入和鼠標(biāo)監(jiān)點(diǎn)擊
這篇文章主要介紹了JavaScript如何實(shí)現(xiàn)監(jiān)聽鍵盤輸入和鼠標(biāo)監(jiān)點(diǎn)擊,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
JavaScript關(guān)鍵字this的使用方法詳解
與其他語(yǔ)言相比,函數(shù)的 this 關(guān)鍵字在 JavaScript 中的表現(xiàn)略有不同,此外,在嚴(yán)格模式和非嚴(yán)格模式之間也會(huì)有一些差別,本文就給大家講解一下JavaScript關(guān)鍵字中的this,需要的朋友可以參考下2023-08-08
JavaScript在控件上添加倒計(jì)時(shí)功能的實(shí)現(xiàn)代碼
JavaScript在控件上添加倒計(jì)時(shí)功能,主要原理是利用控件的setEnable(true)/setEnable(false)來(lái)進(jìn)行設(shè)置控件的可用與不可用狀態(tài),實(shí)現(xiàn)過程非常簡(jiǎn)單,需要的的朋友參考下吧2017-07-07
JS彈出對(duì)話框返回值代碼(asp.net后臺(tái))
JS彈出對(duì)話框返回值代碼,需要的朋友可以參考下。2010-12-12
layer實(shí)現(xiàn)登錄彈框,登錄成功后關(guān)閉彈框并調(diào)用父窗口的例子
今天小編就為大家分享一篇layer實(shí)現(xiàn)登錄彈框,登錄成功后關(guān)閉彈框并調(diào)用父窗口的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-09-09

