27
Media Playback and Background Execution

Many applications on a mobile device need audio and video playback. In this chapter, you will learn how to use the most common audio and video playback routines the iOS SDK offers. In addition, we will look at background processes and multitasking.

Figure 27.1  MediaPlayer

MediaPlayer

Creating the MediaPlayer Application

Create an iPhone Window-based Application in Xcode. Name this project MediaPlayer.

MediaPlayer will have a very simple interface so that you can concentrate on the guts of media playback. The application will display two buttons that will initiate different types of audio playback, and it will also display a movie. The object diagram for this application is shown in Figure 27.1.

Figure 27.2  MediaPlayer object diagram

MediaPlayer object diagram

Create a new UIViewController subclass (FileNewNew File...) and check With XIB for user interface. Name this subclass MediaPlayerViewController. In MediaPlayerAppDelegate.m, import the header file for MediaPlayerViewController, create an instance of the view controller, and set it to be the window’s rootViewController.

#​i​m​p​o​r​t​ ​"​M​e​d​i​a​P​l​a​y​e​r​A​p​p​D​e​l​e​g​a​t​e​.​h​"​
#​i​m​p​o​r​t​ ​"​M​e​d​i​a​P​l​a​y​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​.​h​"​

@​i​m​p​l​e​m​e​n​t​a​t​i​o​n​ ​M​e​d​i​a​P​l​a​y​e​r​A​p​p​D​e​l​e​g​a​t​e​
@​s​y​n​t​h​e​s​i​z​e​ ​w​i​n​d​o​w​;​

-​ ​(​B​O​O​L​)​a​p​p​l​i​c​a​t​i​o​n​:​(​U​I​A​p​p​l​i​c​a​t​i​o​n​ ​*​)​a​p​p​l​i​c​a​t​i​o​n​
 ​ ​ ​ ​d​i​d​F​i​n​i​s​h​L​a​u​n​c​h​i​n​g​W​i​t​h​O​p​t​i​o​n​s​:​(​N​S​D​i​c​t​i​o​n​a​r​y​ ​*​)​l​a​u​n​c​h​O​p​t​i​o​n​s​
{​
 ​ ​ ​ ​M​e​d​i​a​P​l​a​y​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​*​v​c​ ​=​ ​[​[​M​e​d​i​a​P​l​a​y​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​a​l​l​o​c​]​ ​i​n​i​t​]​;​
 ​ ​ ​ ​[​[​s​e​l​f​ ​w​i​n​d​o​w​]​ ​s​e​t​R​o​o​t​V​i​e​w​C​o​n​t​r​o​l​l​e​r​:​v​c​]​;​
 ​ ​ ​ ​[​v​c​ ​r​e​l​e​a​s​e​]​;​

 ​ ​ ​ ​[​[​s​e​l​f​ ​w​i​n​d​o​w​]​ ​m​a​k​e​K​e​y​A​n​d​V​i​s​i​b​l​e​]​;​

 ​ ​ ​ ​r​e​t​u​r​n​ ​Y​E​S​;​
}​

The application needs two buttons for playing audio. One button’s title will change during runtime, and both buttons need action methods. In MediaPlayerViewController.h, declare an instance variable for the button with the title that will change and the two action methods.

@​i​n​t​e​r​f​a​c​e​ ​M​e​d​i​a​P​l​a​y​e​r​V​i​e​w​C​o​n​t​r​o​l​l​e​r​ ​:​ ​U​I​V​i​e​w​C​o​n​t​r​o​l​l​e​r​
{​
 ​ ​ ​ ​I​B​O​u​t​l​e​t​ ​U​I​B​u​t​t​o​n​ ​*​a​u​d​i​o​B​u​t​t​o​n​;​
}​
-​ ​(​I​B​A​c​t​i​o​n​)​p​l​a​y​A​u​d​i​o​F​i​l​e​:​(​i​d​)​s​e​n​d​e​r​;​
-​ ​(​I​B​A​c​t​i​o​n​)​p​l​a​y​S​h​o​r​t​S​o​u​n​d​:​(​i​d​)​s​e​n​d​e​r​;​
@​e​n​d​

Save this file and then open MediaPlayerViewController.xib.

Click the View object in the outline view to open it. Drag two UIButton objects onto the view and title them as shown in Figure 27.3. Then, make the action connections from the buttons back to the MediaPlayerViewController. Finally, connect the audioButton outlet to the button labeled Play Audio File.

Figure 27.3  MediaPlayerViewController.xib connections

MediaPlayerViewController.xib connections

In order to build and run the application without warnings, you will need stub methods for the IBActions you declared. In MediaPlayerViewController.m, implement them as follows.

-​ ​(​I​B​A​c​t​i​o​n​)​p​l​a​y​A​u​d​i​o​F​i​l​e​:​(​i​d​)​s​e​n​d​e​r​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​p​l​a​y​A​u​d​i​o​F​i​l​e​!​"​)​;​
}​
-​ ​(​I​B​A​c​t​i​o​n​)​p​l​a​y​S​h​o​r​t​S​o​u​n​d​:​(​i​d​)​s​e​n​d​e​r​
{​
 ​ ​ ​ ​N​S​L​o​g​(​@​"​p​l​a​y​S​o​u​n​d​!​"​)​;​
}​

If you want to check your connections, you can build and run the application. The log messages should show up on the console.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.212.124