Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue where the pause and resume function of the mini game platform is not working properly when switching between the front and back end. #15811

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions pal/audio/minigame/player-minigame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import { AudioEvent, AudioPCMDataView, AudioState, AudioType } from '../type';
import { clamp, clamp01 } from '../../../cocos/core';
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
import { OS } from '../../system-info/enum-type';
import { Game, game } from '../../../cocos/game';

export class OneShotAudioMinigame {
private _innerAudioContext: InnerAudioContext;
Expand Down Expand Up @@ -58,6 +57,8 @@ export class OneShotAudioMinigame {
});
const endCallback = (): void => {
if (this._innerAudioContext) {
systemInfo.off('hide', this._onInterruptedBegin, this);
systemInfo.off('show', this._onInterruptedEnd, this);
this._onEndCb?.();
nativeAudio.destroy();
// NOTE: Type 'null' is not assignable to type 'InnerAudioContext'.
Expand All @@ -66,6 +67,18 @@ export class OneShotAudioMinigame {
};
nativeAudio.onEnded(endCallback);
nativeAudio.onStop(endCallback);//OneShotAudio can not be reused.

// event
systemInfo.on('hide', this._onInterruptedBegin, this);
systemInfo.on('show', this._onInterruptedEnd, this);
}

private _onInterruptedBegin (): void {
this._innerAudioContext.pause();
}

private _onInterruptedEnd (): void {
this._innerAudioContext.play();
}

public play (): void {
Expand Down Expand Up @@ -109,8 +122,8 @@ export class AudioPlayerMinigame implements OperationQueueable {
this._eventTarget = new EventTarget();

// event
game.on(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
game.on(Game.EVENT_RESUME, this._onInterruptedEnd, this);
systemInfo.on('hide', this._onInterruptedBegin, this);
systemInfo.on('show', this._onInterruptedEnd, this);
const eventTarget = this._eventTarget;
this._onPlay = (): void => {
this._state = AudioState.PLAYING;
Expand Down Expand Up @@ -171,8 +184,8 @@ export class AudioPlayerMinigame implements OperationQueueable {
innerAudioContext.onEnded(this._onEnded);
}
destroy (): void {
game.off(Game.EVENT_PAUSE, this._onInterruptedBegin, this);
game.off(Game.EVENT_RESUME, this._onInterruptedEnd, this);
systemInfo.off('hide', this._onInterruptedBegin, this);
systemInfo.off('show', this._onInterruptedEnd, this);
if (this._innerAudioContext) {
['Play', 'Pause', 'Stop', 'Seeked', 'Ended'].forEach((event) => {
this._offEvent(event);
Expand All @@ -196,7 +209,7 @@ export class AudioPlayerMinigame implements OperationQueueable {
private _onInterruptedEnd (): void {
// We don't know whether onShow or resolve callback in pause promise is called at first.
if (!this._readyToHandleOnShow) {
this._eventTarget.once(AudioEvent.INTERRUPTION_BEGIN, this._onInterruptedEnd, this);
this._eventTarget.once(AudioEvent.INTERRUPTION_END, this._onInterruptedEnd, this);
return;
}
if (this._state === AudioState.INTERRUPTED) {
Expand Down
Loading