Cocos2d-x  v4
AudioEngine Class Reference

Offers a interface to play audio. More...

Public Types

enum  AudioState
 AudioState enum,all possible states of an audio instance.
 

Static Public Member Functions

static void end ()
 Release objects relating to AudioEngine. More...
 
static AudioProfilegetDefaultProfile ()
 Gets the default profile of audio instances. More...
 
static int play2d (const std::string &filePath, bool loop=false, float volume=1.0f, const AudioProfile *profile=nullptr)
 Play 2d sound. More...
 
static void setLoop (int audioID, bool loop)
 Sets whether an audio instance loop or not. More...
 
static bool isLoop (int audioID)
 Checks whether an audio instance is loop. More...
 
static void setVolume (int audioID, float volume)
 Sets volume for an audio instance. More...
 
static float getVolume (int audioID)
 Gets the volume value of an audio instance. More...
 
static void pause (int audioID)
 Pause an audio instance. More...
 
static void pauseAll ()
 Pause all playing audio instances.
 
static void resume (int audioID)
 Resume an audio instance. More...
 
static void resumeAll ()
 Resume all suspended audio instances.
 
static void stop (int audioID)
 Stop an audio instance. More...
 
static void stopAll ()
 Stop all audio instances.
 
static bool setCurrentTime (int audioID, float sec)
 Sets the current playback position of an audio instance. More...
 
static float getCurrentTime (int audioID)
 Gets the current playback position of an audio instance. More...
 
static float getDuration (int audioID)
 Gets the duration of an audio instance. More...
 
static AudioState getState (int audioID)
 Returns the state of an audio instance. More...
 
static void setFinishCallback (int audioID, const std::function< void(int, const std::string &)> &callback)
 Register a callback to be invoked when an audio instance has completed playing. More...
 
static int getMaxAudioInstance ()
 Gets the maximum number of simultaneous audio instance of AudioEngine.
 
static bool setMaxAudioInstance (int maxInstances)
 Sets the maximum number of simultaneous audio instance for AudioEngine. More...
 
static void uncache (const std::string &filePath)
 Uncache the audio data from internal buffer. More...
 
static void uncacheAll ()
 Uncache all audio data from internal buffer. More...
 
static AudioProfilegetProfile (int audioID)
 Gets the audio profile by id of audio instance. More...
 
static AudioProfilegetProfile (const std::string &profileName)
 Gets an audio profile by name. More...
 
static void preload (const std::string &filePath)
 Preload audio file. More...
 
static void preload (const std::string &filePath, std::function< void(bool isSuccess)> callback)
 Preload audio file. More...
 
static int getPlayingAudioCount ()
 Gets playing audio count.
 
static void setEnabled (bool isEnabled)
 Whether to enable playing audios. More...
 
static bool isEnabled ()
 Check whether AudioEngine is enabled.
 

Detailed Description

Offers a interface to play audio.

Note
Make sure to call AudioEngine::end() when the audio engine is not needed anymore to release resources. @js NA

Member Function Documentation

◆ end()

static void end ( )
static

Release objects relating to AudioEngine.

Warning
It must be called before the application exit. @lua endToLua

◆ getDefaultProfile()

static AudioProfile* getDefaultProfile ( )
static

Gets the default profile of audio instances.

Returns
The default profile of audio instances.

◆ play2d()

static int play2d ( const std::string &  filePath,
bool  loop = false,
float  volume = 1.0f,
const AudioProfile profile = nullptr 
)
static

Play 2d sound.

Parameters
filePathThe path of an audio file.
loopWhether audio instance loop or not.
volumeVolume value (range from 0.0 to 1.0).
profileA profile for audio instance. When profile is not specified, default profile will be used.
Returns
An audio ID. It allows you to dynamically change the behavior of an audio instance on the fly.
See also
AudioProfile

◆ setLoop()

static void setLoop ( int  audioID,
bool  loop 
)
static

Sets whether an audio instance loop or not.

Parameters
audioIDAn audioID returned by the play2d function.
loopWhether audio instance loop or not.

◆ isLoop()

static bool isLoop ( int  audioID)
static

Checks whether an audio instance is loop.

Parameters
audioIDAn audioID returned by the play2d function.
Returns
Whether or not an audio instance is loop.

◆ setVolume()

static void setVolume ( int  audioID,
float  volume 
)
static

Sets volume for an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.
volumeVolume value (range from 0.0 to 1.0).

◆ getVolume()

static float getVolume ( int  audioID)
static

Gets the volume value of an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.
Returns
Volume value (range from 0.0 to 1.0).

◆ pause()

static void pause ( int  audioID)
static

Pause an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.

◆ resume()

static void resume ( int  audioID)
static

Resume an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.

◆ stop()

static void stop ( int  audioID)
static

Stop an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.

◆ setCurrentTime()

static bool setCurrentTime ( int  audioID,
float  sec 
)
static

Sets the current playback position of an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.
secThe offset in seconds from the start to seek to.
Returns

◆ getCurrentTime()

static float getCurrentTime ( int  audioID)
static

Gets the current playback position of an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.
Returns
The current playback position of an audio instance.

◆ getDuration()

static float getDuration ( int  audioID)
static

Gets the duration of an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.
Returns
The duration of an audio instance.

◆ getState()

static AudioState getState ( int  audioID)
static

Returns the state of an audio instance.

Parameters
audioIDAn audioID returned by the play2d function.
Returns
The status of an audio instance.

◆ setFinishCallback()

static void setFinishCallback ( int  audioID,
const std::function< void(int, const std::string &)> &  callback 
)
static

Register a callback to be invoked when an audio instance has completed playing.

Parameters
audioIDAn audioID returned by the play2d function.
callback

◆ setMaxAudioInstance()

static bool setMaxAudioInstance ( int  maxInstances)
static

Sets the maximum number of simultaneous audio instance for AudioEngine.

Parameters
maxInstancesThe maximum number of simultaneous audio instance.

◆ uncache()

static void uncache ( const std::string &  filePath)
static

Uncache the audio data from internal buffer.

AudioEngine cache audio data on ios,mac, and win32 platform.

Warning
This can lead to stop related audio first.
Parameters
filePathAudio file path.

◆ uncacheAll()

static void uncacheAll ( )
static

Uncache all audio data from internal buffer.

Warning
All audio will be stopped first.

◆ getProfile() [1/2]

static AudioProfile* getProfile ( int  audioID)
static

Gets the audio profile by id of audio instance.

Parameters
audioIDAn audioID returned by the play2d function.
Returns
The audio profile.

◆ getProfile() [2/2]

static AudioProfile* getProfile ( const std::string &  profileName)
static

Gets an audio profile by name.

Parameters
profileNameA name of audio profile.
Returns
The audio profile.

◆ preload() [1/2]

static void preload ( const std::string &  filePath)
inlinestatic

Preload audio file.

Parameters
filePathThe file path of an audio.

◆ preload() [2/2]

static void preload ( const std::string &  filePath,
std::function< void(bool isSuccess)>  callback 
)
static

Preload audio file.

Parameters
filePathThe file path of an audio.
callbackA callback which will be called after loading is finished.

◆ setEnabled()

static void setEnabled ( bool  isEnabled)
static

Whether to enable playing audios.

Note
If it's disabled, current playing audios will be stopped and the later 'preload', 'play2d' methods will take no effects.

The documentation for this class was generated from the following file: