mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-19 14:52:40 +08:00
fix audio can't playOneShot on iOS browser (#8117)
* fix audio can't playOneShot on iOS browser * fix ESLint * fix ESLint * fix ESLint * fix ESLint
This commit is contained in:
@@ -51,7 +51,9 @@ class AudioManagerWeb extends AudioManager<ManagedAudio> {
|
||||
} else {
|
||||
audioToDiscard = this._playingAudios.shift();
|
||||
}
|
||||
audioToDiscard?.stop();
|
||||
if (audioToDiscard) {
|
||||
audioToDiscard.stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,6 +136,13 @@ export class AudioPlayerWeb extends AudioPlayer {
|
||||
|
||||
public playOneShot (volume = 1) {
|
||||
if (!this._nativeAudio) { return; }
|
||||
if (this._context.state as string === 'interrupted' || this._context.state as string === 'suspended') {
|
||||
if (this._context.resume) {
|
||||
this._context.resume().catch((err) => {
|
||||
console.error(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
AudioPlayerWeb._manager.discardOnePlayingIfNeeded();
|
||||
const gainNode = this._context.createGain();
|
||||
gainNode.connect(this._context.destination);
|
||||
@@ -245,7 +254,9 @@ export class AudioPlayerWeb extends AudioPlayer {
|
||||
|
||||
private _onGesture () {
|
||||
if (this._context.state !== 'running') {
|
||||
this._context.resume().then(this._onGestureProceedCB);
|
||||
this._context.resume().then(this._onGestureProceedCB).catch((err) => {
|
||||
console.error(err);
|
||||
});
|
||||
} else {
|
||||
this._onGestureProceed();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user