Multimedia playback of iOS development

Source: Internet
Author: User
<span id="Label3"></p><p><p>The IOS SDK provides a number of convenient ways to play Multimedia. This article will use these SDKs to make a demo to tell you how to use them to play audio Files.</p></p><p><p><strong>Audiotoolbox Framework</strong></p></p><p><p>Use the Audiotoolbox Framework. This framework registers the shorter sounds on the system sound Service. The voice that is registered to the system sound service is called the system sounds. It must meet the following conditions.</p></p><p><p>1, the playback time can not exceed 30 seconds</p></p><p><p>2, the data must be PCM or IMA4 stream format</p></p><p><p>3. must be packaged into one of the following three formats: Core audio format (. caf), waveform audio (. wav), or audio interchange File (. Aiff)</p></p><p><p>The sound file must be placed under the Device's local folder. Registering this sound file through the Audioservicescreatesystemsoundid method Audioservicescreatesystemsoundid the Cfurlref object that requires the URL of the sound file. Look at the following registration code:</p></p><pre><pre>#import <AudioToolbox/AudioToolbox.h><br>@interface Mediaplayerviewcontroller:uiviewcontroller<br>{<br> Iboutlet UIButton *audiobutton;<br> Systemsoundid shortsound;<br>}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>-(id) init <br> {<br> self = [super initwithnibname:@ "mediaplayerviewcontroller" bundle:nil]; <br> If (self) {<br>//Get the full path of Sound12.aif <br> NSString *soundpath = [[nsbundle mainbundle] pathfor resource:@ "Sound12" <br> oftype:@ "aif"]; <br>//If This file is actually in the bundle ... <br> If (soundpath) {<br>//Create a file URL with the This path <br> Nsurl *soundurl = [nsurl fi leurlwithpath:soundpath]; <br> <br>//Register sound file located at the URL as a system sound <br> osstatus err = Aud Ioservicescreatesystemsoundid (cfurlref) soundurl, <br> &sho rtsound); <br> If (err! = Kaudioservicesnoerror) <br> NSLog (@ "Could not load%@, error code:%d", soundurl, err); <br>} <br>} <br> Return self; <br>} </pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>This allows you to play the sound using the following code:</p></p><pre><pre>-(ibaction) playshortsound: (id) Sender<br>{<br> Audioservicesplaysystemsound (shortsound);<br>}</pre></pre><p><p>Use the following code to add a vibrating effect:</p></p><pre><pre>-(ibaction) playshortsound: (id) Sender<br>{<br> Audioservicesplaysystemsound (shortsound);<br> Audioservicesplaysystemsound (ksystemsoundid_vibrate);<br>}</pre></pre><p><p><strong>Avfoundation Framework</strong></p></p><p><p>For audio files that have been compressed or more than 30 seconds, you can use the Avaudioplayer class. This class is defined in the Avfoundation Framework.</p></p><p><p>Below we use this class to play a MP3 audio File. The first step is to introduce the Avfoundation framework and then add the following code to MediaPlayerViewController.h:</p></p><pre><pre>#import <AVFoundation/AVFoundation.h><br>@interface Mediaplayerviewcontroller:uiviewcontroller <AVAudioPlayerDelegate><br>{<br> Iboutlet UIButton *audiobutton;<br> Systemsoundid shortsound;<br> Avaudioplayer *audioplayer;</pre></pre><p><p>The Avaudioplayer class also needs to know the path to the audio file, creating a Avaudioplayer instance using the following code:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>-(id) Init<br>{<br> Self = [super initwithnibname:@ "mediaplayerviewcontroller" bundle:nil];<br> <br> If (self) {<br> <br> NSString *musicpath = [[nsbundle mainbundle] pathforresource:@ "Music"<br> oftype:@ "mp3"];<br> If (musicpath) {<br> Nsurl *musicurl = [nsurl fileurlwithpath:musicpath];<br> <br> error:nil];<br> [audioplayer setdelegate:self];<br> }<br> NSString *soundpath = [[nsbundle mainbundle] pathforresource:@ "Sound12"<br> oftype:@ "aif"];</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>We can start playing this mp3 file in a Button's click event, such as:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>-(ibaction) playaudiofile: (id) Sender<br>{<br> If ([audioplayer isplaying]) {<br> Stop playing audio and change text of button<br> [audioplayer stop];<br> [sender settitle:@ "Play Audio File"<br> forstate:uicontrolstatenormal];<br> }<br> else {<br> Start playing audio and change text of button so<br> User can tap to stop playback<br> [audioplayer play];<br> [sender settitle:@ "Stop Audio File"<br> forstate:uicontrolstatenormal];<br> }<br>}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>So running our program, you can play the Music.</p></p><p><p>This class corresponds to a <span>avaudioplayerdelegate with two delegate methods. One is audioplayerdidfinishplaying:successfully: triggered when audio playback is Complete. When playback is complete, you can reset the playback Button's text back to: play Audio File</span></p></p><pre><pre>-(void) audioplayerdidfinishplaying: (avaudioplayer *) player<br> Successfully: (BOOL) Flag<br>{<br> [audiobutton settitle:@ "Play Audio File"<br> forstate:uicontrolstatenormal];<br>}</pre></pre><p><p>The other is audioplayerendinterruption: when the program is interrupted by an external application, it is triggered back to the Application. When you return to this application here, continue playing the Music.</p></p><pre><pre>-(void) audioplayerendinterruption: (avaudioplayer *) player<br>{<br> [audioplayer play];<br>}</pre></pre><p><p><strong>MediaPlayer Framework</strong></p></p><p><p>To play a movie file:</p></p><p><p>You can use MPMoviePlayerController to play movie files in the IOS Sdk. however, There are strict formatting requirements for playing movie files on iOS devices, and only movie files in the following two formats can be played.</p></p><p><p>H.264 (Baseline Profile Level 3.0)<br>Mpeg-4 Part 2 Video</p></p><p><p>fortunately, you can use itunes to convert your files to the top two formats First.<br>MPMoviePlayerController can also play video files on the Internet. however, It is recommended that you download the video file locally and then play it. If you do not do this, iOS may refuse to play a very large video file.</p></p><p><p>This class is defined in the MediaPlayer Framework. In your application, add this reference first, and then modify the <span>MediaPlayerViewController.h File. </span></p></p><pre><pre>#import <MediaPlayer/MediaPlayer.h><br>@interface Mediaplayerviewcontroller:uiviewcontroller <AVAudioPlayerDelegate><br>{<br> MPMoviePlayerController *movieplayer;</pre></pre><p><p>Below we use this class to play a video file in. m4v format. Similar to the previous one, a URL path is Required.</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>-(id) Init<br>{<br> Self = [super initwithnibname:@ "mediaplayerviewcontroller" bundle:nil];<br> If (self) {<br> <br> <br> oftype:@ "m4v"];<br> If (moviepath) {<br> Nsurl *movieurl = [nsurl fileurlwithpath:moviepath];<br> <br> initwithcontenturl:movieurl];<br> }</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>MPMoviePlayerController has a view to show the player controls, and we'll show this player in the Viewdidload Method.</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>-(void) Viewdidload<br>{<br> [self view] Addsubview:[movieplayer view];<br> float halfheight = [[self view] bounds].size.height/2.0;<br> Float width = [[self view] bounds].size.width;<br> [[movieplayer view] setframe:cgrectmake (0, halfheight, width, halfheight)];<br>}</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>There is also a Mpmovieplayerviewcontroller class for playing video files in full-screen, with the same usage as Mpmovieplayercontroller.</p></p><pre><pre>Mpmovieplayerviewcontroller *playerviewcontroller =<br> [[mpmovieplayerviewcontroller alloc] initwithcontenturl:movieurl];<br> [viewcontroller presentmovieplayerviewcontrolleranimated:playerviewcontroller];</pre></pre><p><p>When we listen to music, we can do other things with the iphone, this time we need the player to run in the background, we just need to make a simple setup in the Application.</p></p><p><p>1, Add a Required background modes node in the Info property list, It is an array, set the first item to set the app plays Audio.</p></p><p><p>2. Add the following code to the code that plays Mp3:</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre> If (musicpath) {<br> Nsurl *musicurl = [nsurl fileurlwithpath:musicpath];<br> [[avaudiosession sharedinstance]<br> Setcategory:avaudiosessioncategoryplayback error:nil];<br> Audioplayer = [[avaudioplayer alloc] initwithcontentsofurl:musicurl<br> error:nil];<br> [audioplayer setdelegate:self];<br> }</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p>The ability to play music in the background is not visible in the simulator, only on the real machine.</p></p><p><p></p></p><p><p><strong>Summary:</strong> This article explains in detail the classes used to play audio files in the iOS sdk, and the code for this article provides downloads later in this article.</p></p><p><p><strong>Code: Http://files.cnblogs.com/zhuqil/MediaPlayer.zip</strong></p></p><p><p>Multimedia playback of iOS development</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.