字節(jié)跳動小程序開發(fā)API getRecorderManager

2019-08-20 17:20 更新

創(chuàng)建innerAudioContext實(shí)例,通過它能夠操作音頻播放。

innerAudioContext的屬性列表


名稱 數(shù)據(jù)類型 屬性 默認(rèn)值 描述
src string N/A 音頻源地址
startTime number 0 開始播放的位置,單位s
autoplay boolean false 是否自動播放
loop boolean false 是否自動循環(huán)
obeyMuteSwitch boolean true 是否遵循系統(tǒng)靜音開關(guān)
duration number readonly 當(dāng)前音頻總時長,單位s
currentTime number readonly 當(dāng)前音頻進(jìn)度,單位s
paused boolean readonly 當(dāng)前音頻是否處于暫停狀態(tài)
buffered number readonly 當(dāng)前音頻已緩沖部分,單位s
volume number readonly 當(dāng)前音量

innerAudioContext的方法列表


play()

播放

pause()

暫停播放

stop()

停止播放

seek(position)

跳轉(zhuǎn)到position指定的位置播放,數(shù)據(jù)格式為number,單位為s

destory()

銷毀當(dāng)前innerAudioContext實(shí)例

onCanplay(() => {})

音頻進(jìn)入可以播放狀態(tài),但不保證后面可以流暢播放

offCanplay(() => {})

取消監(jiān)聽 Canplay 事件

onPlay(() => {})

音頻播放事件

offPlay(() => {})

取消監(jiān)聽 Play 事件

onPause(() => {})

音頻暫停事件

offPause(() => {})

取消監(jiān)聽 Pause 事件

onStop(() => {})

音頻停止事件

offStop(() => {})

取消監(jiān)聽 Stop 事件

onEnded(() => {})

音頻自然播放結(jié)束事件

offEnded(() => {})

取消監(jiān)聽 Ended 事件

onTimeUpdate(() => {})

音頻播放進(jìn)度更新事件

offTimeUpdate(() => {})

取消監(jiān)聽 TimeUpdate 事件

onError((error) => {})

音頻播放錯誤事件

offError(() => {})

取消監(jiān)聽 Error 事件

onWaiting(() => {})

音頻加載中事件,當(dāng)音頻因?yàn)閿?shù)據(jù)不足,需要停下來加載時會觸發(fā)

offWaiting(() => {})

取消監(jiān)聽 Waiting 事件

onSeeking(() => {})

音頻進(jìn)行 seek 操作事件

offSeeking(() => {})

取消監(jiān)聽 Seeking 事件

onSeeked(() => {})

音頻完成 seek 操作事件

offSeeked(() => {})

取消監(jiān)聽 Seeked 事件

代碼示例


const innerAudioContext = tt.createInnerAudioContext();
innerAudioContext.autoplay = true;
innerAudioContext.src = 'https://someaudiourl';
innerAudioContext.onPlay(() => {
    console.log('開始播放');
});
innerAudioContext.onError((error) => {
    console.log(error)
});
innerAudioContext.onTimeUpdate((res) => {
    this.setData({
        progress: innerAudioContext.currentTime / innerAudioContext.duration
    });
})

已知問題



以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號