提交 19f264e8 编写于 作者: Q qiang

fix: app-v3 InnerAudioContext.seek InnerAudioContext.onTimeUpdate

上级 1d1bee09
......@@ -9,7 +9,7 @@ const eventNames = [
'pause',
'stop',
'ended',
'timeupdate',
'timeUpdate',
'error',
'waiting',
'seeking',
......@@ -69,7 +69,7 @@ class InnerAudioContext {
this._callbacks = {}
this._options = {}
eventNames.forEach(name => {
this._callbacks[name] = []
this._callbacks[name.toLowerCase()] = []
})
props.forEach(item => {
const name = item.name
......@@ -103,11 +103,12 @@ class InnerAudioContext {
this._operate('stop')
}
seek (position) {
this._operate('play', {
currentTime: position
this._operate('seek', {
currentTime: position * 1e3
})
}
destroy () {
clearInterval(audio.__timing)
invokeMethod('destroyAudioInstance', {
audioId: this.id
})
......@@ -123,6 +124,7 @@ class InnerAudioContext {
eventNames.forEach(item => {
const name = item[0].toUpperCase() + item.substr(1)
item = item.toLowerCase()
InnerAudioContext.prototype[`on${name}`] = function (callback) {
this._callbacks[item].push(callback)
}
......@@ -135,6 +137,17 @@ eventNames.forEach(item => {
}
})
function emit (audio, state, errMsg, errCode) {
audio._callbacks[state].forEach(callback => {
if (typeof callback === 'function') {
callback(state === 'error' ? {
errMsg,
errCode
} : {})
}
})
}
onMethod('onAudioStateChange', ({
state,
audioId,
......@@ -142,14 +155,20 @@ onMethod('onAudioStateChange', ({
errCode
}) => {
const audio = innerAudioContexts[audioId]
audio && audio._callbacks[state].forEach(callback => {
if (typeof callback === 'function') {
callback(state === 'error' ? {
errMsg,
errCode
} : {})
if (audio) {
emit(audio, state, errMsg, errCode)
if (state === 'play') {
const oldCurrentTime = audio.currentTime
audio.__timing = setInterval(() => {
const currentTime = audio.currentTime
if (currentTime !== oldCurrentTime) {
emit(audio, 'timeupdate')
}
}, 200)
} else if (state === 'pause' || state === 'stop' || state === 'error') {
clearInterval(audio.__timing)
}
}
})
})
const innerAudioContexts = Object.create(null)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册