网友真实露脸自拍10p,成人国产精品秘?久久久按摩,国产精品久久久久久无码不卡,成人免费区一区二区三区

小程序模板網(wǎng)

微信小程序媒體組件(一)audio

發(fā)布時(shí)間:2018-05-02 15:34 所屬欄目:小程序開(kāi)發(fā)教程

今天記錄一下audio的基本使用,首先看下效果圖。(聲音請(qǐng)腦補(bǔ)一下~)

 

1.audio屬性(自行去微信官方文檔中了解)

2.一起看一下 audio.wxml


  
<audio id="myAudio" poster="{{poster}}" name="{{name}}" author="{{author}}" src="{{src}}"  bindplay="bindPlay" bindpause='bindPause' bindended='bindEnd' binderror='bindError' bindtimeupdate='bindTimeUpdate' controls loop>audio>  
<button type="primary" class="audioButton" bindtap="audioPlay">播放button>  
<button type="primary" class="audioButton" bindtap="audioPause">暫停button>  
<button type="primary" class="audioButton" bindtap="audio14">設(shè)置當(dāng)前播放時(shí)間為14秒button>  
<button type="primary" class="audioButton" bindtap="audioStart">回到開(kāi)頭button>  

①id為audio組件的唯一標(biāo)識(shí),在js中通過(guò)該id獲取audio上下文context


this.audioCtx = wx.createAudioContext('myAudio')  

②poster、name、author、src為audio資源,詳見(jiàn)屬性表
③bindplay、bindpause、bindended監(jiān)聽(tīng)audio的播放、暫停和結(jié)束,在js中進(jìn)行實(shí)現(xiàn)


bindPlay: function () {//監(jiān)聽(tīng)音樂(lè)開(kāi)始/繼續(xù)播放  
    console.log("<" + this.data.name + '>繼續(xù)播放')  
  },  
  bindPause: function () {//監(jiān)聽(tīng)音樂(lè)暫停  
    console.log("<" + this.data.name + '>暫停播放')  
  },  
  bindEnd: function () {//監(jiān)聽(tīng)音樂(lè)播放結(jié)束  
    console.log("<" + this.data.name + '>結(jié)束播放')  
  },  

④binderror監(jiān)聽(tīng)audio的錯(cuò)誤


bindError: function (error) {//監(jiān)聽(tīng)錯(cuò)誤,錯(cuò)誤信息error.detail.errMsg  
    console.log(error.detail.errMsg)  
  },

其中errMsg有四種
返回錯(cuò)誤碼 描述
MEDIAERRABORTED 獲取資源被用戶(hù)禁止
MEDIAERRNETWORD 網(wǎng)絡(luò)錯(cuò)誤
MEDIAERRDECODE 解碼錯(cuò)誤
MEDIAERRSRCNOTSUPPOERTED 不合適資源
我將模擬器設(shè)置為offline觸發(fā)了“MEDIAERRSRCNOTSUPPOERTED”,暫未觸發(fā)成功過(guò)其他錯(cuò)誤。
⑤bindtimeupdate監(jiān)聽(tīng)播放時(shí)間的變化,單位為s


bindTimeUpdate:function(timeupdate){  
    console.log("當(dāng)前播放位置:"+timeupdate.detail.currentTime+"s")  
  },  

⑥loop為true則音樂(lè)自動(dòng)循環(huán)播放
⑦controls控制播放窗口(紅色框內(nèi)部分)的可見(jiàn)性↓

2.完整的js代碼↓


Page({  
  onReady: function (e) {  
    // 使用 wx.createAudioContext 獲取 audio 上下文 context  
    this.audioCtx = wx.createAudioContext('myAudio')  
  },  
  data: {  
    // poster: '../../resources/image/隔壁團(tuán).jpg',  
    // name: '夏天海邊',  
    // author: '隔壁團(tuán)樂(lè)隊(duì)',  
    // src: '../../resources/audio/夏天海邊.mp3',  
    src: 'http://ws.stream.qqmusic.qq.com/M500001VfvsJ21xFqb.mp3?guid=ffffffff82def4af4b12b3cd9337d5e7&uin=346897220&vkey=6292F51E1E384E06DCBDC9AB7C49FD713D632D313AC4858BACB8DDD29067D3C601481D36E62053BF8DFEAF74C0A5CCFADD6471160CAF3E6A&fromtag=46',  
    poster: 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000',  
    name: '此時(shí)此刻',  
    author: '許巍'  
  },  
  bindPlay: function () {//監(jiān)聽(tīng)音樂(lè)開(kāi)始/繼續(xù)播放  
    console.log("<" + this.data.name + '>繼續(xù)播放')  
  },  
  bindPause: function () {//監(jiān)聽(tīng)音樂(lè)暫停  
    console.log("<" + this.data.name + '>暫停播放')  
  },  
  bindEnd: function () {//監(jiān)聽(tīng)音樂(lè)播放結(jié)束  
    console.log("<" + this.data.name + '>結(jié)束播放')  
  },  
  bindError: function (error) {//監(jiān)聽(tīng)錯(cuò)誤,錯(cuò)誤信息error.detail.errMsg  
    console.log("=================================")  
    console.log(error.detail.errMsg)  
    console.log("=================================")  
  },  
  bindTimeUpdate:function(timeupdate){  
    console.log("當(dāng)前播放位置:"+timeupdate.detail.currentTime+"s")  
  },  
  audioPlay: function () {//點(diǎn)擊“播放”觸發(fā)  
    this.audioCtx.play()  
  },  
  audioPause: function () {//點(diǎn)擊“暫停”觸發(fā)  
    this.audioCtx.pause()  
  },  
  audio14: function () {//設(shè)置當(dāng)前播放時(shí)間為14秒,seek單位為s  
    this.audioCtx.seek(14)  
  },  
  audioStart: function () {  
    this.audioCtx.seek(0)  
  }  
})  

audioContext 對(duì)象的方法列表:
方法 參數(shù) 說(shuō)明
setSrc src 音頻的地址
play 無(wú) 播放
pause 無(wú) 暫停
seek position 跳轉(zhuǎn)到指定位置,單位 s

3.wxss樣式文件↓


/* pages/audio/audio.wxss */  
.audioButton{  
  margin-left: 20px;  
  margin-right: 20px;  
  margin-top: 20px;  
} 


4.上面給出的js源碼中使用了網(wǎng)絡(luò)資源,我們也可以使用本地資源。建立一個(gè)與pages同級(jí)的資源文件夾。目錄結(jié)構(gòu)如下↓(但是需要注意,微信小程序有大小限制。太大資源會(huì)無(wú)法在實(shí)體機(jī)上預(yù)覽)

使用本地資源后,其他功能都不受影響,但是seek(14)有bug,每次都是從頭開(kāi)始播放。不知道是不是小程序的bug難過(guò)。今天關(guān)于audio的部分就記錄到這里啦



易優(yōu)小程序(企業(yè)版)+靈活api+前后代碼開(kāi)源 碼云倉(cāng)庫(kù):starfork
本文地址:http://www.xiuhaier.com/wxmini/doc/course/24172.html 復(fù)制鏈接 如需定制請(qǐng)聯(lián)系易優(yōu)客服咨詢(xún):800182392 點(diǎn)擊咨詢(xún)
QQ在線咨詢(xún)
AI智能客服 ×