直播网站可以做毕设吗,瑞翔网站建设,百度网站的网址,滁州网站建设哪个好点引擎#xff1a; CocosCreator 3.8.0
环境#xff1a; Mac
Gitee: oops-game-kit 注#xff1a; 作者dgflash的oops-framework框架QQ群#xff1a; 628575875 简介 在CocosCreator中#xff0c;音频的播放主要通过组件: AudioSource 来管理音乐、音效的播放、暂停和停止…引擎 CocosCreator 3.8.0
环境 Mac
Gitee: oops-game-kit 注 作者dgflash的oops-framework框架QQ群 628575875 简介 在CocosCreator中音频的播放主要通过组件: AudioSource 来管理音乐、音效的播放、暂停和停止等功能。
该组件的两个主要接口
play() 用于播放音乐playOneShot() 用于播放音效
CocosCreator官方提供了对应的音频管理器大致原理是
在起始场景中添加根节点该节点下包含组件AudioSource该节点在场景脚本中声明为常驻根节点声音管理器为单例模式在项目初始化中该管理器会获取该组件用于实现对音频的管理
import { AudioSource, Component, game } from cc;
import {audioManager} from manager/audioManager;
const { ccclass, property } _decorator;ccclass(GameRoot)
export class GameRoot extends Component {property(AudioSource) _audioSource: AudioSource null!;onLoad () {// 获取AudioSource组件并添加到管理器中const audioSource this.node.getComponent(AudioSource)!;audioManager.instance.init(audioSource);// 声明常驻根节点该节点不会在场景切换中被销毁game.addPersistRootNode(this.node); }
}更多内容可参考博客 CocosCreator笔记 之 背景音乐 AudioManger 在oops-framework框架中音频管理通过AudioManager来实现的。
该类通过管理框架封装的AudioMusic 和 AudioEffect 来实现音乐和音效的播放、暂停和停止等。
他们的继承结构 #mermaid-svg-b3EVBVehcsKJKOjs {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-b3EVBVehcsKJKOjs .error-icon{fill:#552222;}#mermaid-svg-b3EVBVehcsKJKOjs .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-b3EVBVehcsKJKOjs .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-b3EVBVehcsKJKOjs .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-b3EVBVehcsKJKOjs .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-b3EVBVehcsKJKOjs .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-b3EVBVehcsKJKOjs .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-b3EVBVehcsKJKOjs .marker{fill:#333333;stroke:#333333;}#mermaid-svg-b3EVBVehcsKJKOjs .marker.cross{stroke:#333333;}#mermaid-svg-b3EVBVehcsKJKOjs svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-b3EVBVehcsKJKOjs .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-b3EVBVehcsKJKOjs .cluster-label text{fill:#333;}#mermaid-svg-b3EVBVehcsKJKOjs .cluster-label span{color:#333;}#mermaid-svg-b3EVBVehcsKJKOjs .label text,#mermaid-svg-b3EVBVehcsKJKOjs span{fill:#333;color:#333;}#mermaid-svg-b3EVBVehcsKJKOjs .node rect,#mermaid-svg-b3EVBVehcsKJKOjs .node circle,#mermaid-svg-b3EVBVehcsKJKOjs .node ellipse,#mermaid-svg-b3EVBVehcsKJKOjs .node polygon,#mermaid-svg-b3EVBVehcsKJKOjs .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-b3EVBVehcsKJKOjs .node .label{text-align:center;}#mermaid-svg-b3EVBVehcsKJKOjs .node.clickable{cursor:pointer;}#mermaid-svg-b3EVBVehcsKJKOjs .arrowheadPath{fill:#333333;}#mermaid-svg-b3EVBVehcsKJKOjs .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-b3EVBVehcsKJKOjs .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-b3EVBVehcsKJKOjs .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-b3EVBVehcsKJKOjs .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-b3EVBVehcsKJKOjs .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-b3EVBVehcsKJKOjs .cluster text{fill:#333;}#mermaid-svg-b3EVBVehcsKJKOjs .cluster span{color:#333;}#mermaid-svg-b3EVBVehcsKJKOjs div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-b3EVBVehcsKJKOjs :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 音频管理类 音频组件 音乐 音效 AudioManager Component AudioSource AudioMusic AudioEffect 该管理类提供的主要接口有
参数或接口说明progressMusic设置/获取音乐的播放进度volumeMusic设置/获取音乐的音量范围在[0, 1]之间switchMusic设置/获取音乐开关volumeEffect设置/获取音效音量大小范围在[0, 1]之间switchEffect设置/获取音效开关load()音乐音效的初始化加载并设置音量大小等save()本地存储音乐音效的音量、开关等配置数据playMusic(url, callback?)播放音乐可添加音乐播放完成回调playerMusicLoop(url)循环播放音乐setMusicComplete(callback)设置背景音乐播放完成回调playEffect(url)播放音效resumeAll()恢复当前暂停的音乐与音效播放pauseAll()暂停当前音乐与音效的播放stopAll()停止当前音乐与音效的播放
有几个关键的方法需要注意下
load() 用于音乐音效的初始化
load() {// 获取或添加AudioSource的组件用于音乐和音效的管理this.music this.getComponent(AudioMusic) || this.addComponent(AudioMusic)!;this.effect this.getComponent(AudioEffect) || this.addComponent(AudioEffect)!;// 获取本地存储数据设置音乐音效的音量大小和开关let data oops.storage.get(LOCAL_STORE_KEY);if (data) {try {this.local_data JSON.parse(data);this._volume_music this.local_data.volume_music;this._volume_effect this.local_data.volume_effect;this._switch_music this.local_data.switch_music;this._switch_effect this.local_data.switch_effect;}catch (e) {this.local_data {};this._volume_music 1;this._volume_effect 1;this._switch_music true;this._switch_effect true;}// 根据数据设置声音大小if (this.music) this.music.volume this._volume_music;if (this.effect) this.effect.volume this._volume_effect;}
}playMusic 播放音乐相关
// 播放音乐
playMusic(url: string, callback?: Function) {if (this._switch_music) {this.music.load(url, callback);}
}// AudioMusic.ts 加载音乐并播放
public load(url: string, callback?: Function) {// 根据url进行动态加载oops.res.load(url, AudioClip, (err: Error | null, data: AudioClip) {if (err) {error(err);}// 检测音乐是否播放中如果播放则停止if (this.playing) {this._isPlay false;this.stop();}if (this._url) {oops.res.release(this._url);}// 设置要播放的音频this.enabled true;this.clip data;// 注事件定义在这里是为了在播放前设置初始播放位置数据callback callback();// 开始播放this.play();this._url url;});
}注这段代码主要是为了理解框架对音乐音效的封装先后台加载完成后才进行播放。 示例 在框架中使用音效管理在Oops.ts提供的入口主要是
export class oops {/** 游戏音乐管理 */static audio: AudioManager;
}在程序中使用的主要示例
// 初始化
oops.audio.init();
// 设置音乐的播放
oops.audio.volumeMusic 0.5;
oops.audio.playerMusicLoop(audio/bgm);
// 设置音效的播放
oops.audio.switchEffect true;
oops.audio.playEffect(audio/click);
// 设置暂停
oops.audio.pauseAll();注如果设置页面关闭注意音频的保存记得调用save()接口即可。 感谢作者dgflash的分享作者CSDN博客 dgflash CSDN
最后祝大家学习和生活愉快!