iPhone iOS4 Playing audio in the background ~ Technology Articles

iPhone iOS4 Playing audio in the background ~ Technology Articles
Filled Under:
,

iPhone iOS4 Playing audio in the background


Playing audio in the background

As part of the multitasking support introduced with iOS4, the iPhone is now capable of playing audio in the background. When a user exits your application, any currently playing audio will continue to play (unless, of course, another application starts with its own audio).Note that at the time of writing, the iPhone Simulator does not currently support background audio; you will have to test your code on an actual iPhone.

To update the application toplay audio in the background:

1. In the Groups & Files pane, expand the target section, right-click your application target, and select Get Info.

2. Making sure the General tab is selected, click Add (+) at the bottom of the Linked Libraries list, and add Audio Toolbox.framework C.

3. Open Audio Player Example View Controller.h, and add the #import statement to import Audio Toolbox.h (Code Listing 9.7).

4. Switch to Audio Player Example View Controller.m, and add the following just before you create your audio player: This code is sets up an audio session for playing back media (as defined by k Audio Session Category_Media Playback).

By using this category, your audio will continue to play when your application exits or when the screen locks. The final step in getting background audio is to make a change to your application’s info.plist file.

5. In the Groups & Files pane, select the Resources section, and then select Audio Player Example-Info.plist.

6. Right-click in the key column, and select Add Row. Select “Required background modes” from the list D.

7. Expand the key you have just added, click the arrows to the right of Item 0, and select “App plays audio” E.

8. Build and run your application. If you start the song playing and then press the Home button to exit yourapplication, the audio will continue to play. Code Listing 9.8 shows the updated view Did Load method.

Controlling audio from the background

You’ve seen how to make your audio continue to play in the background even after your application has exited, but sometimes you might want to be able to control that audio. For example, you might have written a music player and still want to be able to change tracks and control the audio volume while running another application. Again, the multitasking features of the iOS4 SDK make it very easy for you to add this to your applications.

To update the application to control audio from the background:

1. Open Audio Player Example View Control ler.m, and add the following line to your view Did Load: method:[[UI Application shared Application] beginReceivingRemoteControl Events];This tells your application that it can receive events from the external audio controls when it is running in the background.

2. Since remote audio events are sent via the responder chain, you must make sure that your view controller is capable of receiving them. This is done by implementing the following method You’ll also will need to make sure that your view controller is always the first responder after any user interaction has taken place, so in your play:, change Volume:, and scrub: methods, you need to add the following: [self become First Responder];

3. Finally, you implement the remote Control Received With Event: method to capture remote control events:switch (event.subtype) {case UIEvent Sub type Remote Control Toggle Play Pause:[self play:nil];break;} In this example, since you have only a Play/Stop button, you handle the UI Event Sub type Remote Control Toggle Play Pause event; however, there are other events you can test for, such as skipping to the next chapter or seeking forward and backward through the movie. Code Listing 9.9 shows the completed application.

4. Build and run your application. If you start playing the song and then press the Home button to exit the application, the audio will continue to play as previously. However, this time, if you double-tap the home button and swipe the multitasking area to the right, you will bring up the audio controls F. You can stop and start the audio by tapping the Play/Pause button. Notice also how your application icon has automatically appeared in the control–tapping it will open your application.

BIO:
Sussan Deyhim is from local seo services and a University Lecturer. She loves to write on technical topics like website analysis. She is writing from last three years.