IOS 45: multimedia operations, ios45 multimedia
1. Audio 1> brief introduction to audio implementation
There are four methods for playing audio in iOS:
System Sound Services)
OpenAL (cross-platform open-source audio processing interface)
Audio Queue Services (playing and recording Audio service)
AVAudioPlayer (Advanced Audio Player)
2> System Sound Services (System Sound service)
① System Sound Services is the most basic and simple Sound playback service. You can call the AudioServicesPlaySystemSound function to play some simple audio files.
② Limitations:
1. The sound length must be less than 30 seconds
2. Format: IMA4
3. the playback progress cannot be controlled.
4. Play the sound immediately after calling the Method
5. No loop playback or stereo sound playback
③ Instance code
CFBundleRef mainBundle; SystemSoundID soundFileObject; mainBundle = Response (); CFURLRef soundFileURLRef = Response (mainBundle, CFSTR ("-"), CFSTR ("wav"), NULL ); audioServicesCreateSystemSoundID (soundFileURLRef, & soundFileObject); AudioServicesPlaySystemSound (soundFileObject );
3> OpenAL
① Introduction
OpenAL is a cross-platform open-source audio processing interface.
The most suitable audio for Game Development
OpenAL contains three entities: Listener, Source, and Buffer)
Audio Queue Services is mainly used for recording Audio. to simplify the processing of Audio files, you usually need to use AudioFileServices.
② Development steps
1. Get device
2. Associate context with device
3. Put data into the buffer
4. link the buffer to a source
5. Playback source
OpenAL Web site: http://www.devdiv.com/thread-19636-1-1.html
Http://www.cocoachina.com/bbs/read.php? Tid-112679-page-1.html
Relative underlying API reference: http://blog.csdn.net/midfar/article/details/7233454
4> AVAudioPlayer Overview
We can regard AVAudioPlayer as an advanced player that supports a wide range of audio formats, as follows:
AAC
AMR (AdaptiveMulti-Rate, aformatforspeech)
ALAC (AppleLossless)
ILBC (internetLowBitrateCodec, anotherformatforspeech)
IMA4 (IMA/ADPCM)
LinearPCM (uncompressed)
MP3 MPEG-1audiolayer3)
5> AVAudioPlayer advantages
Supports more formats
You can play audio files of any length.
Supports loop playback.
You can play multiple audio files simultaneously.
Controls the playback progress and starts playing from any point of the audio.
6> AVAudioPlayer development steps
- Step 1: AVAudioPlayer is included in the AVFoundation framework. Therefore, you must first import the audio framework AVFoundation. framework during development.
Introduce header files
#import <AVFoundation/AVFoundation.h>
- Step 2: When initializing AVAudioPlayer, a playback file is required.
AVAudioPlayer * avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: file path error: nil];
- Step 3: Important attributes
Set the initial volume size audioPlayer. volume = 1; (0.0 ~ 1.0)
Set the number of music playback times audioPlayer. numberOfLoops =-1; (if it is a negative number, it will be played cyclically)
Playback progress audioPlayer. currentTime = 0;
- Step 4: important methods
Pre-Playback
[audioPlayer prepareToPlay];
Play
[audioPlayer play];
Pause
[audioPlayer pause];
Stop
[audioPlayer stop];
Protocol: AVAudioPlayerDelegate
Proxy method called when playback is complete
-(Void) audioPlayerDidFinishPlaying :( AVAudioPlayer *) player successfully :( BOOL) flag;
Playback decoding failed
-(Void) audioPlayerDecodeErrorDidOccur :( AVAudioPlayer *) player error :( NSError *) error;
7> simple code example:
// Lazy loading-(AVAudioPlayer *) player {if (_ player = nil) {NSString * urlString = [[NSBundle mainBundle] pathForResource: @ "South expedition-proud young man" ofType: @ "mp3"]; NSURL * url = [NSURL fileURLWithPath: urlString]; _ player = [[AVAudioPlayer alloc] initWithContentsOfURL: url error: nil]; _ player. delegate = self; // sets the default volume _ player. volume = 0.5;} return _ player;} // The playback button responds to the Event-(IBAction) play :( id) sender {[self. player play];} // pause button RESPONSE event-(IBAction) pause :( id) sender {[self. player pause];} // the stop button to respond to the Event-(IBAction) stop :( id) sender {[self. player stop];} // The volume slider responds to the Event-(IBAction) changeVolume :( UISlider *) sender {self. player. volume = sender. value ;}
I have a complete exercise code on github. If you are interested, you can check it out!
8> background audio playback
- First, modify the configuration file in Info. in the plist file, add UIBackGroundModes to add Audio that includes Audio playing Audio and video in the background, location to keep the location information of the current user, and voip to use the network phone. The above fields are added to notify the system framework. When the application enters the background, it requests to continue playing in the background for a period of time. The specific playback duration is determined by the UIBackGroundTask. You can also use local notifications to pre-set local notifications for applications to run in the background.
Background audio settings
AVAudioSession *session = [AVAudioSession sharedInstance]; [session setActive:YES error:nil]; [session setCategory:AVAudioSessionCategoryPlayback error:nil];
- Allow the app to support remote control events
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
2. Video 1> AVPlayer
In iOS, video playback uses AVPlayer (included in the AVFoundation framework) and AVAudioPlayer. However, AVPlayer is more powerful and can be used to play audio or video. In addition, AVPlayer can directly play online audio.
2> video playback steps
- Step 1: import a framework AVFoundation. framework that supports video playback.
Introduce header file code
#import <AVFoundation/AVFoundation.h>
- Step 2: Obtain the playback address
NSString * playString = @ "http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"; // playback address NSURL * playURL = [NSURL URLWithString: playString];
- Step 3: Create an AVPlayerItem object based on the playback URL
AVPlayerItem can be used to obtain video information, current playback time, and total time.
AVPlayerItem *item = [[AVPlayerItem alloc] initWithURL:playURL];
Important attributes of AVPlayerItem
Status
AVPlayerStatusUnknown (indicating an unknown problem occurred during video playback)
AVPlayerStatusReadyToPlay (indicating that the video can be played and the play method can be called)
AVPlayerStatusFailed (indicating that the video cannot be played)
LoadedTimeRange: indicates the cached progress. Listening to this attribute can update the cache progress in the UI, which is also a useful attribute.
- Step 4: Initialize the AVPlayer object based on AVPlayerItem
@interface ViewController () @property(nonatomic, strong)AVPlayer *player;@end self.player = [[AVPlayer alloc] initWithPlayerItem:item];
- Step 5: add the AVPlayerLayer to the Layer on the page to be played
// Set the playback page AVPlayerLayer * layer = [AVPlayerLayer playerLayerWithPlayer: _ player]; // set the playback page size layer. frame = CGRectMake (0,100, [UIScreen mainScreen]. bounds. size. width, 300); // sets the background color layer. backgroundColor = [UIColor cyanColor]. CGColor; // set the ratio between the playback window and the current view to display the content layer. videoGravity = AVLayerVideoGravityResizeAspect; // Add the playback view to the view [self. view. layer addSublayer: layer];
// Play [self. player play];
- Step 7: play the video at the specified time
[Self. player seekToTime: CMTimeMakeWithSeconds (progress, // sets the number of frames per second self. player. currentTime. timescale) completionHandler: ^ (BOOL finished) {}]; // set the volume self. player. volume = 1.0f; // current playback time self. player. currentTime
- Step 8: notification of playback completion
// When the playback is complete, you can register the notification and make different responses as needed. AVPlayerItemDidPlayToEndTimeNotification
You can add a playback completion notification by setting the observer.
-(Void) addNotification {// Add a playback completion notification to AVPlayerItem [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (playbackFinished :) name: AVPlayerItemDidPlayToEndTimeNotification object: self. player. currentItem];}-(void) playbackFinished :( NSNotification *) notification {NSLog (@ "video playback completed. ");}
The preceding six steps can be used to play a video on the iOS client;
AVPlayerltem resource management object. It is used to switch between video playback and different items, rather than creating a new AVPlayer.
Some important attributes of AVPlayerItem allow us to develop custom videos.
Instance code:
-(Void) viewDidLoad {[super viewDidLoad]; NSString * playString = @ "http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"; NSURL * url = [NSURL URLWithString: playString]; // local video // NSURL * url = [NSURL fileURLWithPath: [[NSBundle mainBundle] pathForResource: @ "1457622279563.mp4" ofType: nil]; // set the playback project AVPlayerItem * item = [[AVPlayerItem alloc] initWithURL: url]; // initialize the player Object self. player = [[AVPlayer alloc] initWithPlayerItem: item]; // sets the AVPlayerLayer * layer = [AVPlayerLayer playerLayerWithPlayer: _ player] On the playback page. // sets the playback page size layer. frame = CGRectMake (0,100, [UIScreen mainScreen]. bounds. size. width, 300); // sets the background color layer. backgroundColor = [UIColor cyanColor]. CGColor; // set the ratio between the playback window and the current view to display the content layer. videoGravity = AVLayerVideoGravityResizeAspect; // Add the playback view to the view [self. view. layer addSublayer: layer]; // sets the default value of the playback progress self. progressSlider. value = 0; // set the default value of the playback volume self. player. volume = 1.0f; [self addNotification] ;}# response method of The pragma mark-start playback button-(IBAction) startPlayer :( UIButton *) sender {[self. player play] ;}# pragma mark-response method of the pause playback button-(IBAction) puasePlayer :( UIButton *) sender {[self. player pause] ;}# pragma mark-response method for the change progress button-(IBAction) changeProgress :( UISlider *) sender {self. sumPlayOperation = _ player. currentItem. duration. value/_ player. currentItem. duration. timescale; // CMTimeMake (a, B) a indicates the current time, and B indicates the number of frames per second [_ player seekToTime: CMTimeMakeWithSeconds (sender. value * self. sumPlayOperation, _ player. currentTime. timescale) completionHandler: ^ (BOOL finished) {[self. player play] ;}] ;}- (void) addNotification {// Add a playback completion notification to AVPlayerItem [[nsicationicationcenter defaultCenter] addObserver: self selector: @ selector (playbackFinished :) name: AVPlayerItemDidPlayToEndTimeNotification object: self. player. currentItem];}-(void) playbackFinished :( NSNotification *) notification {NSLog (@ "video playback completed. ");}