Cocos2d uses SimpleAudioEngine to load music and sound effects

Source: Internet
Author: User

Game music and sound effects

Using SimpleAudioEngine in cocos2d can easily play background music and sound effects.

By default, the background music is played cyclically, and the sound effect is played once by default. In general, the sound effects are very short.

Next, we will analyze the SimpleAudioEngine class.

This class has three attributes (backgroundMusicVolume, effectsVolume, willPlayBackgroundMusic), but I don't know why they need to define these three attributes.

+ (SimpleAudioEngine *) sharedEngine

How to obtain a singleton

-(Void) preloadBackgroundMusic :( NSString *) filePath

This method can be used to load the background music required in the game in advance.

-(Void) playBackgroundMusic (NSString *) filePath;

-(Void) playBackgroundMusic (NSString *) filePath loop :( BOOL) loop;

FilePath: file path (file name in the Project)

Loop: loop. YES indicates loop. NO indicates loop. NO indicates loop. YES indicates loop.

-(Void) stopBackgroundMusic;

Stop background music

-(Void) pauseBackgroundMusic;

Pause background music

-(Void) resumeBackgroundMusic;

Resume playing background music

-(Void) rewindBackgroundMusic;

Back, that is, start playing background music again.

-(BOOL) isBackgroundMusicPlaying

Determine whether the background music is playing

 

-(ALuint) playEffect :( NSString *) filePath;

ALuint: it is a synonym for the unsigned integer variable (unsigned int), that is, an alias. The returned value indicates the playback sound ID.

FilePath: file path

-(Void) stopEffect :( ALuint) soundId;

Here, we can use soundId to stop a sound effect. The value of soundId is obtained by the previous method.

-(Void) preloadEffect :( NSString *) filePath;

By loading sound effects, we can pre-load all the sound effects used in the game, and put the sound effects in an array. during playback, you can directly find the corresponding audio playback Based on filePath.

-(Void) unloadEffect :( NSString *) filePath;

Release a sound effect

-(CDSoundSource *) soundSourceForFile :( NSString *) filePath;

In this method, we can use filePath to obtain the corresponding sound configuration information (CDSoundSource). The CDSoundSource attribute has a soundId. When we want to stop a sound effect, we can use this method to obtain the soundId.

 

-(Void) setMute :( BOOL) muteValue

No sound. If muteValue is set to YES, no sound is triggered.

-(Void) setBackgroundMusicVolume :( float) volume

Set the volume of Background Music volume. If this parameter is not set, the default value is 1.0f. A larger value means a larger volume.

-(Float) backgroundMusicVolume

Obtains the volume of background music.

-(Void) setEffectsVolume :( float) volume

Set the sound volume. If this parameter is not set, the default value is 1.0f. A larger value means a larger volume.

-(Float) implements tsvolume

Obtain sound volume

 

// The following are common methods:

Attach background music

[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic: @ "background.pdf"];

Play background music

[[SimpleAudioEngine sharedEngine] playBackgroundMusic: @ "background.pdf"];

Pause background music

[[SimpleAudioEngine sharedEngine] puseBackgroundMusic];

Continue playing background music

[[SimpleAudioEngine sharedEngine] resumeBackgroundMusic];

Stop background music

[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];

Playing Sound Effects

[[SimpleAudioEngine sharedEngine] playEffect: @ "effect1.wav"];

Load sound effects

[[SimpleAudioEngine sharedEngine] preloadEffect: @ "effect1.wav"];

Set alert

[[SimpleAudioEngine sharedEngine] setMute: YES];

Set volume

[[SimpleAudioEngine sharedEngine] setBackgroundMusicVolume: 0.5f];

[[SimpleAudioEngine sharedEngine] setjavastsvolume: 2.0f];

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.