265 lines
6.2 KiB
JavaScript
265 lines
6.2 KiB
JavaScript
/******************************************************************
|
|
* Copyright(C) 2019 - 2020 Nx Studio
|
|
*
|
|
* 声音管理
|
|
*
|
|
* 2018.03.30
|
|
******************************************************************/
|
|
|
|
const NxMod = require( "nx.mod" );
|
|
|
|
var nxAudios = cc.Class( {
|
|
|
|
extends: NxMod,
|
|
name: "nxAudios",
|
|
|
|
// 初始化
|
|
initialize: function( _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() {
|
|
|
|
// 服务注销
|
|
nx.audio = null;
|
|
|
|
// USPER
|
|
return this._super();
|
|
},
|
|
|
|
// ================================================
|
|
// 音乐相关
|
|
// ================================================
|
|
|
|
// 播放背景音乐
|
|
playBGM: function( _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, ( _error, _clip ) => {
|
|
|
|
// 播放
|
|
if( _clip ) {
|
|
cc.audioEngine.playMusic( _clip, true );
|
|
return;
|
|
}
|
|
|
|
} );
|
|
|
|
return this.bgmRes;
|
|
},
|
|
|
|
// 停止背景音乐
|
|
stopBGM: function() {
|
|
|
|
cc.audioEngine.stopMusic();
|
|
this.bgmRes = "";
|
|
},
|
|
|
|
// 获取背景音量
|
|
getBGMVolume: function() {
|
|
return this.vget( "BgmVolume" );
|
|
},
|
|
|
|
// 设置背景音量
|
|
setBGMVolume: function( _vol, _fading = false ) {
|
|
|
|
let vol = parseFloat( _vol ) || 0;
|
|
cc.audioEngine.setMusicVolume( vol );
|
|
|
|
if( !_fading ) {
|
|
nx.storage.set( "LKVolumeBG", vol );
|
|
this.vset( "BgmVolume", vol );
|
|
}
|
|
},
|
|
|
|
// ================================================
|
|
// 音效相关
|
|
// ================================================
|
|
|
|
// 播放音效
|
|
// @ 路径直传 OR cc.AudioClip对象
|
|
playSFX: function( _path, _loop, _cb ) {
|
|
|
|
// Clip直接播放
|
|
if( _path instanceof cc.AudioClip ) {
|
|
let 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, ( _error, _clip ) => {
|
|
|
|
// 读取失败返回
|
|
if( _error ) {
|
|
nx.dt.fnInvoke( _cb, _error );
|
|
return;
|
|
}
|
|
|
|
// 播放
|
|
let id = cc.audioEngine.playEffect( _clip, !!_loop );
|
|
nx.dt.fnInvoke( _cb, null, id, _path );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
// 停止音效
|
|
stopSFX: function( _handle ) {
|
|
cc.audioEngine.stopEffect( _handle );
|
|
},
|
|
|
|
// 音效是否在播放
|
|
// {ERROR: -1, INITIALZING: 0, PLAYING: 1, PAUSED: 2, STOPPED: 3}
|
|
isSFXPlaying: function( _handle ) {
|
|
let state = cc.audioEngine.getState(_handle );
|
|
return state == 1;
|
|
},
|
|
|
|
// 停止所有音效
|
|
stopSFXAll: function() {
|
|
cc.audioEngine.stopAllEffects();
|
|
},
|
|
|
|
// 获取音效音量
|
|
getSFXVolume: function() {
|
|
return this.vget( "SfxVolume" );
|
|
},
|
|
|
|
// 设置音效音量
|
|
setSFXVolume: function( _vol ) {
|
|
|
|
let vol = parseFloat( _vol ) || 0;
|
|
nx.storage.set( "LKVolumeSFX", vol );
|
|
this.vset( "SfxVolume", vol );
|
|
cc.audioEngine.setEffectsVolume( vol );
|
|
},
|
|
|
|
// ================================================
|
|
// 语音相关
|
|
// ================================================
|
|
|
|
// 播放语音
|
|
// @ 路径直传 OR cc.AudioClip对象
|
|
playVoice: function( _path, _cb ) {
|
|
|
|
// Clip直接播放
|
|
if( _path instanceof cc.AudioClip ) {
|
|
let 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, ( _error, _clip ) => {
|
|
|
|
// 读取失败返回
|
|
if( _error ) {
|
|
nx.dt.fnInvoke( _cb, _error );
|
|
return;
|
|
}
|
|
|
|
// 播放
|
|
let id = cc.audioEngine.playEffect( _clip, false );
|
|
cc.audioEngine.setVolume( id, this.getVoiceVolume() );
|
|
nx.dt.fnInvoke( _cb, null, id, _path );
|
|
|
|
} );
|
|
|
|
},
|
|
|
|
// 停止语音
|
|
stopVoice: function( _handle ) {
|
|
cc.audioEngine.stopEffect( _handle );
|
|
},
|
|
|
|
// 获取语音音量
|
|
getVoiceVolume: function() {
|
|
return this.vget( "VocVolume" );
|
|
},
|
|
|
|
// 设置语音音量
|
|
setVoiceVolume: function( _vol ) {
|
|
|
|
let vol = parseFloat( _vol ) || 0;
|
|
this.vset( "VocVolume", vol );
|
|
nx.storage.set( "LKVolumeVOC", vol );
|
|
|
|
},
|
|
|
|
// ================================================
|
|
// 总控制
|
|
// ================================================
|
|
|
|
// 暂停
|
|
pauseAll: function() {
|
|
nx.debug( "声音暂停" );
|
|
cc.audioEngine.pauseAll();
|
|
},
|
|
|
|
// 恢复
|
|
resumeAll: function() {
|
|
nx.debug( "声音恢复" );
|
|
cc.audioEngine.resumeAll();
|
|
},
|
|
|
|
// 全部关闭
|
|
stopAll: function() {
|
|
nx.debug( "声音全部关闭" );
|
|
cc.audioEngine.stopAll();
|
|
this.bgmRes = "";
|
|
},
|
|
|
|
} );
|
|
|
|
module.exports = nxAudios;
|