微信小程序音樂播放器開發(fā)
前言
這篇文章上一版本是用audio組件開發(fā)的播放器,隨后反應(yīng)音頻加載速度慢的問題

又用小程序內(nèi)置的背景音樂播放的方法寫了一遍,邏輯是一樣的邏輯,希望對(duì)大家有所幫助!
<view class='audiosBox'>
<view class="audioOpen" bindtap="listenerButtonPlay" wx:if="{{!isOpen}}">
<image class='image2' src="../../images/play.png" />
</view>
<view class="audioOpen" bindtap="listenerButtonPause" wx:if="{{isOpen}}">
<image src="../../images/pauses.png" />
</view>
<view class='slid'>
<slider bindchange="sliderChange" block-size="12px" step="2" value="{{offset}}" max="{{max}}" selected-color="#4c9dee" />
<view>
<text class='times'>{{starttime}}</text> <!-- 進(jìn)度時(shí)長 -->
<text class='times'>{{duration}}</text> <!-- 總時(shí)長 -->
</view>
</view>
</view>
js部分注意了:bug ios 播放時(shí)必須加title 不然會(huì)報(bào)錯(cuò)導(dǎo)致音樂不播放
//index.js
//獲取應(yīng)用實(shí)例
const bgMusic = wx.getBackgroundAudioManager()
const app = getApp()
Page({
data: {
isOpen: false,//播放開關(guān)
starttime: '00:00', //正在播放時(shí)長
duration: '06:41', //總時(shí)長
src:"http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E061FF02C31F716658E5C81F5594D561F2E88B854E81CAAB7806D5E4F103E55D33C16F3FAC506D1AB172DE8600B37E43FAD&fromtag=46"
},
// 播放
listenerButtonPlay: function () {
var that = this
//bug ios 播放時(shí)必須加title 不然會(huì)報(bào)錯(cuò)導(dǎo)致音樂不播放
bgMusic.title = '此時(shí)此刻'
bgMusic.epname = '此時(shí)此刻'
bgMusic.src = that.data.src;
bgMusic.onTimeUpdate(() => {
//bgMusic.duration總時(shí)長 bgMusic.currentTime當(dāng)前進(jìn)度
console.log(bgMusic.currentTime)
var duration = bgMusic.duration;
var offset = bgMusic.currentTime;
var currentTime = parseInt(bgMusic.currentTime);
var min = "0" + parseInt(currentTime / 60);
var max = parseInt(bgMusic.duration);
var sec = currentTime % 60;
if (sec < 10) {
sec = "0" + sec;
};
var starttime = min + ':' + sec; /* 00:00 */
that.setData({
offset: currentTime,
starttime: starttime,
max: max,
changePlay: true
})
})
//播放結(jié)束
bgMusic.onEnded(() => {
that.setData({
starttime: '00:00',
isOpen: false,
offset: 0
})
console.log("音樂播放結(jié)束");
})
bgMusic.play();
that.setData({
isOpen: true,
})
},
//暫停播放
listenerButtonPause(){
var that = this
bgMusic.pause()
that.setData({
isOpen: false,
})
},
listenerButtonStop(){
var that = this
bgMusic.stop()
},
// 進(jìn)度條拖拽
sliderChange(e) {
var that = this
var offset = parseInt(e.detail.value);
bgMusic.play();
bgMusic.seek(offset);
that.setData({
isOpen: true,
})
},
// 頁面卸載時(shí)停止播放
onUnload() {
var that = this
that.listenerButtonStop()//停止播放
console.log("離開")
},
})
css部分
/**index.wxss**/
.audiosBox{
width: 92%;
margin: auto;
height: 130rpx;
display: flex;
justify-content: space-between;
align-items: center;
background: #f6f7f7;
border-radius: 10rpx;
}
/*按鈕大小 */
.audioOpen{
width: 70rpx;
height: 70rpx;
border: 2rpx solid #4c9dee;
border-radius: 50%;
display: flex;
align-items: center;
justify-content: center;
margin-left: 20rpx;
}
image{
width: 30rpx;
height: 40rpx;
}
.image2{
margin-left: 10%;
}
/*進(jìn)度條長度 */
.slid{
flex: 1;
position: relative;
}
.slid view{
display: flex;
justify-content: space-between;
}
.slid view>text:nth-child(1){
color: #4c9dee;
margin-left:6rpx;
}
.slid view>text:nth-child(2){
margin-right:6rpx;
}
slider{
width: 520rpx;
margin: 0;
margin-left: 35rpx;
}
/*橫向布局 */
.times{
width: 100rpx;
text-align: center;
display: inline-block;
font-size: 24rpx;
color:#999999;
margin-top: 5rpx;
}
.title view{
text-indent: 2em;
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript中的this基本問題實(shí)例小結(jié)
這篇文章主要介紹了JavaScript中的this基本問題,結(jié)合實(shí)例形式總結(jié)分析了JavaScript中this的功能、常見用法與操作注意事項(xiàng),需要的朋友可以參考下2020-03-03
js實(shí)現(xiàn)綠白相間豎向網(wǎng)頁百葉窗動(dòng)畫切換效果
這篇文章主要介紹了js實(shí)現(xiàn)綠白相間豎向網(wǎng)頁百葉窗動(dòng)畫切換效果,實(shí)例分析了javascript實(shí)現(xiàn)百葉窗動(dòng)畫效果的技巧,需要的朋友可以參考下2015-03-03
微信小程序報(bào)錯(cuò): thirdScriptError的錯(cuò)誤問題
這篇文章主要介紹了微信小程序報(bào)錯(cuò): thirdScriptError,本文給大家分享解決方法,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-06-06
JavaScript對(duì)象轉(zhuǎn)數(shù)組的3種簡(jiǎn)單方法小結(jié)
在JavaScript中可以通過Object.keys()結(jié)合Array.map()、Object.entries()或Object.values()等方法將對(duì)象轉(zhuǎn)換為數(shù)組,這些方法各有優(yōu)勢(shì),可根據(jù)需要靈活選擇,需要的朋友可以參考下2024-09-09

