Making a Jingle Bells App – Part 3: Coding The Sound


Posted On by John Slater in Jingle Bells, Making Apps, Tutorials

In the last part of this series we covered the coding involved in enabling gesture recognition in the app and we managed to successfully have the device recognise movement and determine whether or not the device was shaken. In this part we are going to move onto the coding of the function that will play the sound when the device is shaken.

We’ve already got the shake function writing to the log when the device is shaken so we will amend this function to instead run a function, a function that will play a short system sound which in this case is the bells sound.

1. Importing The AudioToolbox Framework

You may or not be aware of the frameworks in iOS, basically the different APIs Apple has to offer are all within various Frameworks, for system sounds you can find the APIs within the AudioToolbox framework, the one we are going to cover in this tutorial, but there are also Frameworks for Animation, Location and Rich media like Audio (music) and Video. The reason behind having different frameworks is simply because including unnecessary APIs would increase the size of your app, so instead you have the option to only include what you need.

1.1 Adding Framework

In Xcode importing a framework is very straight forward, simply open your project and in the product navigator on the left, select “JingleBells”.

In the “Summary” pane you will see the section for “Linked Frameworks & Libraries”, this is where we will import the “Audio Toolbox” framework, so click the add (+) button.

In the modal window search for “Audio” and this will bring back some result, from here click on “AudioToolbox.framework” and click “Add” and that’s now imported into our project.

1.2 Import Framework

Now the framework is successfully added to our project we need to import the framework into our application’s ViewController.h file, so open this file where you will see this line…

#import <UIKit/UIKit.h>

Directly below this we need to import our new framework, which is straight forward…

#import <UIKit/UIKit.h>
#import <AudioToolbox/AudioToolbox.h>

2. The Sound Function

While we’re in the ViewController.h file we will now define the sound object/function and begin creating it.

2.1 Defining The Sound Player Object

In the interface file in the last tutorial we added the following line…

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration;

This was defining the shake function, we now need to add something similar for our sound player, so below this line add the following…


2.2 Creating The Sound Player Object

Moving to the implementation file ViewController.m we will now begin making the function so that we can play some sound.

In the last tutorial we added the following object/function which would be called when there is movement in the device…

-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration{

To keep things in order we will add our new -(void)playJingleBell function right below this…

-(void)playJingleBell {
	// code here

3. Playing Sound

Now we have the outline of our function created and ready to go we now need to add the sound making magic within it, this is where things start to get confusing because there are multiple lines of code for one simple function. The following code is used the play a short system sound…

-(void)playJingleBell {
    CFBundleRef mainBundle = CFBundleGetMainBundle(); /* Define mainBundle as the current app's bundle */
    CFURLRef fileURL = CFBundleCopyResourceURL(mainBundle, (CFStringRef)@"jingleBell", CFSTR("wav"), NULL); /* Set Bundle as Main Bundle, Define Sound Filename, Define Sound Filetype */
    UInt32 soundID; /* define soundID as a 32Bit Unsigned Integer */
    AudioServicesCreateSystemSoundID (fileURL, &soundID); /* Assign Sound to SoundID */
    AudioServicesPlaySystemSound(soundID); /* Now play the sound associated with this sound ID */

To break this down slightly i’ve commented each line of code for you, hopefully it offers some insight into what each line’s purpose is.

4. Playing Sound On Shake

We already have the function setup to play a sound, we also have the function setup to detect movement and determine whether or not the device was shaken, now we just need to put the two together.

This is very simple step because we already have something triggering when the device is shaken, the NSLog();. On the line where we implemented this in the last tutorial replace the NSLog with the following line…

[self playJingleBell];

… this will trigger the playJingleBell function we just created whenever the device is shaken.

5. Adding Vibration

As i mentioned in the first tutorial in this series the vibration function is actually part of the AudioToolbox framework and can be called with simply one line of code. The vibration, when called, will play along side the system sound. To add vibration add the following line to the end of our playJingleBell function…



So we now have a working example of an application that will play a sound when the device is shaken, you can test this by pressing CMD+R and testing it on your iPhone or iPod.

We still have a couple more thing to complete in this series, notably the interface of the application which, as we stand now, is non-existent. In the next tutorial we will cover building the interface in Interface Builder and triggering the background to change colour when the device is shaken.

You can download the Xcode project for what we have created so far below. If you have any questions or need any assistance with bugs feel free to use the comments below or contact me via email or Twitter.

Download The Xcode Project

You can download the Xcode Project for this tutorial to help you better understand the code and syntax.

Download Now!

About John Slater

I'm a professional web designer professionally, in a professional way, but also love iOS Development and two years ago is when i threw myself into the deep end and learnt enough to create a few apps. While i develop my skills i want to help others learn iOS development, because i'm kind like that. @slaterjohn