音频的分包与GameComponent相关的统一整理

This commit is contained in:
donggang
2024-07-02 10:52:39 +08:00
parent 43f40d3e69
commit 292eebd640
4 changed files with 91 additions and 55 deletions

View File

@@ -12,10 +12,19 @@ const { ccclass } = _decorator;
* 注用playOneShot播放的音乐效果在播放期间暂时没办法即时关闭音乐
*/
/** 资源加载记录 */
interface ResRecord {
source: boolean;
ac: AudioClip,
bundle?: string,
path?: string
}
/** 游戏音效 */
@ccclass('AudioEffect')
export class AudioEffect extends AudioSource {
private effects: Map<string, AudioClip> = new Map<string, AudioClip>();
private effects: Map<string, ResRecord> = new Map<string, ResRecord>();
private _progress: number = 0;
/** 获取音乐播放进度 */
@@ -38,30 +47,33 @@ export class AudioEffect extends AudioSource {
* @param url 音效资源地址
* @param callback 资源加载完成并开始播放回调
*/
load(url: string | AudioClip, callback?: Function) {
load(url: string | AudioClip, callback?: Function, bundleName?: string) {
if (bundleName == null) bundleName = oops.res.defaultBundleName;
// 资源播放音乐对象
if (url instanceof AudioClip) {
this.effects.set(url.uuid, url);
this.effects.set(url.uuid, { source: true, ac: url });
this.playOneShot(url, this.volume);
callback && callback();
}
else {
// 地址加载音乐资源后播放
if (this.effects.has(url) == false) {
oops.res.load(url, AudioClip, (err: Error | null, data: AudioClip) => {
oops.res.load(bundleName, url, AudioClip, (err: Error | null, data: AudioClip) => {
if (err) {
error(err);
}
this.effects.set(url, data);
let key = `${bundleName}:${url}`;
this.effects.set(key, { source: false, bundle: bundleName, path: url, ac: data });
this.playOneShot(data, this.volume);
callback && callback();
});
}
// 播放缓存中音效
else {
const ac = this.effects.get(url)!;
this.playOneShot(ac, this.volume);
const rr = this.effects.get(url)!;
this.playOneShot(rr.ac, this.volume);
callback && callback();
}
}
@@ -70,7 +82,13 @@ export class AudioEffect extends AudioSource {
/** 释放所有已使用过的音效资源 */
releaseAll() {
for (let key in this.effects) {
oops.res.release(key);
const rr = this.effects.get(key)!;
if (rr.source) {
this.release(rr.ac);
}
else {
this.release(rr.path!, rr.bundle!);
}
}
this.effects.clear();
}
@@ -78,8 +96,11 @@ export class AudioEffect extends AudioSource {
/**
* 释放指定地址音效资源
* @param url 音效资源地址
* @param bundleName 资源所在包名
*/
release(url: string | AudioClip) {
release(url: string | AudioClip, bundleName?: string) {
if (bundleName == null) bundleName = oops.res.defaultBundleName;
var ac: AudioClip | undefined = undefined;
if (url instanceof AudioClip) {
ac = url;
@@ -89,11 +110,12 @@ export class AudioEffect extends AudioSource {
}
}
else {
ac = this.effects.get(url);
if (ac) {
this.effects.delete(url);
oops.res.release(url);
const key = `${bundleName}:${url}`;
const rr = this.effects.get(key);
if (rr) {
this.effects.delete(key);
oops.res.release(rr.path!, rr.bundle!);
}
}
}
}
}

View File

@@ -41,18 +41,18 @@ export class AudioManager extends Component {
* @param url 资源地址
* @param callback 音乐播放完成事件
*/
playMusic(url: string, callback?: Function) {
playMusic(url: string, callback?: Function, bundleName?: string) {
if (this._switch_music && !this.music.playing) {
this.music.loop = false;
this.music.load(url, callback);
this.music.load(url, callback, bundleName);
}
}
/** 循环播放背景音乐 */
playMusicLoop(url: string) {
playMusicLoop(url: string, bundleName?: string) {
if (this._switch_music && !this.music.playing) {
this.music.loop = true;
this.music.load(url);
this.music.load(url, null!, bundleName);
}
}
@@ -113,15 +113,15 @@ export class AudioManager extends Component {
* 播放音效
* @param url 资源地址
*/
playEffect(url: string | AudioClip) {
playEffect(url: string | AudioClip, callback?: Function, bundleName?: string) {
if (this._switch_effect) {
this.effect.load(url);
this.effect.load(url, callback, bundleName);
}
}
/** 释放音效资源 */
releaseEffect(url: string | AudioClip) {
this.effect.release(url);
releaseEffect(url: string | AudioClip, bundleName?: string) {
this.effect.release(url, bundleName);
}
/**

View File

@@ -6,17 +6,22 @@
*/
import { AudioClip, AudioSource, _decorator, error } from 'cc';
import { oops } from '../../Oops';
const { ccclass, menu } = _decorator;
/** 背景音乐 */
/**
* 背景音乐
* 1、播放一个新背景音乐时先加载音乐资源然后停止正在播放的背景资源同时施放当前背景音乐资源最后播放新的背景音乐
*/
@ccclass('AudioMusic')
export class AudioMusic extends AudioSource {
/** 背景音乐播放完成回调 */
onComplete: Function | null = null;
private _progress: number = 0;
private _url: string = null!;
private _isPlay: boolean = false;
private _bundleName: string = null!;
private _url: string = null!;
/** 获取音乐播放进度 */
get progress(): number {
@@ -38,31 +43,33 @@ export class AudioMusic extends AudioSource {
* @param url 音乐资源地址
* @param callback 加载完成回调
*/
load(url: string, callback?: Function) {
load(url: string, callback?: Function, bundleName?: string) {
if (bundleName == null) bundleName = oops.res.defaultBundleName;
if (this._url == null) {
oops.res.load(url, AudioClip, (err: Error | null, data: AudioClip) => {
oops.res.load(bundleName, url, AudioClip, (err: Error | null, data: AudioClip) => {
if (err) {
error(err);
}
// 注:事件定义在这里,是为了在播放前设置初始播放位置数据
callback && callback();
this.playPrepare(url, data);
this.playPrepare(bundleName, url, data);
});
}
else {
this.playPrepare(url, this.clip!);
this.playPrepare(bundleName, url, this.clip!);
}
}
/** 停止音乐播放 */
stop() {
this._bundleName = null!;
this._url = null!;
super.stop();
}
private playPrepare(url: string, data: AudioClip) {
private playPrepare(bundleName: string, url: string, data: AudioClip) {
if (this.playing) {
this._isPlay = false;
this.stop();
@@ -70,7 +77,7 @@ export class AudioMusic extends AudioSource {
if (this._url) {
this.clip = null;
oops.res.release(this._url);
oops.res.release(this._url, this._bundleName);
}
this.enabled = true;
@@ -78,6 +85,8 @@ export class AudioMusic extends AudioSource {
this.play();
// 记录新的资源包与资源名数据
this._bundleName = bundleName;
this._url = url;
}
@@ -100,7 +109,8 @@ export class AudioMusic extends AudioSource {
release() {
if (this._url) {
this.clip = null;
oops.res.release(this._url);
oops.res.release(this._url, this._bundleName);
this._bundleName = null!;
this._url = null!;
}
}

View File

@@ -121,7 +121,6 @@ export class GameComponent extends Component {
//#region 资源加载管理
/** 资源路径 */
private resPathsAudioEffect: Map<string, string> = null!; // 音效类资源
private resPaths: Map<ResType, Map<string, ResRecord>> = null!; // 资源使用记录
/**
@@ -269,43 +268,48 @@ export class GameComponent extends Component {
/** 释放音效资源 */
releaseAudioEffect() {
if (this.resPathsAudioEffect) {
this.resPathsAudioEffect.forEach((value: string, key: string) => {
oops.audio.effect.release(key);
});
this.resPathsAudioEffect.clear();
this.resPathsAudioEffect = null!;
if (this.resPaths) {
var rps = this.resPaths.get(ResType.Audio);
if (rps) {
rps.forEach((value: ResRecord) => {
oops.audio.releaseEffect(value.path, value.bundle);
});
}
}
}
//#endregion
//#region 音频播放管理
/**
* 播放背景音乐
* @param url 资源地址
* 播放背景音乐(不受自动释放资源管理)
* @param url 资源地址
* @param callback 资源加载完成回调
* @param bundleName 资源包名
*/
playMusic(url: string) {
oops.audio.playMusic(url);
playMusic(url: string, callback?: Function, bundleName?: string) {
oops.audio.playMusic(url, callback, bundleName);
}
/**
* 循环播放背景音乐
* @param url 资源地址
* 循环播放背景音乐(不受自动释放资源管理)
* @param url 资源地址
* @param bundleName 资源包名
*/
playMusicLoop(url: string) {
playMusicLoop(url: string, bundleName?: string) {
oops.audio.stopMusic();
oops.audio.playMusicLoop(url);
oops.audio.playMusicLoop(url, bundleName);
}
/**
* 播放音效
* @param url 资源地址
*/
playEffect(url: string) {
if (this.resPathsAudioEffect == null) this.resPathsAudioEffect = new Map<string, string>();
this.resPathsAudioEffect.set(url, oops.res.defaultBundleName);
oops.audio.playEffect(url);
* 播放音效
* @param url 资源地址
* @param callback 资源加载完成回调
* @param bundleName 资源包名
*/
playEffect(url: string, callback?: Function, bundleName?: string) {
if (bundleName == null) bundleName = oops.res.defaultBundleName;
this.addPathToRecord(ResType.Audio, bundleName, url);
oops.audio.playEffect(url, callback, bundleName);
}
//#endregion