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

fix: app-v3 InnerAudioContext.seek InnerAudioContext.onTimeUpdate

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