简体   繁体   中英

How to play background music (Cocos2d iOS)

I'm new to the Cocos2d framework, and I ned help figuring out how to play a .wav file in a loop, in my main menu scene. Also, I'd like some help on how to play a sound upon the collision of two sprites.

Thanks

Prior to v3 you would preload audio files at some point before using them. For example to preload background music you would (which prepares it for playback but doesn't load the whole thing, it is streamed):

[[SimpleAudioEngine sharedEngine] preloadBackgroundMusic:@"sound.aifc"];

And to play it you would call:

[[SimpleAudioEngine sharedEngine] playBackgroundMusic:"sound.aifc"];

And to stop it you would call:

[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];

For sound effects (like when playing a sound when two objects collide), you would do the same thing by first preloading (which does load and cache the whole thing):

[[SimpleAudioEngine sharedEngine] preloadEffect:@"sound.caf"];

Then play a specific instance whenever you need it:

unsigned int audioId = [[SimpleAudioEngine sharedEngine] playEffect:@"sound.caf"
                                                              pitch:1.0f
                                                                pan:0.0f
                                                               gain:1.0f];

And to stop that specific instance you would call:

[[SimpleAudioEngine sharedEngine] stopEffect:audioId];

For effects you have to unload the audio once you are done with it (background music you do not have to):

[[SimpleAudioEngine sharedEngine] unloadEffect:@"sound.caf"];

If you are using v3 you would preload, play, and stop the background music using:

// Preload...
[[OALSimpleAudio sharedInstance] preloadBg:@"sound.aifc"];

// Play (and loop the music)...
[[OALSimpleAudio sharedInstance] playBgWithLoop:YES];

// To stop the music...
[[OALSimpleAudio sharedInstance] stopBg];

For audio effects in v3 you can preload the sound effect using:

ALBuffer* soundBuffer = [[OALSimpleAudio sharedInstance] preloadEffect:@"sound.caf"];

Or you can do it asynchronously:

__block ALBuffer* soundBuffer;

[[OALSimpleAudio sharedInstance] preloadEffect:@"sound.caf"
                                  reduceToMono:NO
                               completionBlock:^(ALBuffer* buffer)
{
    soundBuffer = buffer;
}];

Or you can load an array of audio files via:

NSArray* soundFiles = @[@"sound1.caf", @"sound2.caf", @"sound3.caf"];

[[OALSimpleAudio sharedInstance] preloadEffects:soundFiles
                                   reduceToMono:NO
                                  progressBlock:^(NSUInteger progress,
                                                  NSUInteger successCount,
                                                  NSUInteger total)
{
    if (successCount == total)
    {
        CCLOG(@"Sound files loaded!");
    }
}];

To play a sound effect:

[[OALSimpleAudio sharedInstance] playEffect:@"sound.caf" loop:NO];

Or another way to play a sound effect if you had the audio buffer:

[[OALSimpleAudio sharedInstance] playBuffer:soundBuffer
                                     volume:1.0f
                                      pitch:1.0f
                                        pan:0.0f
                                       loop:NO];

To unload all loaded and cached effects:

[[OALSimpleAudio sharedInstance] unloadAllEffects];

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM