@cc-component/cc-core 1.7.1 → 1.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/assets/core/home/AudioUtil.ts +54 -23
- package/package.json +1 -1
|
@@ -85,6 +85,36 @@ export class AudioUtil {
|
|
|
85
85
|
}
|
|
86
86
|
|
|
87
87
|
// ==================== 背景音乐控制 ====================
|
|
88
|
+
/**
|
|
89
|
+
|
|
90
|
+
// 播放音频资源,根据传入的状态参数决定播放类型(背景音乐或音效)。
|
|
91
|
+
// @param params - 播放参数对象
|
|
92
|
+
// @param params.path - 音频资源的路径
|
|
93
|
+
// @param params.bundle - 资源包标识符
|
|
94
|
+
// @param params.status - 播放类型状态码:1.表示背景音乐,2.表示音效,3.表示可控音效
|
|
95
|
+
// @param params.loop - 是否循环播放(仅对背景音乐有效),可选
|
|
96
|
+
// @param params.volume - 播放音量(0.0 到 1.0 之间),可选
|
|
97
|
+
// @param params.complete - 播放完成后的回调函数,可选 */
|
|
98
|
+
// async play(params: {
|
|
99
|
+
// path: string;
|
|
100
|
+
// bundle: string;
|
|
101
|
+
// status: number;
|
|
102
|
+
// loop?: boolean;
|
|
103
|
+
// volume?: number;
|
|
104
|
+
// complete?: () => void;
|
|
105
|
+
// }) {
|
|
106
|
+
// switch (params.status) {
|
|
107
|
+
// case 1:
|
|
108
|
+
// this.playMusic(params);
|
|
109
|
+
// break;
|
|
110
|
+
// case 2:
|
|
111
|
+
// this.playShot(params);
|
|
112
|
+
// break;
|
|
113
|
+
// case 3:
|
|
114
|
+
// this.playShot(params);
|
|
115
|
+
// break;
|
|
116
|
+
// }
|
|
117
|
+
// }
|
|
88
118
|
/**
|
|
89
119
|
* 播放背景音乐(支持完成回调)
|
|
90
120
|
* @param params.path 音频路径
|
|
@@ -93,7 +123,7 @@ export class AudioUtil {
|
|
|
93
123
|
* @param params.volume 音量(默认使用 music_volume)
|
|
94
124
|
* @param params.callback 播放完成回调(仅在 loop=false 时有效)
|
|
95
125
|
*/
|
|
96
|
-
async
|
|
126
|
+
async playMusic(params: {
|
|
97
127
|
path: string;
|
|
98
128
|
bundle?: string;
|
|
99
129
|
loop?: boolean;
|
|
@@ -110,7 +140,6 @@ export class AudioUtil {
|
|
|
110
140
|
|
|
111
141
|
// 设置完成回调(仅非循环时有意义)
|
|
112
142
|
this._musicCompleteCallback = (!params.loop && params.complete) ? params.complete : undefined;
|
|
113
|
-
|
|
114
143
|
this._audioSource.play();
|
|
115
144
|
} catch (e) {
|
|
116
145
|
console.error(`[AudioUtil] Failed to load or play music: ${params.path}`, e);
|
|
@@ -131,31 +160,33 @@ export class AudioUtil {
|
|
|
131
160
|
}
|
|
132
161
|
|
|
133
162
|
// ==================== 音效播放 ====================
|
|
134
|
-
/**
|
|
135
|
-
async
|
|
163
|
+
/** 播放一次性音效 */
|
|
164
|
+
async playEffects(params: {
|
|
136
165
|
path: string;
|
|
137
|
-
bundle
|
|
166
|
+
bundle: string;
|
|
138
167
|
volume?: number;
|
|
139
|
-
isMusic?: boolean;
|
|
140
168
|
complete?: () => void;
|
|
141
169
|
}) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
170
|
+
const clip = await this.loadAudioClip(params.bundle, params.path);
|
|
171
|
+
const volume = params.volume ?? this.sound_volume;
|
|
172
|
+
this._audioSource.playOneShot(clip, volume);
|
|
173
|
+
}
|
|
174
|
+
/**播放一次性音效--可控制 */
|
|
175
|
+
async playEffectsAudio(params: {
|
|
176
|
+
path: string;
|
|
177
|
+
bundle: string;
|
|
178
|
+
volume?: number;
|
|
179
|
+
complete?: () => void;
|
|
180
|
+
}) {
|
|
181
|
+
const clip = await this.loadAudioClip(params.bundle, params.path);
|
|
182
|
+
const volume = params.volume ?? this.sound_volume;
|
|
183
|
+
this.audioSourceShot.stop(); // 停止当前播放
|
|
184
|
+
this.audioSourceShot.clip = clip;
|
|
185
|
+
this.audioSourceShot.loop = false;
|
|
186
|
+
this.audioSourceShot.volume = volume
|
|
187
|
+
// 设置完成回调(仅非循环时有意义)
|
|
188
|
+
this._musicCompleteCallbackShot = (params.complete) ? params.complete : undefined;
|
|
189
|
+
this.audioSourceShot.play();
|
|
159
190
|
}
|
|
160
191
|
|
|
161
192
|
|