Files
fc/dev/project/library/imports/bc/bc4a4316-2897-4a85-8d19-ce95ed232af1.js
T

231 lines
5.8 KiB
JavaScript
Raw Normal View History

2026-05-24 10:21:26 +08:00
"use strict";
cc._RF.push(module, 'bc4a4MWKJdKhY0ZzpXtIyrx', 'nx.audio');
// Scripts/nx/kernel/nx.audio.js
"use strict";
/******************************************************************
* Copyright(C) 2019 - 2020 Nx Studio
*
* 声音管理
*
* 2018.03.30
******************************************************************/
var NxMod = require("nx.mod");
var nxAudios = cc.Class({
"extends": NxMod,
name: "nxAudios",
// 初始化
initialize: function initialize(_args) {
// 初始化
this._super(_args);
// 插件
nx.plugin.add(this, ["view"]);
// 视图附着
this.vattach("Frame");
// 服务开启
nx.audio = this;
// 音量默认
this.setBGMVolume(nx.storage.get("LKVolumeBG", 1));
this.setSFXVolume(nx.storage.get("LKVolumeSFX", 1));
this.setVoiceVolume(nx.storage.get("LKVolumeVOC", 1));
this.bgmRes = null;
return true;
},
// 销毁
uninitialize: function uninitialize() {
// 服务注销
nx.audio = null;
// USPER
return this._super();
},
// ================================================
// 音乐相关
// ================================================
// 播放背景音乐
playBGM: function playBGM(_path) {
// 重复播放判断
if (this.bgmRes == _path && cc.audioEngine.isMusicPlaying()) {
return this.bgmRes;
}
// 先停止
this.stopBGM();
// Clip直接播放
if (_path instanceof cc.AudioClip) {
this.bgmRes = _path.name;
cc.audioEngine.playMusic(_path, true);
return this.bgmRes;
}
// 读取Clip
this.bgmRes = _path;
nx.res.loadAudio(_path, function (_error, _clip) {
// 播放
if (_clip) {
cc.audioEngine.playMusic(_clip, true);
return;
}
});
return this.bgmRes;
},
// 停止背景音乐
stopBGM: function stopBGM() {
cc.audioEngine.stopMusic();
this.bgmRes = "";
},
// 获取背景音量
getBGMVolume: function getBGMVolume() {
return this.vget("BgmVolume");
},
// 设置背景音量
setBGMVolume: function setBGMVolume(_vol, _fading) {
if (_fading === void 0) {
_fading = false;
}
var vol = parseFloat(_vol) || 0;
cc.audioEngine.setMusicVolume(vol);
if (!_fading) {
nx.storage.set("LKVolumeBG", vol);
this.vset("BgmVolume", vol);
}
},
// ================================================
// 音效相关
// ================================================
// 播放音效
// @ 路径直传 OR cc.AudioClip对象
playSFX: function playSFX(_path, _loop, _cb) {
// Clip直接播放
if (_path instanceof cc.AudioClip) {
var id = cc.audioEngine.playEffect(_path, !!_loop);
nx.dt.fnInvoke(_cb, null, id);
return;
}
// 参数检查
if (nx.dt.strEmpty(_path)) {
nx.error("[音频管理]音效播放失败,无效路径!");
return;
}
// 播放
nx.res.loadAudio(_path, function (_error, _clip) {
// 读取失败返回
if (_error) {
nx.dt.fnInvoke(_cb, _error);
return;
}
// 播放
var id = cc.audioEngine.playEffect(_clip, !!_loop);
nx.dt.fnInvoke(_cb, null, id, _path);
});
},
// 停止音效
stopSFX: function stopSFX(_handle) {
cc.audioEngine.stopEffect(_handle);
},
// 音效是否在播放
// {ERROR: -1, INITIALZING: 0, PLAYING: 1, PAUSED: 2, STOPPED: 3}
isSFXPlaying: function isSFXPlaying(_handle) {
var state = cc.audioEngine.getState(_handle);
return state == 1;
},
// 停止所有音效
stopSFXAll: function stopSFXAll() {
cc.audioEngine.stopAllEffects();
},
// 获取音效音量
getSFXVolume: function getSFXVolume() {
return this.vget("SfxVolume");
},
// 设置音效音量
setSFXVolume: function setSFXVolume(_vol) {
var vol = parseFloat(_vol) || 0;
nx.storage.set("LKVolumeSFX", vol);
this.vset("SfxVolume", vol);
cc.audioEngine.setEffectsVolume(vol);
},
// ================================================
// 语音相关
// ================================================
// 播放语音
// @ 路径直传 OR cc.AudioClip对象
playVoice: function playVoice(_path, _cb) {
var _this = this;
// Clip直接播放
if (_path instanceof cc.AudioClip) {
var id = cc.audioEngine.playEffect(_path, false);
nx.dt.fnInvoke(_cb, null, id);
return;
}
// 参数检查
if (nx.dt.strEmpty(_path)) {
nx.error("[音频管理]语音播放失败,无效路径!");
return;
}
// 播放
nx.res.loadAudio(_path, function (_error, _clip) {
// 读取失败返回
if (_error) {
nx.dt.fnInvoke(_cb, _error);
return;
}
// 播放
var id = cc.audioEngine.playEffect(_clip, false);
cc.audioEngine.setVolume(id, _this.getVoiceVolume());
nx.dt.fnInvoke(_cb, null, id, _path);
});
},
// 停止语音
stopVoice: function stopVoice(_handle) {
cc.audioEngine.stopEffect(_handle);
},
// 获取语音音量
getVoiceVolume: function getVoiceVolume() {
return this.vget("VocVolume");
},
// 设置语音音量
setVoiceVolume: function setVoiceVolume(_vol) {
var vol = parseFloat(_vol) || 0;
this.vset("VocVolume", vol);
nx.storage.set("LKVolumeVOC", vol);
},
// ================================================
// 总控制
// ================================================
// 暂停
pauseAll: function pauseAll() {
nx.debug("声音暂停");
cc.audioEngine.pauseAll();
},
// 恢复
resumeAll: function resumeAll() {
nx.debug("声音恢复");
cc.audioEngine.resumeAll();
},
// 全部关闭
stopAll: function stopAll() {
nx.debug("声音全部关闭");
cc.audioEngine.stopAll();
this.bgmRes = "";
}
});
module.exports = nxAudios;
cc._RF.pop();