123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- System.register(["cc"], function (_export, _context) {
- "use strict";
- var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, assetManager, AudioClip, AudioSource, director, Node, ch_audio, _crd, ch_log, ch_storage, loadType;
- _export("default", void 0);
- return {
- setters: [function (_cc) {
- _cclegacy = _cc.cclegacy;
- __checkObsolete__ = _cc.__checkObsolete__;
- __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
- assetManager = _cc.assetManager;
- AudioClip = _cc.AudioClip;
- AudioSource = _cc.AudioSource;
- director = _cc.director;
- Node = _cc.Node;
- }],
- execute: function () {
- _crd = true;
- _cclegacy._RF.push({}, "e1803S2AupKB5LQvJ7w31/E", "audio", undefined);
- __checkObsolete__(['assetManager', 'AudioClip', 'AudioSource', 'director', 'Node']);
- ch_log = chsdk.log;
- ch_storage = chsdk.storage;
- /**音频等资源加载方式*/
- _export("loadType", loadType = /*#__PURE__*/function (loadType) {
- loadType[loadType["none"] = 0] = "none";
- loadType[loadType["bundle"] = 1] = "bundle";
- loadType[loadType["remote"] = 2] = "remote";
- return loadType;
- }({}));
- /**音频播放控制
- * 需要初始化资源加载方式
- * loadType.bundle 时需要设置 bundle名
- * loadType.remote 测试使用远程的音频需要设置远程地址
- */
- _export("default", ch_audio = class ch_audio {
- static getInstance() {
- if (!this._instance) this._instance = new ch_audio();
- return this._instance;
- }
- constructor() {
- this._volume_music = 1;
- this._volume_effect = 1;
- this._switch_music = true;
- this._switch_effect = true;
- this._effect_max = 5;
- this._effect_index = 0;
- this._effect_source_pool = [];
- this._music_source = void 0;
- this._load_type = loadType.none;
- this._bundle_name = void 0;
- this._remote_url = void 0;
- this._playing_sound = new Set();
- var audio = new Node();
- audio.name = '__ch_audio__';
- director.getScene().addChild(audio);
- director.addPersistRootNode(audio);
- this._music_source = this._create(audio);
- for (var i = 0; i < this._effect_max; i++) {
- this._effect_source_pool.push(this._create(audio));
- }
- this.load();
- }
- /**
- * 创建音频源
- * @param node 节点
- * @param volume 音量
- * @returns AudioSource 音频源组件
- */
- _create(node) {
- var source = node.addComponent(AudioSource);
- source.loop = false;
- source.playOnAwake = false;
- source.volume = 0.5;
- return source;
- }
- /**初始化*/
- init(load_type, bundle_name, remote_url) {
- this._load_type = load_type;
- this._bundle_name = bundle_name;
- this._remote_url = remote_url;
- }
- /**切换bundle*/
- set_bundle_name(bundle_name) {
- this._bundle_name = bundle_name;
- }
- /**
- * 释放通过 [[load]] 或者 [[loadDir]] 加载的声音资源。
- * @param sound 声音资源路径
- */
- release(sound) {
- if (this._load_type == loadType.none) {
- ch_log.warn('音频模块未初始化');
- } else if (this._load_type == loadType.bundle) {
- var bundle = assetManager.getBundle(this._bundle_name);
- if (!sound) {
- bundle.releaseAll();
- } else {
- bundle.release(sound, AudioClip);
- }
- }
- }
- /** 保存音乐音效的音量、开关配置数据到本地 */
- save() {
- var local_data = {};
- local_data.volume_music = this._volume_music;
- local_data.volume_effect = this._volume_effect;
- local_data.switch_music = this._switch_music;
- local_data.switch_effect = this._switch_effect;
- ch_storage.set("ch_audio", local_data);
- }
- /** 本地加载音乐音效的音量、开关配置数据并设置到游戏中 */
- load() {
- var local_data = ch_storage.getObject("ch_audio");
- if (local_data) {
- try {
- this.setState(local_data);
- } catch (e) {
- this.setStateDefault();
- }
- } else {
- this.setStateDefault();
- }
- }
- setState(local_data) {
- this.volumeMusic = local_data.volume_music;
- this.volumeEffect = local_data.volume_effect;
- this.switchMusic = local_data.switch_music;
- this.switchEffect = local_data.switch_effect;
- }
- setStateDefault() {
- this.volumeMusic = 0.8;
- this.volumeEffect = 0.8;
- this.switchMusic = true;
- this.switchEffect = true;
- }
- /**
- * 获取背景音乐音量
- */
- get volumeMusic() {
- return this._volume_music;
- }
- /**
- * 设置背景音乐音量
- * @param value 音乐音量值
- */
- set volumeMusic(value) {
- this._volume_music = value;
- this._music_source.volume = value;
- }
- /**
- * 获取背景音乐开关值
- */
- get switchMusic() {
- return this._switch_music;
- }
- /**
- * 设置背景音乐开关值
- * @param value 开关值
- */
- set switchMusic(value) {
- this._switch_music = value;
- if (value == false) this._music_source.stop();
- }
- /**
- * 获取音效音量
- */
- get volumeEffect() {
- return this._volume_effect;
- }
- /**
- * 设置获取音效音量
- * @param value 音效音量值
- */
- set volumeEffect(value) {
- this._volume_effect = value;
- for (var i = 0; i < this._effect_source_pool.length; i++) {
- this._effect_source_pool[i].volume = this._volume_effect;
- }
- }
- /**
- * 获取音效开关值
- */
- get switchEffect() {
- return this._switch_effect;
- }
- /**
- * 设置音效开关值
- * @param value 音效开关值
- */
- set switchEffect(value) {
- this._switch_effect = value;
- if (value == false) {
- for (var i = 0; i < this._effect_source_pool.length; i++) {
- this._effect_source_pool[i].stop();
- }
- }
- }
- /**
- * @en
- * play short audio, such as strikes,explosions
- * @zh
- * 播放短音频,比如 打击音效,爆炸音效等
- * @param sound clip or url for the audio
- * @param interval 同名字音频限制播放间隔(毫秒) (默认:0不限制 特殊系数:>0 <=1 使用音频时间X此系数)
- */
- playOneShot(sound, interval, remote_ext) {
- if (interval === void 0) {
- interval = 0;
- }
- if (remote_ext === void 0) {
- remote_ext = '.mp3';
- }
- if (!this._switch_effect) return;
- if (sound instanceof AudioClip) {
- this.doPlayOneShot(sound, interval);
- } else {
- if (this._load_type == loadType.none) {
- ch_log.warn('音频模块未初始化');
- } else if (this._load_type == loadType.bundle) {
- var bundle = assetManager.getBundle(this._bundle_name);
- if (!bundle) {
- ch_log.warn("\u8BF7\u786E\u4FDD bundle" + this._bundle_name + " \u5DF2\u52A0\u8F7D");
- } else {
- bundle.load(sound, (err, clip) => {
- if (err) {
- ch_log.error(err);
- } else {
- this.doPlayOneShot(clip, interval);
- }
- });
- }
- } else if (this._load_type == loadType.remote) {
- assetManager.loadRemote(this._remote_url + sound + remote_ext, (err, clip) => {
- if (err) {
- ch_log.error(err);
- } else {
- this.doPlayOneShot(clip, interval);
- }
- });
- }
- }
- }
- doPlayOneShot(clip, interval) {
- var name = clip.name;
- if (interval > 0) {
- if (this._playing_sound.has(name)) return;
- this._playing_sound.add(name);
- var time = interval <= 1 ? clip.getDuration() * interval * 1000 : interval;
- setTimeout(() => {
- this._playing_sound.delete(name);
- }, time);
- this.getNextEffectSource().playOneShot(clip, this._volume_effect);
- } else {
- this.getNextEffectSource().playOneShot(clip, this._volume_effect);
- }
- }
- getNextEffectSource() {
- var source = this._effect_source_pool[this._effect_index];
- this._effect_index = (this._effect_index + 1) % this._effect_max;
- return source;
- }
- /**
- * @en
- * play long audio, such as the bg music
- * @zh
- * 播放长音频,比如 背景音乐
- * @param sound clip or url for the sound
- */
- play(sound, remote_ext) {
- if (remote_ext === void 0) {
- remote_ext = '.mp3';
- }
- if (!this._switch_music) return;
- if (sound instanceof AudioClip) {
- this._music_source.loop = true;
- this._music_source.stop();
- this._music_source.clip = sound;
- this._music_source.play();
- this._music_source.volume = this._volume_music;
- } else {
- if (this._load_type == loadType.none) {
- ch_log.warn('音频模块未初始化');
- } else if (this._load_type == loadType.bundle) {
- var bundle = assetManager.getBundle(this._bundle_name);
- if (!bundle) {
- ch_log.warn("\u8BF7\u786E\u4FDD bundle" + this._bundle_name + " \u5DF2\u52A0\u8F7D");
- } else {
- bundle.load(sound, (err, clip) => {
- if (err) {
- ch_log.error(err);
- } else {
- this._music_source.loop = true;
- this._music_source.stop();
- this._music_source.clip = clip;
- this._music_source.play();
- this._music_source.volume = this._volume_music;
- }
- });
- }
- } else if (this._load_type == loadType.remote) {
- assetManager.loadRemote(this._remote_url + sound + remote_ext, (err, clip) => {
- if (err) {
- ch_log.error(err);
- } else {
- this._music_source.loop = true;
- this._music_source.stop();
- this._music_source.clip = clip;
- this._music_source.play();
- this._music_source.volume = this._volume_music;
- }
- });
- }
- }
- }
- /**
- * stop the audio play
- */
- stop() {
- this._music_source.stop();
- for (var i = 0; i < this._effect_source_pool.length; i++) {
- this._effect_source_pool[i].stop();
- }
- }
- /**stop and clean */
- clean() {
- var _this$_music_source$c;
- this._music_source.stop();
- (_this$_music_source$c = this._music_source.clip) == null || _this$_music_source$c.name;
- this._music_source.clip = null;
- for (var i = 0; i < this._effect_source_pool.length; i++) {
- this._effect_source_pool[i].stop();
- this._effect_source_pool[i].clip = null;
- }
- }
- /**
- * pause the audio play
- */
- pause() {
- this._music_source.pause();
- }
- /**
- * resume the audio play
- */
- resume() {
- if (!this._switch_music) return;
- this._music_source.play();
- }
- /** 重播当前音乐 */
- replay_music() {
- this._music_source.stop();
- this._music_source.play();
- }
- });
- ch_audio._instance = void 0;
- _cclegacy._RF.pop();
- _crd = false;
- }
- };
- });
- //# sourceMappingURL=e908646c1435d653de6447e2ae846c959ac29b33.js.map
|