Cocos audio
AI 编程助手的专业技能库,涵盖 30+ 技能,按 6 类组织
npx -y skills add morning-start/agent-skills --skill cocos-audioAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 1 stars1 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
What its author says it does
Copied from the file, not written here
Cocos Creator 3.8 音频系统,背景音乐、音效播放、音频管理
SKILL.md
3.8 KB, ~1.1k tokens by cl100k_base, as published. Nobody here has run it
Cocos Creator 3.8 声音系统
任务目标
- 本 Skill 用于:掌握 Cocos Creator 音频系统实现游戏音效
- 能力包含:音频播放、背景音乐、音效管理、音量控制
- 触发条件:添加游戏音效、背景音乐、语音播放
概述
音频是游戏中不可或缺的一部分,好的音乐能让游戏更加真实、富有沉浸感。Cocos Creator 的音频系统支持导入并播放大多数常见的音频文件格式。
音频组件
AudioSource 组件
import { AudioSource } from 'cc';
const audioSource = node.getComponent(AudioSource);
audioSource.clip = audioClip;
audioSource.volume = 0.5;
audioSource.loop = true;
audioSource.playOnLoad = true;
属性说明
- clip:音频资源
- volume:音量(0-1)
- loop:是否循环
- playOnLoad:加载后自动播放
- mute:静音
脚本控制
播放控制
// 播放
audioSource.play();
// 暂停
audioSource.pause();
// 停止
audioSource.stop();
// 恢复
audioSource.resume();
播放状态
if (audioSource.state === AudioSource.State.PLAYING) {
console.log('Audio is playing');
}
动态加载播放
加载音频
import { resources } from 'cc';
resources.load('audio/bgm_main', AudioClip, (err, audioClip) => {
if (!err) {
const audioSource = this.node.addComponent(AudioSource);
audioSource.clip = audioClip;
audioSource.loop = true;
audioSource.play();
}
});
短音效播放
import { resources } from 'cc';
resources.load('audio/sfx_click', AudioClip, (err, clip) => {
if (!err) {
const audioSource = this.node.addComponent(AudioSource);
audioSource.clip = clip;
audioSource.play();
}
});
音频管理
背景音乐管理
class AudioManager {
private bgmSource: AudioSource | null = null;
playBGM(clip: AudioClip, volume: number = 1) {
if (!this.bgmSource) {
this.bgmSource = new AudioSource();
}
this.bgmSource.clip = clip;
this.bgmSource.volume = volume;
this.bgmSource.loop = true;
this.bgmSource.play();
}
stopBGM() {
if (this.bgmSource) {
this.bgmSource.stop();
}
}
setBGMVolume(volume: number) {
if (this.bgmSource) {
this.bgmSource.volume = volume;
}
}
}
音效池管理
class SFXManager {
private sfxPool: AudioSource[] = [];
private sfxClips: Map<string, AudioClip> = new Map();
playSFX(name: string, volume: number = 1) {
const clip = this.sfxClips.get(name);
if (!clip) return;
const source = this.getFreeSource();
if (source) {
source.clip = clip;
source.volume = volume;
source.play();
}
}
private getFreeSource(): AudioSource | null {
for (const source of this.sfxPool) {
if (source.state !== AudioSource.State.PLAYING) {
return source;
}
}
return null;
}
}
加载模式
Web Audio
- 兼容性更好
- 问题较少
- 占用内存较多
DOM Audio
- 每次播放需要用户操作
- 同时只能播放一个音频
- 适合背景音乐
资源索引
必要参考
注意事项
性能优化
- 合理控制同音效数量
- 使用短音效不循环
- 善用静音控制
最佳实践
- 背景音乐与音效分离管理
- 预加载常用音效
- 提供音量设置选项
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.