1: 掌握cc.AudioSource組件的使用;
cc.AudioSource
1:AudioSource組件是音頻源組件, 發出聲音的源頭;
2: AudioSource組件面板:
clip: 聲源的播放的音頻對象: AudioClip, mp3, wav, ogg,
volume: 音量大小, [0, 1]百分比
mute: 是否靜音;
Loop: 是否循環播放;
Play on Load: 是否在組件加載的時候播放;
Preload: 是否預先加載;
cc.AudioClip對象
1: 音頻剪輯對象,支持的格式有mp3, wav, ogg
2: 可以在編輯器上手動關聯,生成AudioCip對象;
3: 可以通過代碼加載AudioCip; (資源加載詳細講解);
AudioSource代碼使用
1: 代碼中獲得cc.AudioSource組件:
編輯器關聯;
代碼獲取組件;
2: AudioSource 主要的方法:
play(); 播放音頻;
stop(); 停止聲音播放;
pause(); 暫停聲音播放;
resume(); 恢復聲音播放;
rewind(); 重頭開始播放;
其它接口見文檔;
3: AudioSource代碼主要屬性:
loop: 是否循環播放
isPlaying: 是否正在播放;
mute: 是否靜音;
如果要在開始的時候設置某些屬性,可以放到start函數里面;
cc.Class({ extends: cc.Component, properties: { // foo: { // default: null, // The default value will be used only when the component attaching // to a node for the first time // url: cc.Texture2D, // optional, default is typeof default // serializable: true, // optional, default is true // visible: true, // optional, default is true // displayName: 'Foo', // optional // readonly: false, // optional, default is false // }, // ... // 編輯器來指定 audio: { type: cc.AudioSource, default: null, }, }, // use this for initialization onLoad: function () { // 獲得節點, 獲得節點上的組件 this.audio2 = this.node.getChildByName("audio").getComponent(cc.AudioSource); }, start: function() { this.audio2.loop = true; // 循環播放, 注意一下位置 this.audio2.mute = false; // 設置靜音 console.log(this.audio2.isPlaying); // 是否正在播放 // this.audio.play(); this.audio2.play(); /*this.scheduleOnce(function() { this.audio.stop(); }.bind(this), 3);*/ /*this.scheduleOnce(function() { this.audio.pause(); // 暫停 }.bind(this), 3); this.scheduleOnce(function() { this.audio.resume(); // 恢復 }.bind(this), 6);*/ }, // called every frame, uncomment this function to activate update callback // update: function (dt) { // }, });