Multimedia playback of iOS development

Source: Internet
Author: User
Tags file url

this article reprinted to http://mobile.51cto.com/iphone-423284.htm

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.

AD:2014WOT Global Software Technology Summit Beijing Station course video release

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.

Audiotoolbox Framework

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.

1, the playback time can not exceed 30 seconds

2, the data must be PCM or IMA4 stream format

3. Must be packaged into one of the following three formats: Core audio format (. CAF), waveform audio (. wav), or audio interchange File (. aiff)

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:

    1. #import <AudioToolbox/AudioToolbox.h>
    2. @interface Mediaplayerviewcontroller:uiviewcontroller
    3. {
    4. Iboutlet UIButton *audiobutton;
    5. Systemsoundid Shortsound;
    6. }
  1. -(ID) init
  2. {
  3. self = [Super initwithnibname:@"Mediaplayerviewcontroller" bundle:nil];
  4. if (self) {
  5. //Get The full path of Sound12.aif
  6. NSString *soundpath = [[NSBundle mainbundle] pathforresource:@"Sound12"
  7. oftype:@"AIF"];
  8. //If This file was actually in the bundle ...
  9. if (soundpath) {
  10. //Create a file URL with the This path
  11. Nsurl *soundurl = [Nsurl Fileurlwithpath:soundpath];
  12. //Register sound file located at the URL as a system sound
  13. Osstatus err = Audioservicescreatesystemsoundid ((cfurlref) Soundurl,
  14. &shortsound);
  15. if (err! = Kaudioservicesnoerror)
  16. NSLog (@"Could not load%@, error code:%d", soundurl, err);
  17. }
  18. }
  19. return self;
  20. }

This allows you to play the sound using the following code:

    1. -(Ibaction) Playshortsound: (ID) sender
    2. {
    3. Audioservicesplaysystemsound (Shortsound);
    4. }

Use the following code to add a vibrating effect:

    1. -(Ibaction) Playshortsound: (ID) sender
    2. {
    3. Audioservicesplaysystemsound (Shortsound);
    4. Audioservicesplaysystemsound (ksystemsoundid_vibrate);
    5. }

Avfoundation Framework

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.

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:

    1. #import <AVFoundation/AVFoundation.h>
    2. @interface Mediaplayerviewcontroller:uiviewcontroller <AVAudioPlayerDelegate>
    3. {
    4. Iboutlet UIButton *audiobutton;
    5. Systemsoundid Shortsound;
    6. Avaudioplayer *audioplayer;

The Avaudioplayer class also needs to know the path to the audio file, creating a Avaudioplayer instance using the following code:

  1. -(ID) init
  2. {
  3. self = [Super initwithnibname:@"Mediaplayerviewcontroller" bundle:nil];
  4. if (self) {
  5. NSString *musicpath = [[NSBundle mainbundle] pathforresource:@"Music"
  6. oftype:@"MP3"];
  7. if (musicpath) {
  8. Nsurl *musicurl = [Nsurl Fileurlwithpath:musicpath];
  9. Audioplayer = [[Avaudioplayer alloc] Initwithcontentsofurl:musicurl
  10. Error:nil];
  11. [Audioplayer setdelegate:self];
  12. }
  13. NSString *soundpath = [[NSBundle mainbundle] pathforresource:@"Sound12"
  14. oftype:@"AIF"];

We can start playing this MP3 file in a button's click event, such as:

  1. -(Ibaction) Playaudiofile: (ID) sender
  2. {
  3. if ([Audioplayer isplaying]) {
  4. //Stop playing audio and change text of button
  5. [Audioplayer stop];
  6. [Sender settitle:@"Play Audio File"
  7. Forstate:uicontrolstatenormal];
  8. }
  9. else {
  10. //Start playing audio and change text of button so
  11. //user can tap to stop playback
  12. [Audioplayer play];
  13. [Sender settitle:@"Stop Audio File"
  14. Forstate:uicontrolstatenormal];
  15. }
  16. }

So running our program, you can play the music.

This class corresponds to a 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

    1. -(void) audioplayerdidfinishplaying: (Avaudioplayer *) player
    2. Successfully: (BOOL) flag
    3. {
    4. [Audiobutton settitle:@"Play Audio File"
    5. Forstate:uicontrolstatenormal];
    6. }

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.

    1. -(void) Audioplayerendinterruption: (Avaudioplayer *) player
    2. {
    3. [Audioplayer play];
    4. }

MediaPlayer Framework

To play a movie file:

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.

H.264 (Baseline Profile level 3.0)
mpeg-4 Part 2 video

Fortunately, you can use itunes to convert your files to the top two formats first.
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.

This class is defined in the MediaPlayer framework. In your application, add this reference first, and then modify the MediaPlayerViewController.h file.

    1. #import <MediaPlayer/MediaPlayer.h>
    2. @interface Mediaplayerviewcontroller:uiviewcontroller <AVAudioPlayerDelegate>
    3. {
    4. MPMoviePlayerController *movieplayer;

Below we use this class to play a video file in. m4v format. Similar to the previous one, a URL path is required.

  1. -(ID) init
  2. {
  3. self = [Super initwithnibname:@"Mediaplayerviewcontroller" bundle:nil];
  4. if (self) {
  5. NSString *moviepath = [[NSBundle mainbundle] pathforresource:@"Layers"
  6. oftype:@"M4V"];
  7. if (moviepath) {
  8. Nsurl *movieurl = [Nsurl Fileurlwithpath:moviepath];
  9. MoviePlayer = [[MPMoviePlayerController alloc]
  10. Initwithcontenturl:movieurl];
  11. }

MPMoviePlayerController has a view to show the player controls, and we'll show this player in the Viewdidload method.

    1. -(void) Viewdidload
    2. {
    3. [Self view] addsubview:[movieplayer view];
    4. float halfheight = [[self view] bounds].size.height/2.0;
    5. float width = [[self view] bounds].size.width;
    6. [[MoviePlayer View] setframe:cgrectmake (0, Halfheight, width, halfheight)];
    7. }

There is also a Mpmovieplayerviewcontroller class for playing video files in full-screen, with the same usage as MPMoviePlayerController.

    1. Mpmovieplayerviewcontroller *playerviewcontroller =
    2. [[Mpmovieplayerviewcontroller alloc] initwithcontenturl:movieurl];
    3. [Viewcontroller Presentmovieplayerviewcontrolleranimated:playerviewcontroller];

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.

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.

2. Add the following code to the code that plays MP3:

  1. if (Musicpath) {
  2. Nsurl *musicurl = [Nsurl Fileurlwithpath:musicpath];
  3. [[Avaudiosession Sharedinstance]
  4. Setcategory:avaudiosessioncategoryplayback Error:nil];
  5. Audioplayer = [[Avaudioplayer alloc] Initwithcontentsofurl:musicurl
  6. Error:nil];
  7. [Audioplayer setdelegate:self];
  8. }

The ability to play music in the background is not visible in the simulator, only on the real machine.

Multimedia playback of iOS development

Related Article

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.