Advertisement
Scroll to top
Read Time: 6 min
This post is part of a series called iOS Multitasking.
iOS Multitasking: Local Notifications
iOS Multitasking: Background Tasks

This is a continuation of the iOS Multitasking series. Along with notifications (as discussed last week), iOS Multitasking also provides a feature called Background Audio. As the name suggests, this feature enables applications to play audio even when the application is in the background, similar to how the iPod or Music Application bundled with the device works.

In this tutorial we will be covering playing a sound in the background, not how to record audio in the background. To demonstrate we will be making a simple noise maker and give it the ability to play in the background.

Step 1: Setting Up the Project

First create a new project and call it NoiseMaker (or some other name) as a View-Based Application with the default settings.

Once the project has been created go to NoiseMaker-Info.plist and add UIBackgroundModes as a new row. It should then create the array.

Open the array and to the right of Item 0 set it to audio. Your NoiseMaker-Info.plist should look like this:

iOS_Background_AudioiOS_Background_AudioiOS_Background_Audio

Now go to the target settings and go to the tab labeled Build Phases.

iOS_Background_AudioiOS_Background_AudioiOS_Background_Audio

When you've finished the last sub-step go to Link Binary With Libraries and add the AVFoundation framework.

iOS_Background_AudioiOS_Background_AudioiOS_Background_Audio

In the Spotlight Search on your Mac for the file HeadSpin Long.caf. Find the file and rename it so the space is deleted.

Drag the file into Xcode under the NoiseMaker Directory. Make sure Copy Resources into Destinations Folder is checked.

Step 2: Setting Up the User Interface

In the NoiseMaker Directory select the NoiseMakerViewController.h file and add the following code under the #import declaration.

1
	#import <AVFoundation/AVFoundation.h>

Now add the following code under the @interface declaration.

1
	
2
	IBOutlet UIButton *playPauseButton; //Toggles the playback state

3
    IBOutlet UISlider *volumeControl; //Sets the volume for the audio player

4
    IBOutlet UILabel *alertLabel; //The alert label showing the status of the loading of the file

5
    
6
    AVAudioPlayer *audioPlayer; //Plays the audio

Then right under the closing bracket add the following code.

1
	
2
@property (nonatomic, retain) IBOutlet UIButton *playPauseButton;
3
@property (nonatomic, retain) IBOutlet UISlider *volumeControl;
4
@property (nonatomic, retain) IBOutlet UILabel *alertLabel; 
5
6
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
7
8
- (IBAction)volumeDidChange:(id)slider; //handle the slider movement

9
- (IBAction)togglePlayingState:(id)button; //handle the button tapping

10
11
- (void)playAudio; //play the audio

12
- (void)pauseAudio; //pause the audio

13
- (void)togglePlayPause; //toggle the state of the audio


After all of this code your .h file should look like this.

1
	
2
#import <UIKit/UIKit.h>

3
#import <AVFoundation/AVFoundation.h>

4
5
@interface NoiseMakerViewController : UIViewController {
6
    IBOutlet UIButton *playPauseButton; //Toggles the playback state

7
    IBOutlet UISlider *volumeControl; //Sets the volume for the audio player

8
    IBOutlet UILabel *alertLabel; //The alert label showing the status of the loading of the file

9
    
10
    AVAudioPlayer *audioPlayer; //Plays the audio

11
}
12
13
@property (nonatomic, retain) IBOutlet UIButton *playPauseButton;
14
@property (nonatomic, retain) IBOutlet UISlider *volumeControl;
15
@property (nonatomic, retain) IBOutlet UILabel *alertLabel; 
16
17
@property (nonatomic, retain) AVAudioPlayer *audioPlayer;
18
19
- (IBAction)volumeDidChange:(id)slider; //handle the slider movement

20
- (IBAction)togglePlayingState:(id)button; //handle the button tapping

21
22
- (void)playAudio; //play the audio

23
- (void)pauseAudio; //pause the audio

24
- (void)togglePlayPause; //toggle the state of the audio

25
26
@end

Step 3: Building the Interface

Now open the NoiseMakerViewController.xib. First add a UILabel somewhere in the view. In the Attributes Inspector select the option to make the UILabel's text centered and erase all of the UILabel's text. Now go to the Size Inspector and set the X position to 20, the Y position to 100, the width to 280, and the height to 21.

Next go to the Connections Inspector and drag the referencing outlet to the files owner and select the alertLabel option.

iOS_Background_Audio
iOS_Background_Audio
iOS_Background_AudioiOS_Background_AudioiOS_Background_Audio

Now add a UIButton to the view and set the button's text to Play. Go to the Size Inspector and set the X Position to 86, the Y Position to 211, the width to 150, and the height to 37. Then in the Connections Inspector drag the TouchUpInside action to the file owners and select the togglePlayingState: option.

