mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-01 07:42:38 +08:00
[v3.8.6] Optimize code size: audio (#18198)
* [v3.8.6] Optimize code size: mangle AudioBufferManager since it's not exported.
This commit is contained in:
@@ -2195,6 +2195,14 @@ browser don't support web audio
|
|||||||
|
|
||||||
This feature supports WebGL render mode only.
|
This feature supports WebGL render mode only.
|
||||||
|
|
||||||
|
### 5203
|
||||||
|
|
||||||
|
Audio buffer cache %s has not been added.
|
||||||
|
|
||||||
|
### 5204
|
||||||
|
|
||||||
|
Audio buffer %s has been cached.
|
||||||
|
|
||||||
### 5300
|
### 5300
|
||||||
|
|
||||||
Type of target to deserialize not matched with data: target is %s, data is %s
|
Type of target to deserialize not matched with data: target is %s, data is %s
|
||||||
|
|||||||
@@ -74,7 +74,7 @@
|
|||||||
"subMetas": {},
|
"subMetas": {},
|
||||||
"userData": {
|
"userData": {
|
||||||
"gltfIndex": 2,
|
"gltfIndex": 2,
|
||||||
"triangleCount": 72
|
"triangleCount": 92
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"40ece": {
|
"40ece": {
|
||||||
|
|||||||
@@ -22,6 +22,9 @@
|
|||||||
THE SOFTWARE.
|
THE SOFTWARE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { warnID } from '../../cocos/core/platform/debug';
|
||||||
|
|
||||||
|
/** @mangle */
|
||||||
interface CachedAudioBufferData {
|
interface CachedAudioBufferData {
|
||||||
usedCount: number,
|
usedCount: number,
|
||||||
audioBuffer: AudioBuffer,
|
audioBuffer: AudioBuffer,
|
||||||
@@ -33,6 +36,7 @@ interface AudioBufferDataMap {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* This is a manager to manage the cache of audio buffer for web audio.
|
* This is a manager to manage the cache of audio buffer for web audio.
|
||||||
|
* @mangle
|
||||||
*/
|
*/
|
||||||
class AudioBufferManager {
|
class AudioBufferManager {
|
||||||
private _audioBufferDataMap: AudioBufferDataMap = {};
|
private _audioBufferDataMap: AudioBufferDataMap = {};
|
||||||
@@ -40,7 +44,7 @@ class AudioBufferManager {
|
|||||||
public addCache (url: string, audioBuffer: AudioBuffer): void {
|
public addCache (url: string, audioBuffer: AudioBuffer): void {
|
||||||
const audioBufferData = this._audioBufferDataMap[url];
|
const audioBufferData = this._audioBufferDataMap[url];
|
||||||
if (audioBufferData) {
|
if (audioBufferData) {
|
||||||
console.warn(`Audio buffer ${url} has been cached`);
|
warnID(5204, url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this._audioBufferDataMap[url] = {
|
this._audioBufferDataMap[url] = {
|
||||||
@@ -52,7 +56,7 @@ class AudioBufferManager {
|
|||||||
public retainCache (url: string): void {
|
public retainCache (url: string): void {
|
||||||
const audioBufferData = this._audioBufferDataMap[url];
|
const audioBufferData = this._audioBufferDataMap[url];
|
||||||
if (!audioBufferData) {
|
if (!audioBufferData) {
|
||||||
console.warn(`Audio buffer cache ${url} has not been added.`);
|
warnID(5203, url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
audioBufferData.usedCount++;
|
audioBufferData.usedCount++;
|
||||||
@@ -66,7 +70,7 @@ class AudioBufferManager {
|
|||||||
public tryReleasingCache (url: string): void {
|
public tryReleasingCache (url: string): void {
|
||||||
const audioBufferData = this._audioBufferDataMap[url];
|
const audioBufferData = this._audioBufferDataMap[url];
|
||||||
if (!audioBufferData) {
|
if (!audioBufferData) {
|
||||||
console.warn(`Audio buffer cache ${url} has not been added.`);
|
warnID(5203, url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (--audioBufferData.usedCount <= 0) {
|
if (--audioBufferData.usedCount <= 0) {
|
||||||
|
|||||||
@@ -32,7 +32,7 @@ import { enqueueOperation, OperationInfo, OperationQueueable } from '../operatio
|
|||||||
import { OS } from '../../system-info/enum-type';
|
import { OS } from '../../system-info/enum-type';
|
||||||
|
|
||||||
export class OneShotAudioMinigame {
|
export class OneShotAudioMinigame {
|
||||||
private _innerAudioContext: InnerAudioContext;
|
private declare _innerAudioContext: InnerAudioContext;
|
||||||
private _onPlayCb?: () => void;
|
private _onPlayCb?: () => void;
|
||||||
get onPlay (): (() => void) | undefined {
|
get onPlay (): (() => void) | undefined {
|
||||||
return this._onPlayCb;
|
return this._onPlayCb;
|
||||||
@@ -92,17 +92,17 @@ export class OneShotAudioMinigame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class AudioPlayerMinigame implements OperationQueueable {
|
export class AudioPlayerMinigame implements OperationQueueable {
|
||||||
private _innerAudioContext: InnerAudioContext;
|
private declare _innerAudioContext: InnerAudioContext;
|
||||||
private _state: AudioState = AudioState.INIT;
|
private _state: AudioState = AudioState.INIT;
|
||||||
private _cacheTime = 0;
|
private _cacheTime = 0;
|
||||||
private _needSeek = false;
|
private _needSeek = false;
|
||||||
private _seeking = false;
|
private _seeking = false;
|
||||||
|
|
||||||
private _onPlay: () => void;
|
private declare _onPlay: () => void;
|
||||||
private _onPause: () => void;
|
private declare _onPause: () => void;
|
||||||
private _onStop: () => void;
|
private declare _onStop: () => void;
|
||||||
private _onSeeked: () => void;
|
private declare _onSeeked: () => void;
|
||||||
private _onEnded: () => void;
|
private declare _onEnded: () => void;
|
||||||
private _readyToHandleOnShow = false;
|
private _readyToHandleOnShow = false;
|
||||||
|
|
||||||
private _resetSeekCache (): void {
|
private _resetSeekCache (): void {
|
||||||
@@ -123,85 +123,87 @@ export class AudioPlayerMinigame implements OperationQueueable {
|
|||||||
public _operationQueue: OperationInfo[] = [];
|
public _operationQueue: OperationInfo[] = [];
|
||||||
|
|
||||||
constructor (innerAudioContext: InnerAudioContext) {
|
constructor (innerAudioContext: InnerAudioContext) {
|
||||||
this._innerAudioContext = innerAudioContext;
|
const self = this;
|
||||||
this._eventTarget = new EventTarget();
|
self._innerAudioContext = innerAudioContext;
|
||||||
|
|
||||||
// event
|
// event
|
||||||
systemInfo.on('hide', this._onInterruptedBegin, this);
|
systemInfo.on('hide', self._onInterruptedBegin, self);
|
||||||
systemInfo.on('show', this._onInterruptedEnd, this);
|
systemInfo.on('show', self._onInterruptedEnd, self);
|
||||||
const eventTarget = this._eventTarget;
|
const eventTarget = self._eventTarget;
|
||||||
this._onPlay = (): void => {
|
self._onPlay = (): void => {
|
||||||
this._state = AudioState.PLAYING;
|
self._state = AudioState.PLAYING;
|
||||||
eventTarget.emit(AudioEvent.PLAYED);
|
eventTarget.emit(AudioEvent.PLAYED);
|
||||||
if (this._needSeek) {
|
if (self._needSeek) {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
this.seek(this._cacheTime).catch((e) => {});
|
self.seek(self._cacheTime).catch((e) => {});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
innerAudioContext.onPlay(this._onPlay);
|
innerAudioContext.onPlay(self._onPlay);
|
||||||
this._onPause = (): void => {
|
self._onPause = (): void => {
|
||||||
this._state = AudioState.PAUSED;
|
self._state = AudioState.PAUSED;
|
||||||
try {
|
try {
|
||||||
const currentTime = this._innerAudioContext.currentTime;
|
const currentTime = self._innerAudioContext.currentTime;
|
||||||
if (currentTime !== null && currentTime !== undefined) {
|
if (currentTime !== null && currentTime !== undefined) {
|
||||||
this._cacheTime = currentTime;
|
self._cacheTime = currentTime;
|
||||||
}
|
}
|
||||||
} catch {
|
} catch {
|
||||||
// Do nothing, cacheTime is not updated.
|
// Do nothing, cacheTime is not updated.
|
||||||
}
|
}
|
||||||
eventTarget.emit(AudioEvent.PAUSED);
|
eventTarget.emit(AudioEvent.PAUSED);
|
||||||
};
|
};
|
||||||
innerAudioContext.onPause(this._onPause);
|
innerAudioContext.onPause(self._onPause);
|
||||||
this._onStop = (): void => {
|
self._onStop = (): void => {
|
||||||
this._state = AudioState.STOPPED;
|
self._state = AudioState.STOPPED;
|
||||||
// Reset all properties
|
// Reset all properties
|
||||||
this._resetSeekCache();
|
self._resetSeekCache();
|
||||||
eventTarget.emit(AudioEvent.STOPPED);
|
eventTarget.emit(AudioEvent.STOPPED);
|
||||||
if (TAOBAO || TAOBAO_MINIGAME) {
|
if (TAOBAO || TAOBAO_MINIGAME) {
|
||||||
/**Unable to seek again after stop; After stop, regardless of whether the starttime has been set,
|
/**Unable to seek again after stop; After stop, regardless of whether the starttime has been set,
|
||||||
the playback will always start from 0 again**/
|
the playback will always start from 0 again**/
|
||||||
} else {
|
} else {
|
||||||
const currentTime = this._innerAudioContext ? this._innerAudioContext.currentTime : 0;
|
const currentTime = self._innerAudioContext ? self._innerAudioContext.currentTime : 0;
|
||||||
if (currentTime !== 0) {
|
if (currentTime !== 0) {
|
||||||
this._innerAudioContext.seek(0);
|
self._innerAudioContext.seek(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
innerAudioContext.onStop(this._onStop);
|
innerAudioContext.onStop(self._onStop);
|
||||||
this._onSeeked = (): void => {
|
self._onSeeked = (): void => {
|
||||||
eventTarget.emit(AudioEvent.SEEKED);
|
eventTarget.emit(AudioEvent.SEEKED);
|
||||||
this._seeking = false;
|
self._seeking = false;
|
||||||
if (this._needSeek) {
|
if (self._needSeek) {
|
||||||
this._needSeek = false;
|
self._needSeek = false;
|
||||||
if (this._cacheTime.toFixed(2) !== this._innerAudioContext.currentTime.toFixed(2)) {
|
if (self._cacheTime.toFixed(2) !== self._innerAudioContext.currentTime.toFixed(2)) {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
||||||
this.seek(this._cacheTime).catch((e) => {});
|
self.seek(self._cacheTime).catch((e) => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
innerAudioContext.onSeeked(this._onSeeked);
|
innerAudioContext.onSeeked(self._onSeeked);
|
||||||
this._onEnded = (): void => {
|
self._onEnded = (): void => {
|
||||||
this._state = AudioState.INIT;
|
self._state = AudioState.INIT;
|
||||||
this._resetSeekCache();
|
self._resetSeekCache();
|
||||||
eventTarget.emit(AudioEvent.ENDED);
|
eventTarget.emit(AudioEvent.ENDED);
|
||||||
};
|
};
|
||||||
innerAudioContext.onEnded(this._onEnded);
|
innerAudioContext.onEnded(self._onEnded);
|
||||||
}
|
}
|
||||||
destroy (): void {
|
destroy (): void {
|
||||||
systemInfo.off('hide', this._onInterruptedBegin, this);
|
const self = this;
|
||||||
systemInfo.off('show', this._onInterruptedEnd, this);
|
systemInfo.off('hide', self._onInterruptedBegin, self);
|
||||||
if (this._innerAudioContext) {
|
systemInfo.off('show', self._onInterruptedEnd, self);
|
||||||
|
const innerAudioContext = self._innerAudioContext;
|
||||||
|
if (innerAudioContext) {
|
||||||
['Play', 'Pause', 'Stop', 'Seeked', 'Ended'].forEach((event) => {
|
['Play', 'Pause', 'Stop', 'Seeked', 'Ended'].forEach((event) => {
|
||||||
this._offEvent(event);
|
self._offEvent(event);
|
||||||
});
|
});
|
||||||
// NOTE: innerAudioContext might not stop the audio playing, have to call it explicitly.
|
// NOTE: innerAudioContext might not stop the audio playing, have to call it explicitly.
|
||||||
this._innerAudioContext.stop();
|
innerAudioContext.stop();
|
||||||
this._innerAudioContext.destroy();
|
innerAudioContext.destroy();
|
||||||
// NOTE: Type 'null' is not assignable to type 'InnerAudioContext'
|
// NOTE: Type 'null' is not assignable to type 'InnerAudioContext'
|
||||||
this._innerAudioContext = null as any;
|
self._innerAudioContext = null as any;
|
||||||
// Restore the state of the audio, otherwise it will cause 'destroy' to be called first and 'stop' to be called later.
|
// Restore the state of the audio, otherwise it will cause 'destroy' to be called first and 'stop' to be called later.
|
||||||
// this will cause a error.
|
// this will cause a error.
|
||||||
this._state = AudioState.INIT;
|
self._state = AudioState.INIT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private _onInterruptedBegin (): void {
|
private _onInterruptedBegin (): void {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ export interface OperationInfo {
|
|||||||
invoking: boolean,
|
invoking: boolean,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @mangle */
|
||||||
export interface OperationQueueable {
|
export interface OperationQueueable {
|
||||||
_operationQueue: OperationInfo[];
|
_operationQueue: OperationInfo[];
|
||||||
_eventTarget: EventTarget;
|
_eventTarget: EventTarget;
|
||||||
|
|||||||
Reference in New Issue
Block a user