In the previous articles we explained how to add sprites to the iPhone application. We also demonstrated how to add animations using the actions provided by the Cocos2d framework. In this article we are going to apply audio to our application.

Recommended Reading:

This is the continuation of the series "Creating iPhone Applications Using Cocos2d". It is highly recommended to read the following articles for better understanding:

1) Introduction to Cocos2d for the iPhone
2) Sprites and Animation in Cocos2d Application
3) Cocos2d iPhone Sprite Movement and Collision Detection

Audio in ABC Pop:

If you have downloaded ABC Pop application from the Apple app stores you will notice that there are two different types of audios used in the application. The first audio that you will hear is the background music which plays throughout the life of an application. The other audio is the sound effects played when a sprite is touched.

There are many libraries to handle the audio in your application but since we are already using the Cocos2d framework we will use the SimpleAudioEngine framework for handling audio in our application. SimpleAudioEngine is build into the Cocos2d framework and provides the simplest way of adding audio to a Cocos2d application.

Where to get the Audio for FREE?

Professional audio is expensive! If you are an individual developer then your first priority would be to get free audio. There are many sources where you can get good quality audio but Freesound.org is one of the leading communities when it comes to free audio sources. Free-Loops is another great site to get free audio. Although the audio is free you must carefully read their user agreement and must comply to it in all situations.

Audio files hosted on freesound.org might be in different formats. In this article we will convert the file format to caf (Compressed Audio Format) since it is the recommended format for iPhone applications.

Converting Files to CAF Format:

To convert formats to caf you can use the built in tool called "afconvert". Open the terminal window and run the following command:



For more information on how to convert between formats visit this article.

Adding Audio to Our Application:

As, mentioned before SimpleAudioEngine makes it extremely easy to embed audio in your iPhone application. The following line of code plays the background sound.



The above code will play the "background_music.caf" file. If the audio file is large in size then it will take some time for the file to load. If you want to start the background_music at a particular instance then it is always a good idea to preload the audio before playing it. Use the following code to preload the audio:



Once, you have preloaded the audio it remains in remains in the memory for the next usage. By preloading the audio you remove the initial pause/hiccup caused by the loading of the audio for the first time. The playBackgroundMusic method takes several arguments in which one of them is loop. The loop argument represents if the background music has to be repeated or not. By default the playBackgroundMusic method will repeat the audio as shown in the Cocos2d playBackgroundMusic implementation:

 

The same concepts apply to audio effects. As, mentioned earlier effects are usually generated to indicate the actions produced by the sprites. In ABC Pop application there are multiple effects used which includes the female voice, popping sound of the bubble etc. It is highly recommended that you preload all the effects since a pause in an audio makes an application unprofessional.

References:

1) SimpleAudioEngine Documentation

Conclusion:

Audio is considered one of the pillers of a successfull application. SimpleAudioEngine makes it easy to add audio effects to the application. In the next article we will introduce the SpaceManager, physics engine for the iPhone game development.