由于当前cocos creator音频部分的文档还没有, 这儿简单介绍一下音频部分.

  • 普通的音频的组件是cc.AudioSource, 可以附加在一个node节点上, 同时只能播放一个音频, 添加组件后, 需要将音频拖放到Clip一栏里面(属性为audio-clip).

组件可以进行的操作包括播放/暂停/停止/继续

audioSource.play()
audioSource.pause()
audioSource.stop()
audioSource.resume()

可以获取播放进度和总长度

audioSource.getCurrentTime()  //单位为秒的浮点数
audioSource.getDuration()  //同样是单位为秒的浮点数

从属性浏览器中可以发现还有如下属性:
clip
volume
mute
loop
playOnLoad
preload

  • 使用音频引擎

引擎是一个cc下的对象cc.audioEngine来操作的.

引擎并不需要去操作一个node对象, 而是直接操作一个资源管理器里面的源文件.所以在定义cc.Class的时候与audioSource不同. 以url的方式定义, 音乐文件(很可能)需要放在resources目录中.

// 在操作audioSource的AudioSourceControl.js文件中, 定义属性为cc.AudioSource类型. 然后, 从资源管理器中拖动一个音乐文件到audiosource节点的clip上. 再从层级管理器中拖动audiosource节点到另一个节点的AudioSourceControl.js属性的audioSource上
cc.Class({
    properties: {
        audioSource: {
            type: cc.AudioSource,
            default: null
        },
    // 省略其他代码
})

// 在操作audioEngine的AudioEngineControl.js文件中, 定义**url**为cc.AudioClip类型. 然后, 从资源管理器中拖动一个音乐文件到使用此AudioEngineControl.js的节点的audio属性上.
// 从示例代码中来看, 这个文件是放在resources目录中的,也就是说, 需要动态加载的.
cc.Class({
    properties: {
        audio: {
            url: cc.AudioClip,
            default: null
        },
    // 省略其他代码
})

使用引擎播放, 每次会产生一个audio id, 记住这个id, 用来在后面对该播放中的音乐进行操作.

// 播放,暂停,继续, 停止
var id = cc.audioEngine.play(this.audio, false, 1);  //参数分别为filepath, loop, volume. filepath示例为res/raw-assets/resources/audio/music_logo.mp3, 如果是拖进来的就不用管这个了.
cc.audioEngine.pause(id)
cc.audioEngine.resume(id)
cc.audioEngine.stop(id)

对所有的播放中音乐统一操作:

        cc.audioEngine.stopAll();
        cc.audioEngine.pauseAll();
        cc.audioEngine.resumeAll();

获取各种状态

cc.audioEngine.getCurrentTime(id) // 秒数, 有对应set方法
cc.audioEngine.getDuration(id)  // 秒数
cc.audioEngine.getStatus(id)  // 返回值-1: 停止状态, 1: 播放状态, 2: 暂停状态.
cc.audioEngine.getVolume(id) // 有对应set方法

  • 下面放出cocos creator的示例代码, 先是AudioSourceControl
cc.Class({
    extends: cc.Component,

    properties: {
        audioSource: {
            type: cc.AudioSource,
            default: null
        },

        label: {
            type: cc.Label,
            default: null
        }
    },

    // use this for initialization
    onLoad: function () {
        // cc.audioEngine.setMaxWebAudioSize(1024*10);
    },

    update: function () {
        if (!this.label) {
            return;
        }
        var audio = this.audioSource;
        this.label.string = audio.getCurrentTime().toFixed(1) + ' s / ' + audio.getDuration().toFixed(1) + ' s';
    },

    play: function () {
        this.audioSource.play();
    },

    pause: function () {
        this.audioSource.pause();
    },

    stop: function () {
        this.audioSource.stop();
    },

    resume: function () {
        this.audioSource.resume();
    }
});

  • 再是audioEngineControl
cc.Class({
    extends: cc.Component,

    properties: {
        audio: {
            url: cc.AudioClip,
            default: null
        },

        label: {
            type: cc.Label,
            default: null
        }
    },

    onLoad: function () {
        this.maxNum = cc.audioEngine.getMaxAudioInstance();
        this.audioPool = [];

        // check deprecated
        ['playMusic', 'playEffect'].forEach(function (name) {
            if (!cc.audioEngine[name]) {
                cc.warn('.' + name + ' is not found!');
            }
        });
    },

    update: function () {
        if (!this.label) return;
        for (var i=0; i<this.audioPool.length; i++) {
            var id = this.audioPool[i];
            var state = cc.audioEngine.getState(id);
            if (state < 0) {
                this.audioPool.splice(i, 1);
                i--;
            }
        }
        this.label.string = 'Instance: ' + this.audioPool.length + ' / ' + this.maxNum;
    },

    play: function () {
        if (!this.audio) return;
        var id = cc.audioEngine.play(this.audio, false, 1);
        this.audioPool.push(id);
    },

    stopAll: function () {
        if (!this.audio) return;
        cc.audioEngine.stopAll();
    },

    pauseAll: function () {
        if (!this.audio) return;
        cc.audioEngine.pauseAll();
    },

    resumeAll: function () {
        if (!this.audio) return;
        cc.audioEngine.resumeAll();
    },
});

标签: none 阅读量: 1378

添加新评论