I am developing a driving game using SpriteKit
and am having trouble with engine sound effects.
I want to have two different engine sounds. One for when the throttle button is being pressed and one for when the throttle button is not being pressed. One of the two sounds will be playing constantly while the game is going.
What is the best approach? Should my sound files be extremely short (0.10 seconds or less) and looped or should they be fairly long and just turned on and off? Should I use SKAction
to play the sounds or AVAudioPlayer
or something else? I have tried using AVAudioPlayer
but every time I pause and play the player (switching the throttle on or off), the frame rate of the game momentarily drops. Any help is appreciated!
Based on the comment left by LearnCocos2D, I looked into ObjectAL. For those not familiar with ObjectAL
, it is designed to be a simple and intuitive interface to OpenAL
and AVAudioPlayer
. You can download and find more information about it here...
http://kstenerud.github.io/ObjectAL-for-iPhone/index.html
For my audio clip I used 3 to 5 second long .caf audio file of a motor sound. ObjectAL
allowed me to continuously loop and vary the pitch of the audio file. By varying the pitch, I could simulate the motor at different speeds.
Below is a sample of the code...
Two member variables or you can set them as properties...
ALBuffer *_buffer;
ALSource *_source;
Method to initialize the motor sound effect...
- (void)initializeSound
{
// We'll let OALSimpleAudio deal with the device and context.
// Since we're not going to use it for playing effects, don't give it any sources.
[OALSimpleAudio sharedInstance].reservedSources = 0;
_source = [ALSource source];
_buffer = [[OpenALManager sharedInstance] bufferFromFile:@"EngineSound.caf"];
_source.pitch = 0.30; // Start at low pitch for engine idle.
[_source play:_buffer loop:YES];
}
Inside my SKScene
's update method I adjust the pitch according to speed.
- (void)update:(CFTimeInterval)currentTime
{
CGFloat enginePitch;
// Code to calculate desired enginePitch value based on vehicle speed.
_source.pitch = enginePitch;
}
Because of an alleged bug in SpriteKit
, I did have an issue with a gpus_ReturnNotPermittedKillClient
EXC_BAD_ACCESS
error when closing the app. I found the fix here...
https://stackoverflow.com/a/19283721/3148272
I would suggest using AVAudioPlayer. You need to have it in property and not create every time you want to play something. SKAction is bad in a way that you can't stop or cancel it (even by removing action).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With