mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
fix audio crash on COCOSPLAY (#8629)
* fix audio crash on COCOSPLAY * add Note
This commit is contained in:
+16
-6
@@ -81,12 +81,22 @@ minigame.onAccelerometerChange = function (cb) {
|
||||
};
|
||||
// #endregion Accelerometer
|
||||
|
||||
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(ral, {
|
||||
onPlay: true, // polyfill for vivo
|
||||
onPause: true,
|
||||
onStop: true,
|
||||
onSeek: true,
|
||||
});
|
||||
// NOTE: Audio playing crash on COCOSPLAY, need to play audio asynchronously.
|
||||
if (COCOSPLAY) {
|
||||
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(ral, {
|
||||
onPlay: true, // polyfill for vivo
|
||||
onPause: true,
|
||||
onStop: true,
|
||||
onSeek: true,
|
||||
}, true);
|
||||
} else {
|
||||
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(ral, {
|
||||
onPlay: true, // polyfill for vivo
|
||||
onPause: true,
|
||||
onStop: true,
|
||||
onSeek: true,
|
||||
});
|
||||
}
|
||||
|
||||
// safeArea
|
||||
// origin point on the top-left corner
|
||||
|
||||
+6
-64
@@ -75,70 +75,12 @@ minigame.startAccelerometer = function (res: any) {
|
||||
};
|
||||
// #endregion Accelerometer
|
||||
|
||||
// minigame.createInnerAudioContext = createInnerAudioContextPolyfill(wx, {
|
||||
// onPlay: true,
|
||||
// onPause: true,
|
||||
// onStop: true,
|
||||
// onSeek: false,
|
||||
// });
|
||||
|
||||
// TODO: move to createInnerAudioContextPolyfill()
|
||||
minigame.createInnerAudioContext = function () {
|
||||
const audioContext: InnerAudioContext = wx.createInnerAudioContext();
|
||||
|
||||
// add polyfill if onPlay method doesn't work this platform
|
||||
const originalPlay = audioContext.play;
|
||||
let _onPlayCB: (()=> void) | null = null;
|
||||
Object.defineProperty(audioContext, 'onPlay', {
|
||||
value (cb: ()=> void) {
|
||||
_onPlayCB = cb;
|
||||
},
|
||||
});
|
||||
Object.defineProperty(audioContext, 'play', {
|
||||
value () {
|
||||
originalPlay.call(audioContext);
|
||||
if (_onPlayCB) {
|
||||
setTimeout(_onPlayCB, 0);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// add polyfill if onPause method doesn't work this platform
|
||||
const originalPause = audioContext.pause;
|
||||
let _onPauseCB: (()=> void) | null = null;
|
||||
Object.defineProperty(audioContext, 'onPause', {
|
||||
value (cb: ()=> void) {
|
||||
_onPauseCB = cb;
|
||||
},
|
||||
});
|
||||
Object.defineProperty(audioContext, 'pause', {
|
||||
value () {
|
||||
originalPause.call(audioContext);
|
||||
if (_onPauseCB) {
|
||||
setTimeout(_onPauseCB, 0);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// add polyfill if onStop method doesn't work on this platform
|
||||
const originalStop = audioContext.stop;
|
||||
let _onStopCB: (()=> void) | null = null;
|
||||
Object.defineProperty(audioContext, 'onStop', {
|
||||
value (cb: ()=> void) {
|
||||
_onStopCB = cb;
|
||||
},
|
||||
});
|
||||
Object.defineProperty(audioContext, 'stop', {
|
||||
value () {
|
||||
originalStop.call(audioContext);
|
||||
if (_onStopCB) {
|
||||
setTimeout(_onStopCB, 0);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
return audioContext;
|
||||
};
|
||||
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(wx, {
|
||||
onPlay: true,
|
||||
onPause: true,
|
||||
onStop: true,
|
||||
onSeek: false,
|
||||
}, true);
|
||||
|
||||
// safeArea
|
||||
// origin point on the top-left corner
|
||||
|
||||
+30
-5
@@ -21,9 +21,10 @@ type InnerAudioContextPolyfillConfig = {
|
||||
* This method is to create a polyfill on minigame platform when the innerAudioContext callback doesn't work.
|
||||
* @param minigameEnv Specify the minigame enviroment such as `wx`, `swan` etc.
|
||||
* @param polyfillConfig Specify the field, if it's true, the polyfill callback will be applied.
|
||||
* @param isAsynchronous Specify whether the callback is called asynchronous.
|
||||
* @returns A polyfilled createInnerAudioContext method.
|
||||
*/
|
||||
export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfig: InnerAudioContextPolyfillConfig) {
|
||||
export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfig: InnerAudioContextPolyfillConfig, isAsynchronous = false) {
|
||||
return () => {
|
||||
const audioContext: InnerAudioContext = minigameEnv.createInnerAudioContext();
|
||||
|
||||
@@ -41,7 +42,13 @@ export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfi
|
||||
configurable: true,
|
||||
value () {
|
||||
originalPlay.call(audioContext);
|
||||
_onPlayCB?.();
|
||||
if (_onPlayCB) {
|
||||
if (isAsynchronous) {
|
||||
setTimeout(_onPlayCB, 0);
|
||||
} else {
|
||||
_onPlayCB();
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -60,7 +67,13 @@ export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfi
|
||||
configurable: true,
|
||||
value () {
|
||||
originalPause.call(audioContext);
|
||||
_onPauseCB?.();
|
||||
if (_onPauseCB) {
|
||||
if (isAsynchronous) {
|
||||
setTimeout(_onPauseCB, 0);
|
||||
} else {
|
||||
_onPauseCB();
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -79,7 +92,13 @@ export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfi
|
||||
configurable: true,
|
||||
value () {
|
||||
originalStop.call(audioContext);
|
||||
_onStopCB?.();
|
||||
if (_onStopCB) {
|
||||
if (isAsynchronous) {
|
||||
setTimeout(_onStopCB, 0);
|
||||
} else {
|
||||
_onStopCB();
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -98,7 +117,13 @@ export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfi
|
||||
configurable: true,
|
||||
value (time: number) {
|
||||
originalSeek.call(audioContext, time);
|
||||
_onSeekCB?.();
|
||||
if (_onSeekCB) {
|
||||
if (isAsynchronous) {
|
||||
setTimeout(_onSeekCB, 0);
|
||||
} else {
|
||||
_onSeekCB();
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user