A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.
TypeScript 2d-game-engine3d3d-game-engineandroidcppecsgame-editorgame-enginegamedeviostypescriptwebassemblywebglwebgpuwechat-minigame
fix(audio): preserve manual minigame pauses on device resume
yhhba5024b713cf
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 80d921ac8..1aafc9adb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -16,6 +16,8 @@ published separately; it ships inside the editor.### Fixed+- **Mini-game device resume preserves manual audio pauses.** The backend resumes only live contexts that were playing when device suspension began. Repeated suspension retains the original resume set, and stopped, ended, or disposed contexts are never restarted.+- **Audio plugin cleanup belongs to each App.** Shared plugin instances release the quitting App's voices, backend, and memory/despawn subscriptions without stopping another App. Released systems stay inactive, and repeated preview shutdowns release every backend once.- **Retired WebAudio handles cannot modify reused pool nodes.** Gain and pan writes from stopped or completed handles are ignored, preserving the next voice's mix. Completion callbacks are checked against the currently playing source, so an old source cannot release the pool slot of a resumed voice.diff --git a/sdk/src/audio/MiniGameAudioBackend.ts b/sdk/src/audio/MiniGameAudioBackend.tsindex 1587f1943..2066f3244 100644--- a/sdk/src/audio/MiniGameAudioBackend.ts+++ b/sdk/src/audio/MiniGameAudioBackend.ts@@ -101,6 +101,7 @@ export class MiniGameAudioBackend implements PlatformAudioBackend {private readonly g_: MiniGameGlobal;private contexts_ = new Map<number, MiniGameInnerAudioContext>();+ private suspendedContexts_: Map<number, MiniGameInnerAudioContext> | null = null;private urlCache_ = new Map<number, string>();private nextId_ = 0;@@ -173,20 +174,29 @@ export class MiniGameAudioBackend implements PlatformAudioBackend {}suspend(): void {- for (const ctx of this.contexts_.values()) {+ if (this.suspendedContexts_) return;+ const suspended = new Map<number, MiniGameInnerAudioContext>();+ this.suspendedContexts_ = suspended;+ for (const [id, ctx] of this.contexts_) {+ if (ctx.paused) continue;+ suspended.set(id, ctx);ctx.pause();}}resume(): void {- for (const ctx of this.contexts_.values()) {- if (ctx.paused) {+ const suspended = this.suspendedContexts_;+ if (!suspended) return;+ this.suspendedContexts_ = null;+ for (const [id, ctx] of suspended) {+ if (this.contexts_.get(id) === ctx && ctx.paused) {ctx.play();}}}dispose(): void {+ this.suspendedContexts_ = null;for (const ctx of this.contexts_.values()) {ctx.destroy();}diff --git a/sdk/tests/minigame-audio-backend.test.ts b/sdk/tests/minigame-audio-backend.test.tsindex 52870e4af..ad6d46caf 100644--- a/sdk/tests/minigame-audio-backend.test.ts+++ b/sdk/tests/minigame-audio-backend.test.ts@@ -4,7 +4,7 @@ import { describe, it, expect, beforeEach, vi } from 'vitest';import { MiniGameAudioBackend } from '../src/audio/MiniGameAudioBackend';function createMockInnerAudioContext() {- return {+ const ctx = {src: '',startTime: 0,autoplay: false,@@ -14,7 +14,7 @@ function createMockInnerAudioContext() {playbackRate: 1.0,duration: 0,currentTime: 0,- paused: false,+ paused: true,play: vi.fn(),pause: vi.fn(),stop: vi.fn(),@@ -24,6 +24,11 @@ function createMockInnerAudioContext() {offEnded: vi.fn(),onError: vi.fn(),};+ ctx.play.mockImplementation(() => { ctx.paused = false; });+ ctx.pause.mockImplementation(() => { ctx.paused = true; });+ ctx.stop.mockImplementation(() => { ctx.paused = true; });+ ctx.destroy.mockImplementation(() => { ctx.paused = true; });+ return ctx;}describe('MiniGameAudioBackend', () => {@@ -96,6 +101,48 @@ describe('MiniGameAudioBackend', () => {});describe('suspend/resume', () => {+ it('resumes device-paused voices while preserving a manually paused voice', async () => {+ const buffer = await backend.loadBuffer('a.mp3');+ const manual = backend.play(buffer, {}), active = backend.play(buffer, {});+ manual.pause(); backend.suspend(); backend.resume();+ expect(manual.isPlaying).toBe(false); expect(active.isPlaying).toBe(true);+ });+ it('resume without a device suspension does not restart manually paused voices', async () => {+ const buffer = await backend.loadBuffer('a.mp3');+ const handle = backend.play(buffer, {}); handle.pause(); backend.resume();+ expect(handle.isPlaying).toBe(false);+ });+ it('repeated device suspension retains the original set of playing voices', async () => {+ const buffer = await backend.loadBuffer('a.mp3');+ const handle = backend.play(buffer, {});+ backend.suspend(); backend.suspend(); backend.resume();+ expect(handle.isPlaying).toBe(true);+ const ctx = (globalThis as any).wx.createInnerAudioContext.mock.results.at(-1).value;+ expect(ctx.pause).toHaveBeenCalledOnce(); expect(ctx.play).toHaveBeenCalledTimes(2);+ });+ it.each(['stop', 'end'])('a context retired by %s during suspension is never resumed', async kind => {+ const buffer = await backend.loadBuffer('a.mp3'), handle = backend.play(buffer, {});+ const ctx = (globalThis as any).wx.createInnerAudioContext.mock.results.at(-1).value;+ backend.suspend();+ if (kind === 'stop') handle.stop(); else ctx.onEnded.mock.calls[0][0]();+ backend.resume(); expect(ctx.play).toHaveBeenCalledOnce(); expect(ctx.destroy).toHaveBeenCalledOnce();+ });+ it('disposal discards the device resume set', async () => {+ const buffer = await backend.loadBuffer('a.mp3');+ backend.play(buffer, {}); backend.suspend(); backend.dispose(); backend.resume();+ const ctx = (globalThis as any).wx.createInnerAudioContext.mock.results.at(-1).value;+ expect(ctx.play).toHaveBeenCalledOnce(); expect(ctx.destroy).toHaveBeenCalledOnce();+ });+ it('repeated takeovers reuse live contexts and keep manual pauses intact', async () => {+ const buffer = await backend.loadBuffer('a.mp3');+ const manual = backend.play(buffer, {}), active = backend.play(buffer, {}); manual.pause();+ for (let i = 0; i < 100; i++) { backend.suspend(); backend.resume(); }+ expect(manual.isPlaying).toBe(false); expect(active.isPlaying).toBe(true);+ const host = (globalThis as any).wx;+ expect(host.createInnerAudioContext).toHaveBeenCalledTimes(2);+ expect(host.createInnerAudioContext.mock.results[0].value.play).toHaveBeenCalledOnce();+ expect(host.createInnerAudioContext.mock.results[1].value.play).toHaveBeenCalledTimes(101);+ });it('should pause all active contexts on suspend', async () => {const h1 = await backend.loadBuffer('a.mp3');const h2 = await backend.loadBuffer('b.mp3');diff --git a/tools/releaseNotes.mjs b/tools/releaseNotes.mjsindex e0e1cbeda..83c08c56a 100644--- a/tools/releaseNotes.mjs+++ b/tools/releaseNotes.mjs@@ -28,6 +28,8 @@ export const NOTES_FLOOR = 'v0.65.0';* the reader recognises anyway.*/export const NOTED = {+ 'fix(audio): preserve manual minigame pauses on device resume':+ { note: 'Mini-game device resume preserves manual audio pauses.' },'fix(audio): isolate plugin cleanup by app':{ note: 'Audio plugin cleanup belongs to each App.' },'fix(audio): guard reused pool nodes from retired handles':