微信小程序音頻播放之音樂播放器

show.gif


雖然界面很簡(jiǎn)單,但是一個(gè)音頻播放器該有的功能大部分都有了(沒有歌詞顯示功能).

主要實(shí)現(xiàn)的功能有:

1.實(shí)現(xiàn)音頻播放,暫停;

2.實(shí)現(xiàn)拖拽進(jìn)度條,快進(jìn)音頻進(jìn)度;

3.實(shí)現(xiàn)上一首,下一首,列表循環(huán)播放;

4.實(shí)現(xiàn)關(guān)閉小程序,也可在后臺(tái)播放,正式版需要通過審核,開發(fā)版本可正常測(cè)試;

一丶index.js

數(shù)據(jù)初始化

/**

? * 頁面的初始數(shù)據(jù)

? */data:{playStatus:true,audioIndex:0,progress:0,duration:0,audioList:[],showList:true},/**

? * 生命周期函數(shù)--監(jiān)聽頁面加載

? */onLoad:function(options){this.setData({audioList:data})this.playMusic();},

playMusic切換播放歌曲的方法.

playMusic:function(){letaudio=this.data.audioList[this.data.audioIndex];letmanager=wx.getBackgroundAudioManager();manager.title=audio.name||"音頻標(biāo)題";manager.epname=audio.epname||"專輯名稱";manager.singer=audio.author||"歌手名";manager.coverImgUrl=audio.poster;// 設(shè)置了 src 之后會(huì)自動(dòng)播放manager.src=audio.src;manager.currentTime=0;letthat=this;manager.onPlay(function(){console.log("======onPlay======");that.setData({playStatus:true})that.countTimeDown(that,manager);});manager.onPause(function(){that.setData({playStatus:false})console.log("======onPause======");});manager.onEnded(function(){console.log("======onEnded======");that.setData({playStatus:false})setTimeout(function(){that.nextMusic();},1500);});},

countTimeDown循環(huán)計(jì)時(shí),進(jìn)度展示

//循環(huán)計(jì)時(shí)

countTimeDown:function(that,manager,cancel){if(that.data.playStatus){setTimeout(function(){if(that.data.playStatus){// console.log("duration: " + manager.duration);// console.log(manager.currentTime);that.setData({progress:Math.ceil(manager.currentTime),progressText:that.formatTime(Math.ceil(manager.currentTime)),duration:Math.ceil(manager.duration),durationText:that.formatTime(Math.ceil(manager.duration))})that.countTimeDown(that,manager);}},1000)}},

sliderChangeslider的拖拽事件

//拖動(dòng)事件

sliderChange:function(e){letmanager=wx.getBackgroundAudioManager();manager.pause();manager.seek(e.detail.value);this.setData({progressText:this.formatTime(e.detail.value)})setTimeout(function(){manager.play();},1000);},

lastMusic上一首

//上一首

lastMusic:function(){let audioIndex=this.data.audioIndex>0?this.data.audioIndex-1:this.data.audioList.length-1;this.setData({audioIndex:audioIndex,playStatus:false,progress:0,progressText:"00:00",durationText:"00:00"})setTimeout(function(){this.playMusic();}.bind(this),1000);},

playOrpause中間的按鈕,播放/暫停切換

//播放按鈕

playOrpause:function(){letmanager=wx.getBackgroundAudioManager();if(this.data.playStatus){manager.pause();}else{manager.play();}},

nextMusic下一首

//下一首

nextMusic:function(){let audioIndex=this.data.audioIndex<this.data.audioList.length-1?this.data.audioIndex+1:0;this.setData({audioIndex:audioIndex,playStatus:false,progress:0,progressText:"00:00",durationText:"00:00"})setTimeout(function(){this.playMusic();}.bind(this),1000);},

listClick列表點(diǎn)擊事件

//列表點(diǎn)擊事件

listClick:function(e){let pos=e.currentTarget.dataset.pos;if(pos!=this.data.audioIndex){this.setData({audioIndex:pos,showList:false})this.playMusic();}else{this.setData({showList:false})}},

界面切換,時(shí)長(zhǎng)格式化

//界面切換

pageChange:function(){this.setData({showList:true})},

//格式化時(shí)長(zhǎng)

formatTime:function(s){lett='';s=Math.floor(s);if(s>-1){letmin=Math.floor(s/60)%60;letsec=s%60;if(min<10){t+="0";}t+=min+":";if(sec<10){t+="0";}t+=sec;}returnt;},

二丶index.wxml

<!--index.wxml-->

<viewclass="container"><viewwx:if="{{showList}}"class="list"><viewwx:for="{{audioList}}"class='item {{audioIndex==index?"active":""}}'bindtap='listClick'data-pos='{{index}}'><view>{{item.name}}</view><text>{{item.author}}</text></view></view><viewwx:elseclass='background'><viewclass='info'><view>{{audioList[audioIndex].name||""}}</view><view>{{audioList[audioIndex].author||""}}</view></view><imageclass='list'bindtap='pageChange'src='/images/list.png'></image><imageclass='poster {{playStatus?"rotate":"rotate-paused"}}'mode="scaleToFill"src='{{audioList[audioIndex].poster}}'></image><viewclass='progress'><text>{{progressText}}</text><sliderclass='bar'bindchange="sliderChange"bindchanging="sliderChanging"value="{{progress}}"step="1"min='0'max='{{duration}}'activeColor="#1aad19"block-size="12"block-color="#1aad19"/><text>{{durationText}}</text></view><viewclass='buttons'><imageclass='button'bindtap='lastMusic'src='/images/last.png'></image><imageclass='button'bindtap='playOrpause'src='{{playStatus?"/images/pause.png":"/images/play.png"}}'></image><imageclass='button'bindtap='nextMusic'src='/images/next.png'></image></view></view></view>

三丶index.wxss

/**index.wxss**/

.item{border:1rpx solid #eee;padding:10rpx;font-size:11pt;}.active{background:#a51515;color:#fff;}.background{position:fixed;left:0;top:0;right:0;bottom:0;text-align:center;background:#f5f5f5;}.background .info{position:fixed;top:140rpx;left:0;right:0;font-size:12pt;color:#353535;}.background .list{position:fixed;right:40rpx;top:40rpx;width:60rpx;height:60rpx;}.background .poster{width:150rpx;height:150rpx;border-radius:50%;margin-top:400rpx;}.rotate{animation:rotate 10s linear infinite;}.rotate-paused{animation:rotate 10s linear infinite;animation-play-state:paused;}@keyframesrotate{0%{transform:rotate(0deg);}50%{transform:rotate(180deg);}100%{transform:rotate(360deg);}}.progress{position:fixed;bottom:90rpx;left:50rpx;right:50rpx;display:flex;align-items:center;font-size:10pt;color:rgb(87,49,49);text-align:center;}.progress .bar{flex:1;}.progress text{flex-basis:90rpx;}.buttons{position:fixed;bottom:20rpx;left:50rpx;right:50rpx;display:flex;justify-content:space-around;align-items:center;}.buttons .button{width:70rpx;height:70rpx;}

四丶要實(shí)現(xiàn)關(guān)閉小程序后,依然后臺(tái)播放,微信頂部懸浮展示,需要再app.json配置requiredBackgroundModes屬性

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末鸣戴,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子粘拾,更是在濱河造成了極大的恐慌窄锅,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缰雇,死亡現(xiàn)場(chǎng)離奇詭異酬滤,居然都是意外死亡签餐,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門盯串,熙熙樓的掌柜王于貴愁眉苦臉地迎上來氯檐,“玉大人,你說我怎么就攤上這事体捏」谏悖” “怎么了?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵几缭,是天一觀的道長(zhǎng)河泳。 經(jīng)常有香客問我,道長(zhǎng)年栓,這世上最難降的妖魔是什么拆挥? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮某抓,結(jié)果婚禮上纸兔,老公的妹妹穿的比我還像新娘。我一直安慰自己否副,他們只是感情好汉矿,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著备禀,像睡著了一般洲拇。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上曲尸,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天赋续,我揣著相機(jī)與錄音,去河邊找鬼另患。 笑死纽乱,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的柴淘。 我是一名探鬼主播迫淹,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼为严!你這毒婦竟也來了敛熬?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤第股,失蹤者是張志新(化名)和其女友劉穎应民,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡诲锹,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年繁仁,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片归园。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡黄虱,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出庸诱,到底是詐尸還是另有隱情捻浦,我是刑警寧澤,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布桥爽,位于F島的核電站朱灿,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏钠四。R本人自食惡果不足惜盗扒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望缀去。 院中可真熱鬧侣灶,春花似錦、人聲如沸朵耕。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽阎曹。三九已至,卻和暖如春煞檩,著一層夾襖步出監(jiān)牢的瞬間处嫌,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工斟湃, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留熏迹,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓凝赛,卻偏偏與公主長(zhǎng)得像注暗,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子墓猎,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354