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 memory leak caused by excessive simultaneous playback of sound effects on the mini game platform #15541

Merged
merged 3 commits into from
Jun 27, 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
18 changes: 12 additions & 6 deletions pal/audio/minigame/player-minigame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,18 @@ export class OneShotAudioMinigame {
nativeAudio.onPlay(() => {
this._onPlayCb?.();
});
nativeAudio.onEnded(() => {
this._onEndCb?.();
nativeAudio.destroy();
// NOTE: Type 'null' is not assignable to type 'InnerAudioContext'.
this._innerAudioContext = null as any;
});
const endCallback = (): void => {
if (this._innerAudioContext) {
this._onEndCb?.();
nativeAudio.destroy();
// NOTE: Type 'null' is not assignable to type 'InnerAudioContext'.
this._innerAudioContext = null as any;
}
};
nativeAudio.onEnded(endCallback);
nativeAudio.onStop(endCallback);//OneShotAudio can not be reused.
}

public play (): void {
this._innerAudioContext.play();
}
Expand Down Expand Up @@ -240,6 +245,7 @@ export class AudioPlayerMinigame implements OperationQueueable {
function fail (err): void {
clearEvent();
clearTimeout(timer);
// eslint-disable-next-line no-console
console.error('failed to load innerAudioContext');
reject(new Error(err));
}
Expand Down