iPhone Programming: The Big Nerd Ranch Guide, ISBN: 9780321706485.
Prepared for
[email protected], doug chang. Copyright © 2010 Big
Nerd ...
7DEOHRI&RQWHQWV &KDSWHU3OD\LQJ$XGLRDQG9LGHR &UHDWLQJWKH0HGLD3OD\HU$SSOLFDWLRQ 3OD\LQJ6\VWHP6RXQGV 3OD\LQJ$XGLR)LOHV 3OD\LQJ0RYLH)LOHV /RZOHYHO$3,V &KDOOHQJH$XGLR5HFRUGLQJ
&KDSWHU3OD\LQJ$XGLRDQG9LGHRL3KRQH3URJUDPPLQJ7KH%LJ1HUG5DQFK*XLGH,6%1 3UHSDUHGIRUGRXJF#VWDQIRUGDOXPQLRUJGRXJFKDQJ &RS\ULJKW%LJ1HUG5DQFK,QF7KLVGRZQORDGILOHLVPDGHDYDLODEOHIRUSHUVRQDOXVHRQO\DQGLVVXEMHFWWRWKH7HUPVRI6HUYLFH$Q\RWKHUXVHUHTXLUHVSULRUZULWWHQFRQVHQWIURP WKHFRS\ULJKWRZQHU8QDXWKRUL]HGXVHUHSURGXFWLRQDQGRUGLVWULEXWLRQDUHVWULFWO\SURKLELWHGDQGYLRODWHDSSOLFDEOHODZV$OOULJKWVUHVHUYHG
Chapter 20. Playing Audio and Video Many applications on a mobile device have a need for audio and video playback. The iPhone SDK offers a few options for audio playback routines and a single way to play video files, and, in this chapter, you will learn how to use them.
Figure 20.1. MediaPlayer
Creating the MediaPlayer Application Create a Window-Based Application in Xcode. Name this project =,.)/64/8,-. This application will have a very simple interface so that you can concentrate on the guts of media playback. The window will display three buttons, and each button will initiate playback of a different kind of media: compressed audio, video, or system sounds (Figure 20.1).
245
&KDSWHU3OD\LQJ$XGLRDQG9LGHRL3KRQH3URJUDPPLQJ7KH%LJ1HUG5DQFK*XLGH,6%1 3UHSDUHGIRUGRXJF#VWDQIRUGDOXPQLRUJGRXJFKDQJ &RS\ULJKW%LJ1HUG5DQFK,QF7KLVGRZQORDGILOHLVPDGHDYDLODEOHIRUSHUVRQDOXVHRQO\DQGLVVXEMHFWWRWKH7HUPVRI6HUYLFH$Q\RWKHUXVHUHTXLUHVSULRUZULWWHQFRQVHQWIURP WKHFRS\ULJKWRZQHU8QDXWKRUL]HGXVHUHSURGXFWLRQDQGRUGLVWULEXWLRQDUHVWULFWO\SURKLELWHGDQGYLRODWHDSSOLFDEOHODZV$OOULJKWVUHVHUYHG
Creating the MediaPlayer Application
Figure 20.2. MediaPlayer object diagram View
UIWindow
subviews UIButton title = @"Play Short Sound"
UIButton title = @"Play Video File"
UIButton title = @"PlayAudio File"
Controller window
/LFHQVHGE\ GRXJFKDQJ audioButton
playAudioFile:
playVideoFile:
playShortSound:
MediaPlayerAppDelegate
moviePlayer
MPMoviePlayerController
audioPlayer
shortSound
contentURL Model
NSURL
AVAudioPlayer
SystemSoundID
Each button needs an action method. Declare these methods (and an instance variable for one of the buttons whose title will change during runtime) in =,.)/64/8,-C##D,4,*/",'!. @)+",-$@&)40)2$/6SS#$0$*)+$. Finally, connect the /5.)1S5""1+ outlet to the top button labeled Play Audio File.
246
&KDSWHU3OD\LQJ$XGLRDQG9LGHRL3KRQH3URJUDPPLQJ7KH%LJ1HUG5DQFK*XLGH,6%1 3UHSDUHGIRUGRXJF#VWDQIRUGDOXPQLRUJGRXJFKDQJ &RS\ULJKW%LJ1HUG5DQFK,QF7KLVGRZQORDGILOHLVPDGHDYDLODEOHIRUSHUVRQDOXVHRQO\DQGLVVXEMHFWWRWKH7HUPVRI6HUYLFH$Q\RWKHUXVHUHTXLUHVSULRUZULWWHQFRQVHQWIURP WKHFRS\ULJKWRZQHU8QDXWKRUL]HGXVHUHSURGXFWLRQDQGRUGLVWULEXWLRQDUHVWULFWO\SURKLELWHGDQGYLRODWHDSSOLFDEOHODZV$OOULJKWVUHVHUYHG
Playing System Sounds
Figure 20.3. Interface Builder connections
audioButton
playAudioFile:
playVideoFile:
playShortSound:
Save =/)+K)+.1&'P)( and quit Interface Builder. In order to build and run the application without warnings, you will need a stub method for each of the ?SC0")1+s you declared. In =,.)/64/8,-C##D,4,*/",'2, implement them as follows. UGV"76'+&,-WS0)26?@&,:&0$XV&@W8$-@$/ Y GGG(14>,%&$40)2$/.,-+/,00$/ can also play streaming video from a URL somewhere off in internet land. However, you should seriously consider the problems of this approach on a mobile device. If you have the choice, either bundle a movie file with the application or have your application download the video to the application sandbox after a user launches it. If you do not have the choice, be aware that Apple can reject your application if a video file is too large to be transported over the network in an appropriate amount of time. For example, your application can be rejected if it claims to support the original iPhone (using the Edge network) and streams video at more than 1MB per second. In order to use >4>,%&$40)2$/.,-+/,00$/, you must add yet another framework to your project. Add the MediaPlayer framework to your project and import the appropriate header file at the top of =,.)/64/8,-C##D,4,*/",'!. Create an instance variable in >$@&)40)2$/6SS#$0$*)+$ for the movie player, as well. m&3S,/+Gi>$@&)40)2$/J>$@&)40)2$/kLn @)+",-4>,%&$40)2$/.,-+/,00$/ will take over the screen and play the video. When you instantiate an >4>,%&$40)2$/.,-+/,00$/, it immediately begins loading the video you ask it to. This loading happens on another thread so that your application does not halt while the video loads. A video loaded from disk will most likely be ready to play immediately, but one being streamed from the internet may take some time to load. You may not want to play an unloaded video right away. Therefore, you can register for a preloading notification from the video player. This notification will be sent to interested observers when enough data has been loaded to play the video without pause. AAAA\\9:91")