Have a few questions related to adding sounds to my game, specifically intro music (for splash), background music (loop) and button event sounds. Hope you can share your knowledge on this.
1) Should I use compressed sounds or uncompressed sounds? Or perhaps a combination of the two? Are there any limitations on the iPhone hardware that I should be aware of -- for example, the ability to play multiple compressed sounds?
2) What's the best audio format for my purpose?
3) For background music, I am thinking of using AVAudioPlayer. For button event sounds, I am thinking of using AudioServicesPlaySystemSound, what do you think?
4) Any other issues I should be aware of?
Thank you!
1) According to the iPhone Application Programming Guide: "Starting in iPhone OS 3.0, nearly all supported audio formats can be used for simultaneous playback—namely, all those that can be played using software decoding, as described in Table 7-1. For the most processor-efficient multiple playback, use linear PCM (uncompressed) or IMA4 (compressed) audio."
But if you're going to be playing a lot of sounds at the same time, you should use either uncompressed or IMA4.
2) You should use the CAF file format because it gives you the most flexibility in what data format to use. You can put MP3, AAC, PCM, IMA4, and pretty much any compressed or uncompressed data in a CAF file (.caf). I typically use CAF files with IMA4 compression at mono, 16-bit, 44.1kHz.
3) That's how I do it in my apps: AVAudioPlayer for voice cues that cause the music levels to duck, and AudioServicesPlaySystemSound for buttons and other UI sound effects.
4) If you use uncompressed PCM data, the file sizes can be huge, but IMA4 is a fairly noisy compression scheme. You can use AAC but then you might be limited by how many AAC sounds you can play at once. Before iPhone OS 3.0, you could only play one AAC or MP3 sound at a time.
Related
Can Coco2D playing 2 background sounds?
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"sound1.caf" loop:YES];
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:#"sound2.caf" loop:YES];
Can sound2 be set to half of the volume and sound1 be the dominant?
It is highly discouraged to play two BGM (Background Music) at the same time. Because of the iPhone's hardware architecture, it has only one hardware sound decoder to play compressed music files.
With that said, your sounds seem to be in caf format, which is uncompressed, and thus easy to play using playEffect:. You can play as many effects as you want at the same time, without worrying about performance that much.
NOTE: The playEffect: method should only be used with uncompressed audio, such as caf, aif, ... etc.
From Steve Oldmeadow, author of CocosDenshion, the audio engined shipped with cocos2d.
There is a single AVAudioPlayer that plays background music so you can only have one background music file loaded at a time, if you need something more sophisticated then you have to roll your own solution.
Interesting links
http://www.cocos2d-iphone.org/forum/topic/3074
Cocos2d play 2 different background music files or loop playEffect
I am playing a song in Cocos2D iphone using this line.
[[SimpleAudioEngine sharedEngine] playEffect:#"song.mp3"];
Suppose the mp3 is 1 min long, I want to play the song from 10 sec to 20 sec. Is it possible in Cocos2D iphone. Please help.
I don't think it is possible to "play the song from 10 sec to 20 sec" using the audio engine of cocos2d-iphone (which is named CocosDeshion).
SimpleAudioEngine is the main interface to CocosDeshion. Basically in cocos2d development we use audio files in two way - sound effect (short) and background music (long and usually loop). playEffect: is for the short ones, while playBackgroundMusic: and playBackgroundMusic:loop: is for the long ones. preloadBackgroundMusic: provides you the ability to cache the music in memory to avoid lag while playing, since they are often in compressed format.
As you can see in the documentation of SimpleAudioEngine (link above), there isn't such method for you to play a part of an audio file. Even the underlying class CDAudioManager doesn't support this.
I suggest to extract the 0:10 - 0:20 part as a separate audio file. But if you have a special reason to do this, you may want to use audio playback interfaces (such as AVAudioPlayer) in iOS SDK directly. Please refer to: http://developer.apple.com/library/ios/#documentation/AudioVideo/Conceptual/MultimediaPG/UsingAudio/UsingAudio.html
I'm using cocos2d and it is possible to play both formats using SimpleAudioEngine (obviously did not tried .caf) What format is preferable and why ?
I recommend you to use caf format with IMA4 compression for sound effects and aac for BGM.
CocosDenshion FAQ - What sound formats can I use?
For sound effects played with
CDSoundEngine it is recommended to use
16 bit mono wave files for
uncompressed audio and IMA4 for lossy
compressed audio. IMA4 sounds are
around one quarter the size of the
corresponding wave file.
How do I choose the best audio formats in iPhone OS?
If i play my sounds on the iPhone on max ringer volume it plays fine But when i reduce it to some extend ma AudioToolbox sounds are playing at the same volume But my AVAudioPlayer sounds are playing fine. Is there any way to decrease the volume of sounds played by AudioToolbox...
From: iPhone Application Programming Guide
The AudioServicesPlaySystemSound function lets you very simply play short sound files. The simplicity carries with it a few restrictions. Your sound files must be:
Shorter than 30 seconds in duration
In linear PCM or IMA4 (IMA/ADPCM)
format Packaged in a .caf, .aif, or
.wav file
In addition, when you use the AudioServicesPlaySystemSound function:
Sounds play at the current system
audio level, with no level control
available
Sounds play immediately
Looping and stereo positioning are
unavailable
i am playing the video and in the background i have to play the audio file and if i do this audio is not playing its sound as it just play video is it a extension problem i am using the .mp3 for the audio and .mp4 for the video .
how can we do this we have to play simultanisouly .both the file
balraj verma
Have a look at this:
http://developer.apple.com/iphone/library/documentation/MusicAudio/Conceptual/CoreAudioOverview/CoreAudioEssentials/CoreAudioEssentials.html#//apple_ref/doc/uid/TP40003577-CH10-SW67
"To play multiple sounds simultaneously, create one playback audio queue object for each sound. For each audio queue, schedule the first buffer of audio to start at the same time using the AudioQueueEnqueueBufferWithParameters function.
Audio format is critical when you play sounds simultaneously on iPhone or iPod touch. This is because playback of certain compressed formats in iPhone OS employs an efficient hardware codec. Only a single instance of one of the following formats can play on the device at a time:
AAC
ALAC (Apple Lossless)
MP3"