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:
- #import <AudioToolbox/AudioToolbox.h>
- @interface Mediaplayerviewcontroller:uiviewcontroller
- {
- Iboutlet UIButton *audiobutton;
- Systemsoundid Shortsound;
- }
- -(ID) init
- {
- self = [Super initwithnibname:@"Mediaplayerviewcontroller" bundle:nil];
- if (self) {
- //Get The full path of Sound12.aif
- NSString *soundpath = [[NSBundle mainbundle] pathforresource:@"Sound12"
- oftype:@"AIF"];
- //If This file was actually in the bundle ...
- if (soundpath) {
- //Create a file URL with the This path
- Nsurl *soundurl = [Nsurl Fileurlwithpath:soundpath];
- //Register sound file located at the URL as a system sound
- Osstatus err = Audioservicescreatesystemsoundid ((cfurlref) Soundurl,
- &shortsound);
- if (err! = Kaudioservicesnoerror)
- NSLog (@"Could not load%@, error code:%d", soundurl, err);
- }
- }
- return self;
- }
This allows you to play the sound using the following code:
- -(Ibaction) Playshortsound: (ID) sender
- {
- Audioservicesplaysystemsound (Shortsound);
- }
Use the following code to add a vibrating effect:
- -(Ibaction) Playshortsound: (ID) sender
- {
- Audioservicesplaysystemsound (Shortsound);
- Audioservicesplaysystemsound (ksystemsoundid_vibrate);
- }
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:
- #import <AVFoundation/AVFoundation.h>
- @interface Mediaplayerviewcontroller:uiviewcontroller <AVAudioPlayerDelegate>
- {
- Iboutlet UIButton *audiobutton;
- Systemsoundid Shortsound;
- Avaudioplayer *audioplayer;
The Avaudioplayer class also needs to know the path to the audio file, creating a Avaudioplayer instance using the following code:
- -(ID) init
- {
- self = [Super initwithnibname:@"Mediaplayerviewcontroller" bundle:nil];
- if (self) {
- NSString *musicpath = [[NSBundle mainbundle] pathforresource:@"Music"
- oftype:@"MP3"];
- if (musicpath) {
- Nsurl *musicurl = [Nsurl Fileurlwithpath:musicpath];
- Audioplayer = [[Avaudioplayer alloc] Initwithcontentsofurl:musicurl
- Error:nil];
- [Audioplayer setdelegate:self];
- }
- NSString *soundpath = [[NSBundle mainbundle] pathforresource:@"Sound12"
- oftype:@"AIF"];
We can start playing this MP3 file in a button's click event, such as:
- -(Ibaction) Playaudiofile: (ID) sender
- {
- if ([Audioplayer isplaying]) {
- //Stop playing audio and change text of button
- [Audioplayer stop];
- [Sender settitle:@"Play Audio File"
- Forstate:uicontrolstatenormal];
- }
- else {
- //Start playing audio and change text of button so
- //user can tap to stop playback
- [Audioplayer play];
- [Sender settitle:@"Stop Audio File"
- Forstate:uicontrolstatenormal];
- }
- }
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
- -(void) audioplayerdidfinishplaying: (Avaudioplayer *) player
- Successfully: (BOOL) flag
- {
- [Audiobutton settitle:@"Play Audio File"
- Forstate:uicontrolstatenormal];
- }
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.
- -(void) Audioplayerendinterruption: (Avaudioplayer *) player
- {
- [Audioplayer play];
- }
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.
- #import <MediaPlayer/MediaPlayer.h>
- @interface Mediaplayerviewcontroller:uiviewcontroller <AVAudioPlayerDelegate>
- {
- 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.
- -(ID) init
- {
- self = [Super initwithnibname:@"Mediaplayerviewcontroller" bundle:nil];
- if (self) {
- NSString *moviepath = [[NSBundle mainbundle] pathforresource:@"Layers"
- oftype:@"M4V"];
- if (moviepath) {
- Nsurl *movieurl = [Nsurl Fileurlwithpath:moviepath];
- MoviePlayer = [[MPMoviePlayerController alloc]
- Initwithcontenturl:movieurl];
- }
MPMoviePlayerController has a view to show the player controls, and we'll show this player in the Viewdidload method.
- -(void) Viewdidload
- {
- [Self view] addsubview:[movieplayer view];
- float halfheight = [[self view] bounds].size.height/2.0;
- float width = [[self view] bounds].size.width;
- [[MoviePlayer View] setframe:cgrectmake (0, Halfheight, width, halfheight)];
- }
There is also a Mpmovieplayerviewcontroller class for playing video files in full-screen, with the same usage as MPMoviePlayerController.
- Mpmovieplayerviewcontroller *playerviewcontroller =
- [[Mpmovieplayerviewcontroller alloc] initwithcontenturl:movieurl];
- [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:
- if (Musicpath) {
- Nsurl *musicurl = [Nsurl Fileurlwithpath:musicpath];
- [[Avaudiosession Sharedinstance]
- Setcategory:avaudiosessioncategoryplayback Error:nil];
- Audioplayer = [[Avaudioplayer alloc] Initwithcontentsofurl:musicurl
- Error:nil];
- [Audioplayer setdelegate:self];
- }
The ability to play music in the background is not visible in the simulator, only on the real machine.
Multimedia playback of iOS development