Cocos Creator API

1.5.x

Cocos Creator is the game engine for the future.

audioEngine static

Module: cc

cc.audioEngine is the singleton object, it provide simple audio APIs.

Methods

play
(
  • filePath
  • loop
  • volume
)
Number

Play audio.

name type description
filePath String

The path of the audio file without filename extension.

loop Boolean

Whether the music loop or not.

volume Number

Volume size.

returns:

type: Number

audioId

examples:

//example
var audioID = cc.audioEngine.play(path, false, 0.5);

setLoop
(
  • audioID
  • loop
)

Set audio loop.

name type description
audioID Number

audio id.

loop Boolean

Whether cycle.

examples:

//example
cc.audioEngine.setLoop(id, true);

isLoop
(
  • audioID
)
Boolean

Get audio cycle state.

name type description
audioID Number

audio id.

returns:

type: Boolean

Whether cycle.

examples:

//example
cc.audioEngine.isLoop(id);

setVolume
(
  • audioID
  • volume
)

Set the volume of audio.

name type description
audioID Number

audio id.

volume Number

Volume must be in 0.0~1.0 .

examples:

//example
cc.audioEngine.setVolume(id, 0.5);

getVolume
(
  • audioID
)
Boolean

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

name type description
audioID Number

audio id.

returns:

type: Boolean

examples:

//example
var volume = cc.audioEngine.getVolume(id);

setCurrentTime
(
  • audioID
  • sec
)
Boolean

Set current time

name type description
audioID Number

audio id.

sec Number

current time.

returns:

type: Boolean

examples:

//example
cc.audioEngine.setCurrentTime(id, 2);

getCurrentTime
(
  • audioID
)
Number

Get current time

name type description
audioID Number

audio id.

returns:

type: Number

audio current time.

examples:

//example
var time = cc.audioEngine.getCurrentTime(id);

getDuration
(
  • audioID
)
Number

Get audio duration

name type description
audioID Number

audio id.

returns:

type: Number

audio duration.

examples:

//example
var time = cc.audioEngine.getDuration(id);

getState
(
  • audioID
)
audioEngine.AudioState

Get audio state

name type description
audioID Number

audio id.

returns:

type: audioEngine.AudioState

audio duration.

examples:

//example
var state = cc.audioEngine.getState(id);

setFinishCallback
(
  • audioID
  • callback
)

Set Audio finish callback

name type description
audioID Number

audio id.

callback Function

loaded callback.

examples:

//example
cc.audioEngine.setFinishCallback(id, function () {});

pause
(
  • audioID
)

Pause playing audio.

name type description
audioID Number

The return value of function play.

examples:

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

pauseAll ( )

Pause all playing audio

examples:

//example
cc.audioEngine.pauseAll();

resume
(
  • audioID
)

Resume playing audio.

name type description
audioID Number

The return value of function play. //example cc.audioEngine.resume(audioID);

resumeAll ( )

Resume all playing audio.

examples:

//example
cc.audioEngine.resumeAll();

stop
(
  • audioID
)

Stop playing audio.

name type description
audioID Number

The return value of function play.

examples:

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

stopAll ( )

Stop all playing audio.

examples:

//example
cc.audioEngine.stopAll();

setMaxAudioInstance
(
  • num
)

Set up an audio can generate a few examples.

name type description
num Number

a number of instances to be created from within an audio

examples:

//example
cc.audioEngine.setMaxAudioInstance(20);

getMaxAudioInstance ( ) Number

Getting audio can produce several examples.

returns:

type: Number

a number of instances to be created from within an audio

examples:

//example
cc.audioEngine.getMaxAudioInstance();

uncache
(
  • filePath
)

Unload the preloaded audio from internal buffer.

name type description
filePath String

examples:

//example
cc.audioEngine.uncache(filePath);

uncacheAll ( )

Unload all audio from internal buffer.

examples:

//example
cc.audioEngine.uncacheAll();

preload
(
  • filePath
  • callback
)

Preload audio file.

name type description
filePath Unknown

The file path of an audio.

callback Unknown

The callback of an audio.

examples:

//example
cc.audioEngine.preload(path);

setMaxWebAudioSize
(
  • kb
)

Set a size, the unit is KB,Over this size is directly resolved into DOM nodes

name type description
kb Unknown

The file path of an audio.

examples:

//example
cc.audioEngine.setMaxWebAudioSize(300);

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