Cocos Creator API

1.0.0

Cocos Creator is a highly customizable game development tool that utilizes the power of cocos2d-x.

audioEngine

Module: cc

cc.audioEngine is the singleton object, it provide simple audio APIs. zn cc.audioengine是单例对象。
主要用来播放背景音乐和音效,背景音乐同一时间只能播放一个,而音效则可以同时播放多个。
注意:
在 Android 系统浏览器上,不同浏览器,不同版本的效果不尽相同。
比如说:大多数浏览器都需要用户物理交互才可以开始播放音效,有一些不支持 WebAudio,
有一些不支持多音轨播放。总之如果对音乐依赖比较强,请做尽可能多的测试。

Methods

playMusic
(
  • url
  • loop
)

Play music.

name type description
url String

The path of the music file without filename extension.

loop Boolean

Whether the music loop or not.

examples:

//example
cc.audioEngine.playMusic(path, false);

stopMusic
(
  • [releaseData ]
)

Stop playing music.

name type description
releaseData optional Boolean

If release the music data or not.As default value is false.

examples:

//example
cc.audioEngine.stopMusic();

pauseMusic ( )

Pause playing music.

examples:

//example
cc.audioEngine.pauseMusic();

resumeMusic ( )

Resume playing music.

examples:

//example
cc.audioEngine.resumeMusic();

rewindMusic ( )

Rewind playing music.

examples:

//example
cc.audioEngine.rewindMusic();

getMusicVolume ( ) Number

The volume of the music max value is 1.0,the min value is 0.0 .

returns:

type: Number

examples:

//example
var volume = cc.audioEngine.getMusicVolume();

setMusicVolume
(
  • volume
)

Set the volume of music.

name type description
volume Number

Volume must be in 0.0~1.0 .

examples:

//example
cc.audioEngine.setMusicVolume(0.5);

isMusicPlaying ( ) Boolean

Whether the music is playing.

returns:

type: Boolean

If is playing return true,or return false.

examples:

//example
 if (cc.audioEngine.isMusicPlaying()) {
     cc.log("music is playing");
 }
 else {
     cc.log("music is not playing");
 }

playEffect
(
  • url
  • loop
)
Number | Null

Play sound effect.

name type description
url String

The path of the sound effect with filename extension.

loop Boolean

Whether to loop the effect playing, default value is false

returns:

type: Number | Null

the audio id

examples:

//example
var soundId = cc.audioEngine.playEffect(path);

setEffectsVolume
(
  • volume
)

Set the volume of sound effects.

name type description
volume Number

Volume must be in 0.0~1.0 .

examples:

//example
cc.audioEngine.setEffectsVolume(0.5);

getEffectsVolume ( ) Number

The volume of the effects max value is 1.0,the min value is 0.0 .

returns:

type: Number

examples:

//example
var effectVolume = cc.audioEngine.getEffectsVolume();

pauseEffect
(
  • audio
)

Pause playing sound effect.

name type description
audio Number

The return value of function playEffect.

examples:

//example
cc.audioEngine.pauseEffect(audioID);

pauseAllEffects ( )

Pause all playing sound effect.

examples:

//example
cc.audioEngine.pauseAllEffects();

resumeEffect
(
  • audioID
)

Resume playing sound effect.

name type description
audioID Number

The return value of function playEffect.

resumeAllEffects ( )

Resume all playing sound effect.

examples:

//example
cc.audioEngine.resumeAllEffects();

stopEffect
(
  • audioID
)

Stop playing sound effect.

name type description
audioID Number

The return value of function playEffect.

examples:

//example
cc.audioEngine.stopEffect(audioID);

stopAllEffects ( )

Stop all playing sound effects.

examples:

//example
cc.audioEngine.stopAllEffects();

unloadEffect
(
  • url
)

Unload the preloaded effect from internal buffer.

name type description
url String

examples:

//example
cc.audioEngine.unloadEffect(EFFECT_FILE);

end ( )

End music and effects.

There are no methods that match your current filter settings. You can change your filter settings in the index section on this page. index