From eeacefc88fdadd6fdcec46f5f42f979b78fd89da Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Thu, 7 Nov 2019 18:59:54 +0800 Subject: [PATCH] fix wx one shot audio doesn't restart (#5699) --- cocos/audio/assets/player-dom.ts | 2 ++ cocos/audio/assets/player-web.ts | 8 +++++--- cocos/audio/assets/player-wx.ts | 7 +++---- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cocos/audio/assets/player-dom.ts b/cocos/audio/assets/player-dom.ts index 111a6394f6..a8dc1007ef 100644 --- a/cocos/audio/assets/player-dom.ts +++ b/cocos/audio/assets/player-dom.ts @@ -69,6 +69,7 @@ export class AudioPlayerDOM extends AudioPlayer { const promise = this._audio.play(); if (!promise) { // Chrome50/Firefox53 below // delay eval here to yield uniform behavior with other platforms + this._state = PlayingState.PLAYING; cc.director.once(cc.Director.EVENT_AFTER_UPDATE, this._post_gesture); return; } @@ -98,6 +99,7 @@ export class AudioPlayerDOM extends AudioPlayer { const promise = this._audio.play(); if (!promise) { // delay eval here to yield uniform behavior with other platforms + this._state = PlayingState.PLAYING; cc.director.once(cc.Director.EVENT_AFTER_UPDATE, this._post_play); return; } diff --git a/cocos/audio/assets/player-web.ts b/cocos/audio/assets/player-web.ts index 6e3c605313..c177865c05 100644 --- a/cocos/audio/assets/player-web.ts +++ b/cocos/audio/assets/player-web.ts @@ -70,11 +70,13 @@ export class AudioPlayerWeb extends AudioPlayer { this._sourceNode.buffer = this._audio; this._sourceNode.loop = this._loop; this._sourceNode.connect(this._gainNode); - this._sourceNode.start(0, this._offset); - this._state = PlayingState.PLAYING; this._startTime = this._context.currentTime; // delay eval here to yield uniform behavior with other platforms - cc.director.once(cc.Director.EVENT_AFTER_UPDATE, () => { this._eventTarget.emit('started'); }); + cc.director.once(cc.Director.EVENT_AFTER_UPDATE, () => { + this._sourceNode.start(0, this._offset); + this._eventTarget.emit('started'); + this._state = PlayingState.PLAYING; + }); /* still not supported by all platforms * this._sourceNode.onended = this._on_ended; /* doing it manually for now */ diff --git a/cocos/audio/assets/player-wx.ts b/cocos/audio/assets/player-wx.ts index 3bd5d297e3..dde598e960 100644 --- a/cocos/audio/assets/player-wx.ts +++ b/cocos/audio/assets/player-wx.ts @@ -108,11 +108,10 @@ export class AudioPlayerWX extends AudioPlayer { same time so here we fall back to re-start style approach */ if (!this._audio) { return; } this._offset = 0; - this._audio.seek(0); - this._audio.volume = volume; - if (this._oneShoting) { return; } - this._audio.loop = false; this._oneShoting = true; + this._audio.stop(); + this._audio.loop = false; + this._audio.volume = volume; this._audio.play(); }