123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- let AudioMgr = {
- /**
- * 初始化
- *
- * @author Pyden
- * @date 2019-03-21
- */
- init () {
- this.currMusic = '';
- this.preloadEffectMap = {};
- },
- /**
- * 播放背景音乐
- *
- * @author libo
- * @date 2019-07-29
- * @param {string} audio 背景音乐名字【不带路径】
- * @param {bool} isLoop 是否循环
- * @returns
- */
- playMusic (audio, isLoop) {
- if (!audio) {
- // G.LogUtils.error('AudioMgr.playMusic 参数错误');
- return;
- }
- audio = 'res_audio/mp3/back_music/' + audio;
- if (this.currMusic == audio) {
- // 背景音乐一样,不重新播放
- return;
- }
- cc.audioEngine.stopMusic();
- this.currMusic = audio;
- cc.loader.loadRes(audio, cc.AudioClip, (err, clip) => {
- if (err) {
- G.LogUtils.log('err', err.stack);
- return;
- }
- if (cc.game.isPaused()) {
- // 游戏暂停不播放音效
- return;
- }
- if (this.currMusic == audio) {
- cc.audioEngine.playMusic(clip, isLoop);
- }
- });
- },
- /**
- * 预加载音效
- *
- *
- *
- * @author Pyden
- * @date 2019-08-08
- * @param {string} audio 背景音乐名字【必须带路径】
- */
- preloadEffect (audio) {
- if (!audio) {
- // G.LogUtils.error('AudioMgr.preloadEffect 参数错误');
- return;
- }
- // 同一个音效仅预加载一次
- if (this.preloadEffectMap[audio]) {
- return;
- }
- this.preloadEffectMap[audio] = true;
- cc.loader.loadRes(audio, cc.AudioClip, (err, clip) => {
- });
- },
- /**
- * 播放音效
- *
- * @author libo
- * @date 2019-07-29
- * @param {string} audio 背景音乐名字【必须带路径】
- * @param {bool} isLoop 是否循环
- * @param {Float} timeoutDuration 超时时长(秒)。默认0.2秒
- * @returns
- */
- playEffect (audio, isLoop, timeoutDuration = 0.2) {
- if (!audio) {
- // G.LogUtils.error('AudioMgr.playEffect 参数错误');
- return;
- }
- setTimeout (() => {
- canPlay = false;
- }, timeoutDuration * 1000);
- cc.loader.loadRes(audio, cc.AudioClip, (err, clip) => {
- if (err) {
- G.LogUtils.log('err', err.stack);
- return;
- }
- if (cc.game.isPaused()) {
- // 游戏暂停不播放音效
- return;
- }
- if (canPlay) {
- cc.audioEngine.playEffect(clip, isLoop);
- }
- });
- }
- };
- module.exports = AudioMgr;
|