From 0cd08e84417bce7cbf37f0a5f8914d71040d63b3 Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 19 Dec 2019 15:02:28 +0800 Subject: [PATCH] fix web audio stop before play (#5927) --- cocos/audio/assets/player-web.ts | 119 +++++++++++-------- cocos/core/director.ts | 10 +- cocos/core/renderer/models/skinning-model.ts | 2 +- 3 files changed, 77 insertions(+), 54 deletions(-) diff --git a/cocos/audio/assets/player-web.ts b/cocos/audio/assets/player-web.ts index 2f2bfd30cd..d43bf3182a 100644 --- a/cocos/audio/assets/player-web.ts +++ b/cocos/audio/assets/player-web.ts @@ -44,9 +44,11 @@ export class AudioPlayerWeb extends AudioPlayer { private _context: AudioContext; private _sourceNode: AudioBufferSourceNode; private _gainNode: GainNode; - private _on_ended: () => void; - private _do_play: () => void; - private _on_gesture: () => void; + private _startInvoked = false; + + private _onEndedCB: () => void; + private _onGestureCB: () => void; + private _onGestureProceedCB: () => void; constructor (info: IAudioInfo) { super(info); @@ -57,62 +59,26 @@ export class AudioPlayerWeb extends AudioPlayer { this._gainNode = this._context.createGain(); this._gainNode.connect(this._context.destination); - this._on_ended = () => { - this._offset = 0; - this._startTime = this._context.currentTime; - if (this._sourceNode.loop) { return; } - this._eventTarget.emit('ended'); - this._state = PlayingState.STOPPED; - }; + this._onEndedCB = this._onEnded.bind(this); + this._onGestureCB = this._onGesture.bind(this); + this._onGestureProceedCB = this._onGestureProceed.bind(this); - this._do_play = () => { - this._state = PlayingState.PLAYING; - this._sourceNode = this._context.createBufferSource(); - this._sourceNode.buffer = this._audio; - this._sourceNode.loop = this._loop; - this._sourceNode.connect(this._gainNode); - this._startTime = this._context.currentTime; - // delay eval here to yield uniform behavior with other platforms - cc.director.once(cc.Director.EVENT_AFTER_UPDATE, () => { - this._sourceNode.start(0, this._offset); - this._eventTarget.emit('started'); - }); - /* still not supported by all platforms * - this._sourceNode.onended = this._on_ended; - /* doing it manually for now */ - clearInterval(this._currentTimer); - this._currentTimer = window.setInterval(() => { - this._on_ended(); - clearInterval(this._currentTimer); - if (this._sourceNode.loop) { - this._currentTimer = window.setInterval(this._on_ended, this._audio.duration * 1000); - } - }, (this._audio.duration - this._offset) * 1000); - }; - - this._on_gesture = () => { - this._context.resume().then(() => { - if (this._interrupted) { this._do_play(); this._interrupted = false; } - cc.game.canvas.removeEventListener('touchend', this._on_gesture); - cc.game.canvas.removeEventListener('mouseup', this._on_gesture); - }); - }; // Chrome41/Firefox40 below don't have resume if (this._context.state !== 'running' && this._context.resume) { - cc.game.canvas.addEventListener('touchend', this._on_gesture); - cc.game.canvas.addEventListener('mouseup', this._on_gesture); + cc.game.canvas.addEventListener('touchend', this._onGestureCB); + cc.game.canvas.addEventListener('mouseup', this._onGestureCB); } } public play () { if (!this._audio || this._state === PlayingState.PLAYING) { return; } if (this._blocking || this._context.state !== 'running') { this._interrupted = true; return; } - this._do_play(); + this._doPlay(); } public pause () { if (this._state !== PlayingState.PLAYING) { return; } - this._sourceNode.stop(); + this._doStop(); this._offset += this._context.currentTime - this._startTime; this._state = PlayingState.STOPPED; clearInterval(this._currentTimer); @@ -121,7 +87,7 @@ export class AudioPlayerWeb extends AudioPlayer { public stop () { this._offset = 0; if (this._state !== PlayingState.PLAYING) { return; } - this._sourceNode.stop(); + this._doStop(); this._state = PlayingState.STOPPED; clearInterval(this._currentTimer); } @@ -143,7 +109,7 @@ export class AudioPlayerWeb extends AudioPlayer { // the serialized duration is not accurate, use the actual duration first this._offset = clamp(val, 0, this._audio && this._audio.duration || this._duration); if (this._state !== PlayingState.PLAYING) { return; } - this._sourceNode.stop(); this._do_play(); + this._doStop(); this._doPlay(); } public getCurrentTime () { @@ -174,4 +140,61 @@ export class AudioPlayerWeb extends AudioPlayer { } public destroy () { super.destroy(); } + + private _doPlay () { + this._state = PlayingState.PLAYING; + this._sourceNode = this._context.createBufferSource(); + this._sourceNode.buffer = this._audio; + this._sourceNode.loop = this._loop; + this._sourceNode.connect(this._gainNode); + this._startTime = this._context.currentTime; + this._startInvoked = false; + // delay eval here to yield uniform behavior with other platforms + cc.director.once(cc.Director.EVENT_AFTER_UPDATE, this._playAndEmit, this); + /* still not supported by all platforms * + this._sourceNode.onended = this._onEnded; + /* doing it manually for now */ + clearInterval(this._currentTimer); + this._currentTimer = window.setInterval(() => { + this._onEnded(); + clearInterval(this._currentTimer); + if (this._sourceNode.loop) { + this._currentTimer = window.setInterval(this._onEndedCB, this._audio.duration * 1000); + } + }, (this._audio.duration - this._offset) * 1000); + } + + private _doStop () { + // stop can only be called after play + if (this._startInvoked) { this._sourceNode.stop(); } + else { cc.director.off(cc.Director.EVENT_AFTER_UPDATE, this._playAndEmit, this); } + } + + private _playAndEmit () { + this._sourceNode.start(0, this._offset); + this._eventTarget.emit('started'); + this._startInvoked = true; + } + + private _onEnded () { + this._offset = 0; + this._startTime = this._context.currentTime; + if (this._sourceNode.loop) { return; } + this._eventTarget.emit('ended'); + this._state = PlayingState.STOPPED; + } + + private _onGestureProceed () { + if (this._interrupted) { this._doPlay(); this._interrupted = false; } + cc.game.canvas.removeEventListener('touchend', this._onGestureCB); + cc.game.canvas.removeEventListener('mouseup', this._onGestureCB); + } + + private _onGesture () { + if (this._context.state !== 'running') { + this._context.resume().then(this._onGestureProceedCB); + } else { + this._onGestureProceed(); + } + } } diff --git a/cocos/core/director.ts b/cocos/core/director.ts index 072e3b2151..8b5ee2205c 100644 --- a/cocos/core/director.ts +++ b/cocos/core/director.ts @@ -31,11 +31,13 @@ * @category core */ +import { SceneAsset } from './assets'; import System from './components/system'; import { CCObject } from './data/object'; import { EventTarget } from './event/event-target'; import { Game } from './game'; import { autoRelease } from './load-pipeline/auto-release-utils'; +import { Color, size, v2, Vec2 } from './math'; import eventManager from './platform/event-manager/event-manager'; import { Root } from './root'; import { Node, Scene } from './scene-graph'; @@ -43,8 +45,6 @@ import ComponentScheduler from './scene-graph/component-scheduler'; import NodeActivator from './scene-graph/node-activator'; import { Scheduler } from './scheduler'; import { js } from './utils'; -import { Color, Vec2, v2, size } from './math'; -import { SceneAsset } from './assets'; // const ComponentScheduler = require('./component-scheduler'); // const NodeActivator = require('./node-activator'); @@ -744,11 +744,11 @@ export class Director extends EventTarget { * @param uuid 场景资源的 uuid。 * @param doNotRun 仅加载和初始化场景,但并不运行。此参数仅在编辑器环境中生效。 */ - public _loadSceneByUuid(uuid: string, doNotRun?: boolean): void; + public _loadSceneByUuid (uuid: string, doNotRun?: boolean): void; - public _loadSceneByUuid(uuid: string, onLaunched?: Director.OnSceneLaunched, doNotRun?: boolean): void; + public _loadSceneByUuid (uuid: string, onLaunched?: Director.OnSceneLaunched, doNotRun?: boolean): void; - public _loadSceneByUuid(uuid: string, onLaunched?: Director.OnSceneLaunched, onUnloaded?: Director.OnUnload, doNotRun?: boolean): void; + public _loadSceneByUuid (uuid: string, onLaunched?: Director.OnSceneLaunched, onUnloaded?: Director.OnUnload, doNotRun?: boolean): void; public _loadSceneByUuid ( uuid: string, diff --git a/cocos/core/renderer/models/skinning-model.ts b/cocos/core/renderer/models/skinning-model.ts index f758a09d85..e1def86c39 100644 --- a/cocos/core/renderer/models/skinning-model.ts +++ b/cocos/core/renderer/models/skinning-model.ts @@ -100,7 +100,7 @@ export class SkinningModel extends Model { const { animInfo, boundsInfo } = this._jointsMedium; const skelBound = boundsInfo![animInfo!.data[1]]; const node = this._transform; - if (this._worldBounds) { + if (this._worldBounds && skelBound) { // @ts-ignore TS2339 skelBound.transform(node._mat, node._pos, node._rot, node._scale, this._worldBounds); }