fix Audio bug (#8515)

* support AudioPlayer.loadOneShotAudio

* support native

* support minigame

* support createInnerAudioContextPolyfill

update

* fix audioLoading on runtime

* fix Safari doesn't support promise based decodeAudioData

* optimize interface design

* add onPlay polyfill for vivo

* fix setting AudioSource.clip concurrently

* add createInnerAudioContextPolyfill for Xiaomi

* add createInnerAudioContextPolyfill on Baidu

* fix web audio player currentTime
This commit is contained in:
PP
2021-04-23 18:07:58 +08:00
committed by GitHub
parent 818482bcb9
commit cd1548d3f6
17 changed files with 577 additions and 529 deletions
+33 -18
View File
@@ -1,27 +1,39 @@
declare module 'pal/audio' {
/**
* Each audio instance needs to be managed, but should not take up too much memory.
* The `OneShotAudio` is a lite version of audio interface designed for audio manager.
* The `OneShotAudio` is a lite version of audio class designed for audio manager.
*/
export interface OneShotAudio {
export class OneShotAudio {
/**
* Constructor for OneShotAudio.
* This is a private constructor.
* OneShotAudio shoulb be instantiated by `AudioPlayer.loadOneShotAudio()` method.
* @param nativeAudio The native audio such as `HTMLAudioElement` or `AudioBuffer`.
* @param volume Init the audio volume.
*/
private constructor (nativeAudio: unknown, volume: number);
/**
* Play the audio.
*/
public play (): void;
/**
* Stops playing the audio.
*/
stop (): void;
public stop (): void;
/**
* Register an callback which would be called when the play starts.
* @param cb The callback.
* @returns This audio.
* Get or set the onPlay callback.
*/
onPlay (cb: () => void): OneShotAudio;
get onPlay (): () => void | undefined;
set onPlay (cb: () => void | undefined);
/**
* Register an callback which would be called when the play ends.
* @param cb The callback.
* @returns This audio.
* Get or set the onEnd callback.
*/
onEnded (cb: () => void): OneShotAudio;
get onEnd (): () => void | undefined;
set onEnd (cb: () => void | undefined);
}
export class AudioPlayer {
@@ -42,12 +54,22 @@ declare module 'pal/audio' {
/**
* Asynchronously load a native audio for playing one shot.
* This is a basic loading method for AudioPlayer.
* @param url URL to the audio.
* @param opts Load options.
* @returns The native audio such as `HTMLAudioElement` or `AudioBuffer`.
*/
static loadNative (url: string, opts?: import('pal/audio/type').AudioLoadOptions): Promise<unknown>;
/**
* Asynchronously load an OneShotAudio instance.
* @param url URL to the audio.
* @param volume Specify the volume.
* @param opts Load options.
* @returns The OneShotAudio instance.
*/
static loadOneShotAudio (url: string, volume: number, opts?: import('pal/audio/type').AudioLoadOptions): Promise<OneShotAudio>;
/**
* Max audio channel count allowed on current platform.
* If the amount of playing audios exceeds the limit,
@@ -101,13 +123,6 @@ declare module 'pal/audio' {
*/
seek (time: number): Promise<void>;
/**
* Plays one shot of the audio, the returned `OneShotAudio` can be managed by audio manager.
* @param volume Specifies the volume. If not specified...
* @returns The one shot audio.
*/
playOneShot (volume?: number): OneShotAudio;
/**
* Asynchronously plays the audio or resumes the audio while it is paused.
*/
+35 -21
View File
@@ -69,6 +69,7 @@ export class AudioSource extends Component {
private _operationsBeforeLoading: string[] = [];
private _isLoaded = false;
private _lastSetClip?: AudioClip;
/**
* @en
* The default AudioClip to be played for this audio source.
@@ -90,23 +91,31 @@ export class AudioSource extends Component {
private _syncPlayer () {
const clip = this._clip;
this._isLoaded = false;
// clear old player
if (this._player) {
this._player.offEnded();
this._player.offInterruptionBegin();
this._player.offInterruptionEnd();
this._player.destroy();
this._player = null;
if (!clip || this._lastSetClip === clip) {
return;
}
if (!clip) { return; }
if (!clip._nativeAsset) {
console.error('Invalid audio clip');
return;
}
this._lastSetClip = clip;
AudioPlayer.load(clip._nativeAsset.url, {
audioLoadMode: clip.loadMode,
}).then((player) => {
if (this._lastSetClip !== clip) {
// In case the developers set AudioSource.clip concurrently,
// we should choose the last one player of AudioClip set to AudioSource.clip
// instead of the last loaded one.
return;
}
this._isLoaded = true;
// clear old player
if (this._player) {
this._player.offEnded();
this._player.offInterruptionBegin();
this._player.offInterruptionEnd();
this._player.destroy();
}
this._player = player;
player.onEnded(() => {
audioManager.removePlaying(player);
@@ -257,24 +266,29 @@ export class AudioSource extends Component {
/**
* @en
* Plays an AudioClip, and scales volume by volumeScale.<br>
* Note: for multiple playback on the same clip, the actual behavior is platform-specific.<br>
* Re-start style fallback will be used if the underlying platform doesn't support it.
* Plays an AudioClip, and scales volume by volumeScale. The result volume is `audioSource.volume * volumeScale`. <br>
* @zh
* 以指定音量播放一个音频一次。<br>
* 注意,对同一个音频片段,不同平台多重播放效果存在差异。<br>
* 对不支持的平台,如前一次尚未播完,则会立即重新播放。
* 以指定音量倍数播放一个音频一次。最终播放的音量为 `audioSource.volume * volumeScale`。 <br>
* @param clip The audio clip to be played.
* @param volumeScale volume scaling factor wrt. current value.
*/
public playOneShot (clip: AudioClip, volumeScale = 1) {
audioManager.discardOnePlayingIfNeeded();
const oneShotAudio = this._player?.playOneShot(this._volume * volumeScale);
oneShotAudio?.onPlay(() => {
audioManager.addPlaying(oneShotAudio);
}).onEnded(() => {
audioManager.removePlaying(oneShotAudio);
});
if (!clip._nativeAsset) {
console.error('Invalid audio clip');
return;
}
AudioPlayer.loadOneShotAudio(clip._nativeAsset.url, this._volume * volumeScale, {
audioLoadMode: clip.loadMode,
}).then((oneShotAudio) => {
audioManager.discardOnePlayingIfNeeded();
oneShotAudio.onPlay = () => {
audioManager.addPlaying(oneShotAudio);
};
oneShotAudio.onEnd = () => {
audioManager.removePlaying(oneShotAudio);
};
oneShotAudio.play();
}).catch((e) => {});
}
protected _syncStates () {
+50 -28
View File
@@ -1,11 +1,52 @@
import { minigame } from 'pal/minigame';
import { OneShotAudio } from 'pal/audio';
import { legacyCC } from '../../../cocos/core/global-exports';
import { EventTarget } from '../../../cocos/core/event/event-target';
import { AudioEvent, AudioState, AudioType } from '../type';
import { clamp, clamp01 } from '../../../cocos/core';
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
class OneShotAudio {
private _innerAudioContext: InnerAudioContext;
private _onPlayCb?: () => void;
get onPlay () {
return this._onPlayCb;
}
set onPlay (cb) {
if (this._onPlayCb) {
this._innerAudioContext.offPlay(this._onPlayCb);
}
this._onPlayCb = cb;
if (cb) {
this._innerAudioContext.onPlay(cb);
}
}
private _onEndCb?: () => void;
get onEnd () {
return this._onEndCb;
}
set onEnd (cb) {
if (this._onEndCb) {
this._innerAudioContext.offEnded(this._onEndCb);
}
this._onEndCb = cb;
if (cb) {
this._innerAudioContext.onEnded(cb);
}
}
private constructor (nativeAudio: InnerAudioContext, volume: number) {
this._innerAudioContext = nativeAudio;
nativeAudio.volume = volume;
}
public play (): void {
this._innerAudioContext.play();
}
public stop (): void {
this._innerAudioContext.stop();
}
}
export class AudioPlayer implements OperationQueueable {
private _innerAudioContext: any;
private _state: AudioState = AudioState.INIT;
@@ -133,6 +174,14 @@ export class AudioPlayer implements OperationQueueable {
innerAudioContext.src = url;
});
}
static loadOneShotAudio (url: string, volume: number): Promise<OneShotAudio> {
return new Promise((resolve, reject) => {
AudioPlayer.loadNative(url).then((innerAudioContext) => {
// @ts-expect-error AudioPlayer should be a friend class in OneShotAudio
resolve(new OneShotAudio(innerAudioContext, volume));
}).catch(reject);
});
}
static readonly maxAudioChannel = 10;
get state (): AudioState {
@@ -167,33 +216,6 @@ export class AudioPlayer implements OperationQueueable {
});
}
playOneShot (volume = 1): OneShotAudio {
let innerAudioContext;
let onPlayCb: () => void;
let onEndedCb: () => void;
AudioPlayer.loadNative(this._innerAudioContext.src).then((res) => {
innerAudioContext = res;
innerAudioContext.volume = volume;
onPlayCb && innerAudioContext.onPlay(onPlayCb);
onEndedCb && innerAudioContext.onEnded(onEndedCb);
innerAudioContext.play();
}).catch((e) => {});
const oneShotAudio: OneShotAudio = {
stop () {
innerAudioContext && innerAudioContext.stop();
},
onPlay (cb) {
onPlayCb = cb;
return this;
},
onEnded (cb) {
onEndedCb = cb;
return this;
},
};
return oneShotAudio;
}
@enqueueOperation
play (): Promise<void> {
return new Promise((resolve) => {
+47 -27
View File
@@ -1,4 +1,3 @@
import { OneShotAudio } from 'pal/audio';
import { AudioType, AudioState, AudioEvent } from '../type';
import { EventTarget } from '../../../cocos/core/event/event-target';
import { legacyCC } from '../../../cocos/core/global-exports';
@@ -9,7 +8,45 @@ const urlCount: Record<string, number> = {};
const audioEngine = jsb.AudioEngine;
const INVALID_AUDIO_ID = -1;
// TODO: set state before playing
export class OneShotAudio {
private _id: number = INVALID_AUDIO_ID;
private _url: string;
private _volume: number;
private _onPlayCb?: () => void;
get onPlay () {
return this._onPlayCb;
}
set onPlay (cb) {
this._onPlayCb = cb;
}
private _onEndCb?: () => void;
get onEnd () {
return this._onEndCb;
}
set onEnd (cb) {
this._onEndCb = cb;
}
private constructor (url: string, volume: number) {
this._url = url;
this._volume = volume;
}
public play (): void {
this._id = jsb.AudioEngine.play2d(this._url, false, this._volume);
jsb.AudioEngine.setFinishCallback(this._id, () => {
this.onEnd?.();
});
this.onPlay?.();
}
public stop (): void {
if (this._id === INVALID_AUDIO_ID) {
return;
}
jsb.AudioEngine.stop(this._id);
}
}
export class AudioPlayer implements OperationQueueable {
private _url: string;
private _id: number = INVALID_AUDIO_ID;
@@ -83,6 +120,14 @@ export class AudioPlayer implements OperationQueueable {
});
});
}
static loadOneShotAudio (url: string, volume: number): Promise<OneShotAudio> {
return new Promise((resolve, reject) => {
AudioPlayer.loadNative(url).then((url) => {
// @ts-expect-error AudioPlayer should be a friend class in OneShotAudio
resolve(new OneShotAudio(url, volume));
}).catch(reject);
});
}
static readonly maxAudioChannel: number = audioEngine.getMaxAudioInstance();
private get _isValid (): boolean {
@@ -150,31 +195,6 @@ export class AudioPlayer implements OperationQueueable {
return resolve();
});
}
playOneShot (volume = 1): OneShotAudio {
let id = INVALID_AUDIO_ID;
let onPlayCb: () => void;
let onEndedCb: () => void;
setTimeout(() => {
id = audioEngine.play2d(this._url, false, volume);
onPlayCb && onPlayCb();
onEndedCb && audioEngine.setFinishCallback(id, onEndedCb);
});
return {
stop () {
if (id !== INVALID_AUDIO_ID) {
audioEngine.stop(id);
}
},
onPlay (cb) {
onPlayCb = cb;
return this;
},
onEnded (cb) {
onEndedCb = cb;
return this;
},
};
}
@enqueueOperation
play (): Promise<void> {
+67 -54
View File
@@ -1,5 +1,4 @@
import { system } from 'pal/system';
import { OneShotAudio } from 'pal/audio';
import { AudioEvent, AudioState, AudioType } from '../type';
import { EventTarget } from '../../../cocos/core/event/event-target';
import { legacyCC } from '../../../cocos/core/global-exports';
@@ -7,10 +6,66 @@ import { clamp, clamp01 } from '../../../cocos/core';
import { enqueueOperation, OperationInfo, OperationQueueable } from '../operation-queue';
import { BrowserType, OS } from '../../system/enum-type';
function ensurePlaying (domAudio: HTMLAudioElement): Promise<void> {
return new Promise((resolve) => {
const promise = domAudio.play();
if (promise === undefined) { // Chrome50/Firefox53 below
return resolve();
}
promise.then(resolve).catch(() => {
const onGesture = () => {
domAudio.play().catch((e) => {});
resolve();
};
const canvas = document.getElementById('GameCanvas') as HTMLCanvasElement;
canvas?.addEventListener('touchend', onGesture, { once: true });
canvas?.addEventListener('mousedown', onGesture, { once: true });
});
return null;
});
}
export class OneShotAudioDOM {
private _domAudio: HTMLAudioElement;
private _onPlayCb?: () => void;
get onPlay () {
return this._onPlayCb;
}
set onPlay (cb) {
this._onPlayCb = cb;
}
private _onEndCb?: () => void;
get onEnd () {
return this._onEndCb;
}
set onEnd (cb) {
if (this._onEndCb) {
this._domAudio.removeEventListener('ended', this._onEndCb);
}
this._onEndCb = cb;
if (cb) {
this._domAudio.addEventListener('ended', cb);
}
}
private constructor (nativeAudio: HTMLAudioElement, volume: number) {
this._domAudio = nativeAudio;
nativeAudio.volume = volume;
}
public play (): void {
ensurePlaying(this._domAudio).then(() => {
this.onPlay?.();
}).catch((e) => {});
}
public stop (): void {
this._domAudio.pause();
}
}
export class AudioPlayerDOM implements OperationQueueable {
private _domAudio: HTMLAudioElement;
private _state: AudioState = AudioState.INIT;
private _onGesture?: () => void;
private _onHide?: () => void;
private _onShow?: () => void;
private _onEnded?: () => void;
@@ -24,9 +79,6 @@ export class AudioPlayerDOM implements OperationQueueable {
// event
// TODO: should not call engine API in pal
this._onGesture = () => this._eventTarget.emit(AudioEvent.USER_GESTURE);
legacyCC.game.canvas.addEventListener('touchend', this._onGesture);
legacyCC.game.canvas.addEventListener('mouseup', this._onGesture);
this._onHide = () => {
if (this._state === AudioState.PLAYING) {
this.pause().then(() => {
@@ -53,11 +105,6 @@ export class AudioPlayerDOM implements OperationQueueable {
}
destroy () {
if (this._onGesture) {
legacyCC.game.canvas.removeEventListener('touchend', this._onGesture);
legacyCC.game.canvas.removeEventListener('mouseup', this._onGesture);
this._onGesture = undefined;
}
if (this._onShow) {
legacyCC.game.off(legacyCC.Game.EVENT_SHOW, this._onShow);
this._onShow = undefined;
@@ -119,6 +166,15 @@ export class AudioPlayerDOM implements OperationQueueable {
domAudio.src = url;
});
}
static loadOneShotAudio (url: string, volume: number): Promise<OneShotAudioDOM> {
return new Promise((resolve, reject) => {
AudioPlayerDOM.loadNative(url).then((domAudio) => {
// @ts-expect-error AudioPlayer should be a friend class in OneShotAudio
const oneShotAudio = new OneShotAudioDOM(domAudio, volume);
resolve(oneShotAudio);
}).catch(reject);
});
}
get src (): string {
return this._domAudio ? this._domAudio.src : '';
@@ -156,53 +212,10 @@ export class AudioPlayerDOM implements OperationQueueable {
return Promise.resolve();
}
private _ensurePlaying (domAudio: HTMLAudioElement): Promise<void> {
return new Promise((resolve) => {
const promise = domAudio.play();
if (promise === undefined) { // Chrome50/Firefox53 below
return resolve();
}
promise.then(resolve).catch(() => {
this._eventTarget.once(AudioEvent.USER_GESTURE, () => {
domAudio.play().catch((e) => {});
resolve();
});
});
return null;
});
}
playOneShot (volume = 1): OneShotAudio {
let onPlayCb: () => void;
let onEndedCb: () => void;
let domAudio: HTMLAudioElement;
AudioPlayerDOM.loadNative(this._domAudio.src).then((res) => {
domAudio = res;
domAudio.volume = volume;
onEndedCb && domAudio.addEventListener('ended', onEndedCb);
this._ensurePlaying(domAudio).then(() => {
onPlayCb && onPlayCb();
}).catch((e) => {});
}).catch((e) => {});
const oneShotAudio: OneShotAudio = {
stop () {
domAudio.pause();
},
onPlay (cb) {
onPlayCb = cb;
return this;
},
onEnded (cb) {
onEndedCb = cb;
return this;
},
};
return oneShotAudio;
}
@enqueueOperation
play (): Promise<void> {
return new Promise((resolve) => {
this._ensurePlaying(this._domAudio).then(() => {
ensurePlaying(this._domAudio).then(() => {
this._state = AudioState.PLAYING;
resolve();
}).catch((e) => {});
+149 -91
View File
@@ -1,4 +1,3 @@
import { OneShotAudio } from 'pal/audio';
import { AudioEvent, AudioState, AudioType } from '../type';
import { EventTarget } from '../../../cocos/core/event/event-target';
import { legacyCC } from '../../../cocos/core/global-exports';
@@ -7,10 +6,139 @@ import { enqueueOperation, OperationInfo, OperationQueueable } from '../operatio
// NOTE: fix CI
const AudioContextClass = (window.AudioContext || window.webkitAudioContext || window.mozAudioContext);
export class AudioContextAgent {
public static support = !!AudioContextClass;
public _context: AudioContext;
constructor () {
this._context = new (window.AudioContext || window.webkitAudioContext || window.mozAudioContext)();
}
get currentTime () {
return this._context.currentTime;
}
public decodeAudioData (audioData: ArrayBuffer): Promise<AudioBuffer> {
return new Promise((resolve) => {
const promise = this._context.decodeAudioData(audioData, (audioBuffer) => {
resolve(audioBuffer);
});
promise?.catch((e) => {}); // Safari doesn't support the promise based decodeAudioData
});
}
public runContext (): Promise<void> {
return new Promise((resolve) => {
const context = this._context;
if (!context.resume) {
return resolve();
}
if (context.state === 'running') {
return resolve();
}
context.resume().catch((e) => {});
// promise rejection cannot be caught, need to check running state again
if (<string>context.state !== 'running') {
const canvas = document.getElementById('GameCanvas') as HTMLCanvasElement;
const onGesture = () => {
context.resume().then(resolve).catch((e) => {});
};
canvas?.addEventListener('touchend', onGesture, { once: true });
canvas?.addEventListener('mousedown', onGesture, { once: true });
}
return null;
});
}
public createBufferSource (audioBuffer?: AudioBuffer, loop?: boolean) {
const sourceBufferNode = this._context.createBufferSource();
if (audioBuffer !== undefined) {
sourceBufferNode.buffer = audioBuffer;
}
if (loop !== undefined) {
sourceBufferNode.loop = loop;
}
return sourceBufferNode;
}
public createGain (volume?: number) {
const gainNode = this._context.createGain();
if (volume !== undefined) {
this.setGainValue(gainNode, volume);
}
return gainNode;
}
public setGainValue (gain: GainNode, volume: number) {
if (gain.gain.setTargetAtTime) {
try {
gain.gain.setTargetAtTime(volume, this._context.currentTime, 0);
} catch (e) {
// Some unknown browsers may crash if timeConstant is 0
gain.gain.setTargetAtTime(volume, this._context.currentTime, 0.01);
}
} else {
gain.gain.value = volume;
}
}
public connectContext (audioNode: GainNode) {
if (!this._context) {
return;
}
audioNode.connect(this._context.destination);
}
}
let audioContextAgent: AudioContextAgent | undefined;
if (AudioContextAgent.support) {
audioContextAgent = new AudioContextAgent();
}
export class OneShotAudioWeb {
private _duration: number;
private _bufferSourceNode: AudioBufferSourceNode;
private _onPlayCb?: () => void;
get onPlay () {
return this._onPlayCb;
}
set onPlay (cb) {
this._onPlayCb = cb;
}
private _onEndCb?: () => void;
get onEnd () {
return this._onEndCb;
}
set onEnd (cb) {
this._onEndCb = cb;
}
private constructor (audioBuffer: AudioBuffer, volume: number) {
this._duration = audioBuffer.duration;
this._bufferSourceNode = audioContextAgent!.createBufferSource(audioBuffer, false);
const gainNode = audioContextAgent!.createGain(volume);
this._bufferSourceNode.connect(gainNode);
audioContextAgent!.connectContext(gainNode);
}
public play (): void {
// audioContextAgent does exist
audioContextAgent!.runContext().then(() => {
this._bufferSourceNode.start();
this.onPlay?.();
setTimeout(() => {
this.onEnd?.();
}, this._duration * 1000);
}).catch((e) => {});
}
public stop (): void {
this._bufferSourceNode.stop();
}
}
export class AudioPlayerWeb implements OperationQueueable {
private _src: string;
private static _context: AudioContext = AudioContextClass && new AudioContextClass();
private _audioBuffer: AudioBuffer;
private _sourceNode?: AudioBufferSourceNode;
private _gainNode: GainNode;
@@ -25,21 +153,16 @@ export class AudioPlayerWeb implements OperationQueueable {
public _eventTarget: EventTarget = new EventTarget();
public _operationQueue: OperationInfo[] = [];
private _onGesture?: () => void;
private _onHide?: () => void;
private _onShow?: () => void;
constructor (audioBuffer: AudioBuffer, url: string) {
const context = AudioPlayerWeb._context;
this._audioBuffer = audioBuffer;
this._gainNode = context.createGain();
this._gainNode.connect(context.destination);
this._gainNode = audioContextAgent!.createGain();
audioContextAgent!.connectContext(this._gainNode);
this._src = url;
// event
// TODO: should not call engine API in pal
this._onGesture = () => this._eventTarget.emit(AudioEvent.USER_GESTURE);
legacyCC.game.canvas.addEventListener('touchend', this._onGesture);
legacyCC.game.canvas.addEventListener('mouseup', this._onGesture);
this._onHide = () => {
if (this._state === AudioState.PLAYING) {
this.pause().then(() => {
@@ -63,11 +186,6 @@ export class AudioPlayerWeb implements OperationQueueable {
// @ts-expect-error need to release AudioBuffer instance
this._audioBuffer = undefined;
}
if (this._onGesture) {
legacyCC.game.canvas.removeEventListener('touchend', this._onGesture);
legacyCC.game.canvas.removeEventListener('mouseup', this._onGesture);
this._onGesture = undefined;
}
if (this._onShow) {
legacyCC.game.off(legacyCC.Game.EVENT_SHOW, this._onShow);
this._onShow = undefined;
@@ -93,7 +211,7 @@ export class AudioPlayerWeb implements OperationQueueable {
xhr.onload = () => {
if (xhr.status === 200 || xhr.status === 0) {
AudioPlayerWeb._context.decodeAudioData(xhr.response).then((buffer) => {
audioContextAgent!.decodeAudioData(xhr.response).then((buffer) => {
resolve(buffer);
}).catch((e) => {});
} else {
@@ -107,6 +225,15 @@ export class AudioPlayerWeb implements OperationQueueable {
xhr.send(null);
});
}
static loadOneShotAudio (url: string, volume: number): Promise<OneShotAudioWeb> {
return new Promise((resolve, reject) => {
AudioPlayerWeb.loadNative(url).then((audioBuffer) => {
// @ts-expect-error AudioPlayer should be a friend class in OneShotAudio
const oneShotAudio = new OneShotAudioWeb(audioBuffer, volume);
resolve(oneShotAudio);
}).catch(reject);
});
}
get src (): string {
return this._src;
@@ -130,26 +257,14 @@ export class AudioPlayerWeb implements OperationQueueable {
set volume (val: number) {
val = clamp01(val);
this._volume = val;
this._setGainValue(this._gainNode, val);
}
private _setGainValue (gain: GainNode, volume: number) {
if (gain.gain.setTargetAtTime) {
try {
gain.gain.setTargetAtTime(volume, AudioPlayerWeb._context.currentTime, 0);
} catch (e) {
// Some unknown browsers may crash if timeConstant is 0
gain.gain.setTargetAtTime(volume, AudioPlayerWeb._context.currentTime, 0.01);
}
} else {
gain.gain.value = volume;
}
audioContextAgent!.setGainValue(this._gainNode, val);
}
get duration (): number {
return this._audioBuffer.duration;
}
get currentTime (): number {
if (this._state !== AudioState.PLAYING) { return this._offset; }
return AudioPlayerWeb._context.currentTime - this._startTime + this._offset;
return (audioContextAgent!.currentTime - this._startTime + this._offset) % this._audioBuffer.duration;
}
@enqueueOperation
@@ -166,75 +281,18 @@ export class AudioPlayerWeb implements OperationQueueable {
});
}
private _runContext (): Promise<void> {
return new Promise((resolve) => {
const context = AudioPlayerWeb._context;
if (!context.resume) {
return resolve();
}
if (context.state === 'running') {
return resolve();
}
context.resume().catch((e) => {});
// promise rejection cannot be caught, need to check running state again
if (<string>context.state !== 'running') {
this._eventTarget.once(AudioEvent.USER_GESTURE, () => {
context.resume().then(resolve).catch((e) => {});
});
}
return null;
});
}
playOneShot (volume = 1): OneShotAudio {
let onPlayCb: () => void;
let onEndedCb: () => void;
const context = AudioPlayerWeb._context;
let sourceNode: AudioBufferSourceNode;
setTimeout(() => {
this._runContext().then(() => {
sourceNode = context.createBufferSource();
sourceNode.buffer = this._audioBuffer;
sourceNode.loop = false;
const gainNode = context.createGain();
gainNode.connect(context.destination);
this._setGainValue(gainNode, volume);
sourceNode.connect(gainNode);
sourceNode.start();
onPlayCb && onPlayCb();
onEndedCb && setTimeout(onEndedCb, this._audioBuffer.duration * 1000);
}).catch((e) => {});
}, 0);
const oneShotAudio: OneShotAudio = {
stop () {
sourceNode.stop();
},
onPlay (cb) {
onPlayCb = cb;
return this;
},
onEnded (cb) {
onEndedCb = cb;
return this;
},
};
return oneShotAudio;
}
@enqueueOperation
play (): Promise<void> {
return new Promise((resolve) => {
const context = AudioPlayerWeb._context;
this._runContext().then(() => {
audioContextAgent!.runContext().then(() => {
// one AudioBufferSourceNode can't start twice
this._sourceNode?.stop();
this._sourceNode = context.createBufferSource();
this._sourceNode.buffer = this._audioBuffer;
this._sourceNode = audioContextAgent!.createBufferSource(this._audioBuffer, this.loop);
this._sourceNode.connect(this._gainNode);
this._sourceNode.start(0, this._offset);
this._state = AudioState.PLAYING;
this._startTime = context.currentTime;
this._startTime = audioContextAgent!.currentTime;
/* still not supported by all platforms *
this._sourceNode.onended = this._onEnded;
@@ -246,7 +304,7 @@ export class AudioPlayerWeb implements OperationQueueable {
this._eventTarget.emit(AudioEvent.ENDED);
clearInterval(this._currentTimer);
this._offset = 0;
this._startTime = context.currentTime;
this._startTime = audioContextAgent!.currentTime;
this._state = AudioState.INIT;
}
};
@@ -262,7 +320,7 @@ export class AudioPlayerWeb implements OperationQueueable {
if (this._state !== AudioState.PLAYING || !this._sourceNode) {
return Promise.resolve();
}
this._offset += AudioPlayerWeb._context.currentTime - this._startTime;
this._offset += audioContextAgent!.currentTime - this._startTime;
this._state = AudioState.PAUSED;
clearInterval(this._currentTimer);
this._sourceNode.stop();
+50 -11
View File
@@ -1,11 +1,38 @@
import { OneShotAudio } from 'pal/audio';
import { warnID } from '../../../cocos/core';
import { AudioLoadOptions, AudioType, AudioState } from '../type';
import { AudioPlayerDOM } from './player-dom';
import { AudioPlayerWeb } from './player-web';
import { AudioPlayerDOM, OneShotAudioDOM } from './player-dom';
import { AudioContextAgent, AudioPlayerWeb, OneShotAudioWeb } from './player-web';
type AbstractOneShotAudio = OneShotAudioDOM | OneShotAudioWeb;
type AbstractAudioPlayer = AudioPlayerDOM | AudioPlayerWeb;
export class OneShotAudio {
private _audio: AbstractOneShotAudio;
get onPlay () {
return this._audio.onPlay;
}
set onPlay (v) {
this._audio.onPlay = v;
}
get onEnd () {
return this._audio.onEnd;
}
set onEnd (v) {
this._audio.onEnd = v;
}
private constructor (audio: AbstractOneShotAudio) {
this._audio = audio;
}
public play (): void {
this._audio.play();
}
public stop (): void {
this._audio.stop();
}
}
export class AudioPlayer {
private _player: AbstractAudioPlayer;
constructor (player: AbstractAudioPlayer) {
@@ -14,12 +41,8 @@ export class AudioPlayer {
static load (url: string, opts?: AudioLoadOptions): Promise<AudioPlayer> {
return new Promise((resolve) => {
if (opts?.audioLoadMode === AudioType.DOM_AUDIO) {
AudioPlayerDOM.load(url).then((domPlayer) => {
resolve(new AudioPlayer(domPlayer));
}).catch((e) => {});
} else if (!(window.AudioContext || window.webkitAudioContext || window.mozAudioContext)) {
warnID(5201);
if (opts?.audioLoadMode === AudioType.DOM_AUDIO || !AudioContextAgent.support) {
if (!AudioContextAgent.support) { warnID(5201); }
AudioPlayerDOM.load(url).then((domPlayer) => {
resolve(new AudioPlayer(domPlayer));
}).catch((e) => {});
@@ -34,11 +57,28 @@ export class AudioPlayer {
this._player.destroy();
}
static loadNative (url: string, opts?: AudioLoadOptions): Promise<unknown> {
if (opts?.audioLoadMode === AudioType.DOM_AUDIO) {
if (opts?.audioLoadMode === AudioType.DOM_AUDIO || !AudioContextAgent.support) {
if (!AudioContextAgent.support) { warnID(5201); }
return AudioPlayerDOM.loadNative(url);
}
return AudioPlayerWeb.loadNative(url);
}
static loadOneShotAudio (url: string, volume: number, opts?: AudioLoadOptions): Promise<OneShotAudio> {
return new Promise((resolve, reject) => {
if (opts?.audioLoadMode === AudioType.DOM_AUDIO || !AudioContextAgent.support) {
if (!AudioContextAgent.support) { warnID(5201); }
AudioPlayerDOM.loadOneShotAudio(url, volume).then((oneShotAudioDOM) => {
// @ts-expect-error AudioPlayer should be a friend class in OneShotAudio
resolve(new OneShotAudio(oneShotAudioDOM));
}).catch(reject);
} else {
AudioPlayerWeb.loadOneShotAudio(url, volume).then((oneShotAudioWeb) => {
// @ts-expect-error AudioPlayer should be a friend class in OneShotAudio
resolve(new OneShotAudio(oneShotAudioWeb));
}).catch(reject);
}
});
}
static readonly maxAudioChannel = 24;
get src (): string { return this._player.src; }
@@ -52,7 +92,6 @@ export class AudioPlayer {
get currentTime (): number { return this._player.currentTime; }
seek (time: number): Promise<void> { return this._player.seek(time); }
playOneShot (volume?: number): OneShotAudio { return this._player.playOneShot(volume); }
play (): Promise<void> { return this._player.play(); }
pause (): Promise<void> { return this._player.pause(); }
stop (): Promise<void> { return this._player.stop(); }
+8 -1
View File
@@ -1,6 +1,6 @@
import { IMiniGame } from 'pal/minigame';
import { Orientation } from '../system/enum-type/orientation';
import { cloneObject } from '../utils';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';
declare let swan: any;
@@ -51,6 +51,13 @@ minigame.onAccelerometerChange = function (cb) {
swan.stopAccelerometer();
};
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(swan, {
onPlay: true,
onPause: true,
onStop: true,
onSeek: false,
});
minigame.getSafeArea = function () {
console.warn('getSafeArea is not supported on this platform');
if (minigame.getSystemInfoSync) {
+8 -1
View File
@@ -1,6 +1,6 @@
import { IMiniGame } from 'pal/minigame';
import { Orientation } from '../system/enum-type/orientation';
import { cloneObject } from '../utils';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';
declare let tt: any;
@@ -50,6 +50,13 @@ minigame.onAccelerometerChange = function (cb) {
tt.stopAccelerometer();
};
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(tt, {
onPlay: true,
onPause: true,
onStop: true,
onSeek: true,
});
// safeArea
// origin point on the top-left corner
minigame.getSafeArea = function () {
+7 -38
View File
@@ -1,6 +1,6 @@
import { IMiniGame } from 'pal/minigame';
import { Orientation } from '../system/enum-type/orientation';
import { cloneObject } from '../utils';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';
declare let ral: any;
@@ -61,43 +61,12 @@ minigame.onAccelerometerChange = function (cb) {
ral.stopAccelerometer();
};
minigame.createInnerAudioContext = function (): InnerAudioContext {
const audioContext: InnerAudioContext = ral.createInnerAudioContext();
// HACK: onSeeked method doesn't work on runtime
const originalSeek = audioContext.seek;
let _onSeekCB: (()=> void) | null = null;
audioContext.onSeeked = function (cb: ()=> void) {
_onSeekCB = cb;
};
audioContext.seek = function (time: number) {
originalSeek.call(audioContext, time);
_onSeekCB?.();
};
// HACK: onPause method doesn't work on runtime
const originalPause = audioContext.pause;
let _onPauseCB: (()=> void) | null = null;
audioContext.onPause = function (cb: ()=> void) {
_onPauseCB = cb;
};
audioContext.pause = function () {
originalPause.call(audioContext);
_onPauseCB?.();
};
// HACK: onStop method doesn't work on runtime
const originalStop = audioContext.stop;
let _onStopCB: (()=> void) | null = null;
audioContext.onStop = function (cb: ()=> void) {
_onStopCB = cb;
};
audioContext.stop = function () {
originalStop.call(audioContext);
_onStopCB?.();
};
return audioContext;
};
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(ral, {
onPlay: true, // polyfill for vivo
onPause: true,
onStop: true,
onSeek: true,
});
// safeArea
// origin point on the top-left corner
+8 -1
View File
@@ -1,6 +1,6 @@
import { IMiniGame } from 'pal/minigame';
import { Orientation } from '../system/enum-type/orientation';
import { cloneObject } from '../utils';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';
declare let wx: any;
@@ -54,6 +54,13 @@ minigame.onAccelerometerChange = function (cb) {
wx.stopAccelerometer();
};
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(wx, {
onPlay: true,
onPause: true,
onStop: true,
onSeek: false,
});
// safeArea
// origin point on the top-left corner
// FIX_ME: wrong safe area when orientation is landscape left
+8 -1
View File
@@ -1,6 +1,6 @@
import { IMiniGame } from 'pal/minigame';
import { Orientation } from '../system/enum-type/orientation';
import { cloneObject } from '../utils';
import { cloneObject, createInnerAudioContextPolyfill } from '../utils';
declare let qg: any;
@@ -71,6 +71,13 @@ minigame.onAccelerometerChange = function (cb) {
qg.stopAccelerometer();
};
minigame.createInnerAudioContext = createInnerAudioContextPolyfill(qg, {
onPlay: true,
onPause: true,
onStop: true,
onSeek: false,
});
minigame.getSafeArea = function () {
console.warn('getSafeArea is not supported on this platform');
if (minigame.getSystemInfoSync) {
+91
View File
@@ -1,3 +1,8 @@
/**
* This method clones methods in minigame enviroment, sucb as `wx`, `swan` etc. to a module called minigame.
* @param targetObject Usually it's specified as the minigame module.
* @param originObj Original minigame environment such as `wx`, `swan` etc.
*/
export function cloneObject (targetObject: any, originObj: any) {
Object.keys(originObj).forEach((key) => {
if (typeof originObj[key] === 'function') {
@@ -7,3 +12,89 @@ export function cloneObject (targetObject: any, originObj: any) {
targetObject[key] = originObj[key];
});
}
type MiningameAudioCallbackName = 'onPlay' | 'onPause' | 'onStop' | 'onSeek';
type InnerAudioContextPolyfillConfig = {
[CallbackName in MiningameAudioCallbackName]: boolean;
};
/**
* 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.
* @returns A polyfilled createInnerAudioContext method.
*/
export function createInnerAudioContextPolyfill (minigameEnv: any, polyfillConfig: InnerAudioContextPolyfillConfig) {
return () => {
const audioContext: InnerAudioContext = minigameEnv.createInnerAudioContext();
// add polyfill if onPlay method doesn't work this platform
if (polyfillConfig.onPlay) {
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);
_onPlayCB?.();
},
});
}
// add polyfill if onPause method doesn't work this platform
if (polyfillConfig.onPause) {
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);
_onPauseCB?.();
},
});
}
// add polyfill if onStop method doesn't work on this platform
if (polyfillConfig.onStop) {
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);
_onStopCB?.();
},
});
}
// add polyfill if onSeeked method doesn't work on this platform
if (polyfillConfig.onSeek) {
const originalSeek = audioContext.seek;
let _onSeekCB: (()=> void) | null = null;
Object.defineProperty(audioContext, 'onSeeked', {
value (cb: ()=> void) {
_onSeekCB = cb;
},
});
Object.defineProperty(audioContext, 'seek', {
value (time: number) {
originalSeek.call(audioContext, time);
_onSeekCB?.();
},
});
}
return audioContext;
};
}
@@ -1,179 +0,0 @@
/****************************************************************************
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
https://www.cocos.com/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated engine source code (the "Software"), a limited,
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
to use Cocos Creator solely to develop games on your target platforms. You shall
not use Cocos Creator software for developing other software or tools that's
used for developing games. You are not granted to publish, distribute,
sublicense, and/or sell copies of Cocos Creator.
The software or tools in this License Agreement are licensed, not sold.
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
const { loadInnerAudioContext } = require('./download-audio');
const AudioPlayer = cc.internal.AudioPlayer;
if (AudioPlayer) {
const { PlayingState, AudioType } = cc.AudioClip;
const AudioManager = cc.internal.AudioManager;
AudioManager.maxAudioChannel = 10;
class AudioManagerMiniGame extends AudioManager {
discardOnePlayingIfNeeded() {
if (this._playingAudios.length < AudioManager.maxAudioChannel) {
return;
}
// a played audio has a higher priority than a played shot
let audioToDiscard;
let oldestOneShotIndex = this._playingAudios.findIndex(audio => !(audio instanceof AudioPlayerRuntime));
if (oldestOneShotIndex > -1) {
audioToDiscard = this._playingAudios[oldestOneShotIndex];
this._playingAudios.splice(oldestOneShotIndex, 1);
}
else {
audioToDiscard = this._playingAudios.shift();
}
if (audioToDiscard) {
audioToDiscard.stop();
}
}
}
cc.AudioClip.prototype._getPlayer = function (clip) {
this._loadMode = AudioType.JSB_AUDIO;
return AudioPlayerRuntime;
};
class AudioPlayerRuntime extends AudioPlayer {
static _manager = new AudioManagerMiniGame();
_startTime = 0;
_offset = 0;
_volume = 1;
_loop = false;
constructor (info) {
super(info);
this._nativeAudio = info.nativeAudio;
this._nativeAudio.onPlay(() => {
if (this._state === PlayingState.PLAYING) { return; }
this._state = PlayingState.PLAYING;
this._startTime = performance.now();
this._clip.emit('started');
});
this._nativeAudio.onPause(() => {
if (this._state === PlayingState.STOPPED) { return; }
this._state = PlayingState.STOPPED;
this._offset += performance.now() - this._startTime;
});
this._nativeAudio.onStop(() => {
if (this._state === PlayingState.STOPPED) { return; }
this._state = PlayingState.STOPPED;
this._offset = 0;
});
this._nativeAudio.onEnded(() => {
if (this._state === PlayingState.STOPPED) { return; }
this._state = PlayingState.STOPPED;
this._offset = 0;
this._clip.emit('ended');
AudioPlayerRuntime._manager.removePlaying(this);
});
this._nativeAudio.onError(function (res) { return console.error(res.errMsg);});
}
play () {
if (!this._nativeAudio) { return; }
if (this._blocking) { this._interrupted = true; return; }
if (this._state === PlayingState.PLAYING) {
/* sometimes there is no way to update the playing state
especially when player unplug earphones and the audio automatically stops
so we need to force updating the playing state by pausing audio */
this.pause();
// restart if already playing
this.setCurrentTime(0);
}
AudioPlayerRuntime._manager.discardOnePlayingIfNeeded();
this._nativeAudio.play();
AudioPlayerRuntime._manager.addPlaying(this);
}
pause () {
if (!this._nativeAudio || this._state !== PlayingState.PLAYING) { return; }
this._nativeAudio.pause();
AudioPlayerRuntime._manager.removePlaying(this._clip);
}
stop () {
if (!this._nativeAudio) { return; }
this._nativeAudio.stop();
AudioPlayerRuntime._manager.removePlaying(this._clip);
}
playOneShot (volume) {
loadInnerAudioContext(this._nativeAudio.src).then(innerAudioContext => {
AudioPlayerRuntime._manager.discardOnePlayingIfNeeded();
innerAudioContext.volume = volume;
innerAudioContext.play();
AudioPlayerRuntime._manager.addPlaying(innerAudioContext);
innerAudioContext.onEnded(() => {
AudioPlayerRuntime._manager.removePlaying(innerAudioContext);
});
});
}
getCurrentTime () {
if (this._state !== PlayingState.PLAYING) { return this._offset / 1000; }
let current = (performance.now() - this._startTime + this._offset) / 1000;
if (current > this._duration) {
if (!this._loop) return 0;
current -= this._duration; this._startTime += this._duration * 1000;
}
return current;
}
setCurrentTime (val) {
if (!this._nativeAudio) { return; }
this._offset = cc.math.clamp(val, 0, this._duration) * 1000;
this._startTime = performance.now();
this._nativeAudio.seek(val);
}
getVolume () {
return this._volume;
}
setVolume (val, immediate) {
this._volume = val;
if (this._nativeAudio) { this._nativeAudio.volume = val; }
}
getLoop () {
return this._loop;
}
setLoop (val) {
this._loop = val;
if (this._nativeAudio) { this._nativeAudio.loop = val; }
}
destroy () {
if (this._nativeAudio) { this._nativeAudio.destroy(); }
super.destroy();
}
}
}
@@ -48,8 +48,18 @@ function handleZip(url, options, onComplete) {
}
}
function downloadDomAudio(url, options, onComplete) {
onComplete(null, url);
function loadAudioPlayer (url, options, onComplete) {
cc.AudioPlayer.load(url).then(player => {
const audioMeta = {
url,
duration: player.duration,
type: player.type,
};
player.destroy();
onComplete(null, audioMeta);
}).catch(err => {
onComplete(err);
});
}
function download(url, func, options, onFileProgress, onComplete) {
@@ -242,7 +252,6 @@ let parsePlist = function (url, options, onComplete) {
});
};
downloader.downloadDomAudio = downloadDomAudio;
downloader.downloadScript = downloadScript;
parser.parsePVRTex = parsePVRTex;
parser.parsePKMTex = parsePKMTex;
@@ -334,10 +343,10 @@ parser.register({
'.ttc': loadFont,
// Audio
'.mp3': downloadDomAudio,
'.ogg': downloadDomAudio,
'.wav': downloadDomAudio,
'.m4a': downloadDomAudio,
'.mp3': loadAudioPlayer,
'.ogg': loadAudioPlayer,
'.wav': loadAudioPlayer,
'.m4a': loadAudioPlayer,
// Txt
'.txt': parseText,
@@ -1,49 +0,0 @@
const downloader = cc.assetManager.downloader;
const parser = cc.assetManager.parser;
function loadInnerAudioContext(url) {
return new Promise((resolve, reject) => {
const nativeAudio = ral.createInnerAudioContext();
let timer = setTimeout(() => {
clearEvent();
resolve(nativeAudio);
}, 8000);
function clearEvent() {
nativeAudio.offCanplay(success);
nativeAudio.offError(fail);
}
function success() {
clearEvent();
clearTimeout(timer);
resolve(nativeAudio);
}
function fail() {
clearEvent();
clearTimeout(timer);
reject('failed to load innerAudioContext: ' + err);
}
nativeAudio.onCanplay(success);
nativeAudio.onError(fail);
nativeAudio.src = url;
});
}
function downloadAudio(url, options, onComplete) {
loadInnerAudioContext(url).then(nativeAudio => {
onComplete(null, nativeAudio);
}, err => {
onComplete(new Error(err));
});
}
parser.register({
// Audio
'.mp3': downloadAudio,
'.ogg': downloadAudio,
'.wav': downloadAudio,
'.m4a': downloadAudio,
});
module.exports = {
loadInnerAudioContext
};
-2
View File
@@ -27,5 +27,3 @@ require('./canvas');
require('./asset-manager.js');
require('./EditBox.js');
require('./game.js');
require('./download-audio');
require('./AudioPlayer');