diff --git a/src/core/service/api/context/audio.js b/src/core/service/api/context/audio.js index 78aedc7c919cba6aeb4642e9f857a2c248bae882..526dd6b394371b00ec989f7acc1a0c9108a1e0a1 100644 --- a/src/core/service/api/context/audio.js +++ b/src/core/service/api/context/audio.js @@ -9,14 +9,14 @@ const eventNames = [ 'pause', 'stop', 'ended', - 'timeupdate', + 'timeUpdate', 'error', 'waiting', 'seeking', 'seeked' ] -const props = [ +const props = [ { name: 'src', cache: true @@ -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) @@ -161,4 +180,4 @@ export function createInnerAudioContext () { const innerAudioContext = new InnerAudioContext(audioId) innerAudioContexts[audioId] = innerAudioContext return innerAudioContext -} +}