Drag the referencing outlet to the files owner and select the option playPauseButton. Now drag out a UISlider and put it onto the view.

Go the Size Inspector and set the X Position to 18, the Y Position to 378, the width to 284, and the height to 23 (default height). Go to the Connections Inspector and drag the referencing outlet to the files owner and select the option volumeControl. Next, drag the valueChanged action to the files owner and select the option volumeDidChange:. The finished interface should look like this.

iOS_Background_Audio

Step 4: Implementing the Audio Player

Now open the NoiseMakerViewController.m file. Under the @implementation declaration add the following lines.

1
	
2
@synthesize playPauseButton;
3
@synthesize volumeControl;
4
@synthesize alertLabel;
5
@synthesize audioPlayer;
6
7
- (IBAction)volumeDidChange:(UISlider *)slider {
8
    //Handle the slider movement

9
    [audioPlayer setVolume:[slider value]];
10
}
11
12
- (IBAction)togglePlayingState:(id)button {
13
    //Handle the button pressing

14
    [self togglePlayPause];
15
}

Now in the dealloc: method add the following lines.

1
	
2
//Remove the objects from memory

3
    
4
    self.playPauseButton = nil;
5
    self.volumeControl = nil;
6
    self.alertLabel = nil;
7
    self.audioPlayer = nil;
8
    
9
    [playPauseButton release];
10
    [volumeControl release];
11
    [alertLabel release];
12
    [audioPlayer release];

Then add the following code under the dealloc: method.

1
	
2
- (void)viewDidAppear:(BOOL)animated {
3
    [super viewDidAppear:animated];
4
    
5
    
6
    //Once the view has loaded then we can register to begin recieving controls and we can become the first responder

7
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
8
    [self becomeFirstResponder];
9
}
10
11
- (void)viewWillDisappear:(BOOL)animated {
12
    [super viewWillDisappear:animated];
13
    
14
    //End recieving events

15
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
16
    [self resignFirstResponder];
17
}
18
19
- (void)playAudio {
20
    //Play the audio and set the button to represent the audio is playing

21
    [audioPlayer play];
22
    [playPauseButton setTitle:@"Pause" forState:UIControlStateNormal];
23
}
24
25
- (void)pauseAudio {
26
    //Pause the audio and set the button to represent the audio is paused

27
    [audioPlayer pause];
28
    [playPauseButton setTitle:@"Play" forState:UIControlStateNormal];
29
}
30
31
- (void)togglePlayPause {
32
    //Toggle if the music is playing or paused

33
    if (!self.audioPlayer.playing) {
34
        [self playAudio];
35
        
36
    } else if (self.audioPlayer.playing) {
37
        [self pauseAudio];
38
        
39
    }
40
}
41
42
//Make sure we can recieve remote control events

43
- (BOOL)canBecomeFirstResponder {
44
    return YES;
45
}
46
47
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
48
    //if it is a remote control event handle it correctly

49
    if (event.type == UIEventTypeRemoteControl) {
50
        if (event.subtype == UIEventSubtypeRemoteControlPlay) {
51
            [self playAudio];
52
        } else if (event.subtype == UIEventSubtypeRemoteControlPause) {
53
            [self pauseAudio];
54
        } else if (event.subtype == UIEventSubtypeRemoteControlTogglePlayPause) {
55
            [self togglePlayPause];
56
        }
57
    }
58
}


Now in the viewDidLoad: method add the following lines of code under the [super viewDidLoad];

1
	
2
//Declare the audio file location and settup the player

3
    NSURL *audioFileLocationURL = [[NSBundle mainBundle] URLForResource:@"HeadspinLong" withExtension:@"caf"];
4
    
5
    NSError *error;
6
    audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:audioFileLocationURL error:&amp;error];
7
    [audioPlayer setNumberOfLoops:-1];
8
    
9
    if (error) {
10
        NSLog(@"%@", [error localizedDescription]);
11
        
12
        [[self volumeControl] setEnabled:NO];
13
        [[self playPauseButton] setEnabled:NO];
14
        
15
        [[self alertLabel] setText:@"Unable to load file"];
16
        [[self alertLabel] setHidden:NO];
17
    } else {
18
        [[self alertLabel] setText:[NSString stringWithFormat:@"%@ has loaded", @"HeadspinLong.caf"]];
19
        [[self alertLabel] setHidden:NO];
20
        
21
        //Make sure the system follows our playback status

22
        [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
23
        [[AVAudioSession sharedInstance] setActive: YES error: nil];
24
        
25
        //Load the audio into memory

26
        [audioPlayer prepareToPlay]; 
27
    }

Final Thoughts

Thanks for reading this tutorial. Next in the series will be Task Completion. Feel free to comment if you encounter any problems or just want to add a tip.

Advertisement
Did you find this post useful?
Want a weekly email summary?
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.