IOS advanced learning-multimedia and ios advanced multimedia

Source: Internet
Author: User

IOS advanced learning-multimedia and ios advanced multimedia

I. Audio

1. There are four methods for playing audio in iOS:

  • System Sound Services ).
  • OpenAL (cross-platform open-source audio processing interface ).
  • Audio Queue Services (Stream play and recording Audio service ).
  • AVAudioPlayer (Advanced Audio Player ).
  • Here we will mainly introduce System Sound Services and AVAudioPlayer.

2. System Sound Services

  • System Sound Services is the most basic and simple Sound playback service. by calling the AudioServicesPlaySystemSound function, you can play some simple audio files.
  • Applicable scenarios: Suitable for playing small prompts or warning sounds, AudioToolbox. framework is a set of C-language-based frameworks that are used to play Sound effects. In essence, short audio is registered to the System Sound Service ).
  • Limitations:
  • Sound length less than 30 s
  • Format: PCM or IMA4
  • The playback progress cannot be controlled.
  • Play the sound immediately after calling the Method
  • No loop playback or stereo sound playback
  • Sample Code: (use storyboard to create a button and associate it with the Click Event)
# Import "ViewController. h "// introduce the header file # import <AudioToolbox/AudioToolbox. h> @ interface ViewController () @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib .} # pragma mark-playing music-(IBAction) playAction :( id) sender {// obtain the main application package CFBundleRef mainBundle; SystemSoundID soundFileObject; mainBundle = CFBundleGetMainBu Ndle (); // URL of the audio file CFURLRef soundFileURLRef = CFBundleCopyResourceURL (mainBundle, CFSTR ("-"), CFSTR ("wav"), NULL ); // register the sound to the system AudioServicesCreateSystemSoundID (soundFileURLRef, & soundFileObject); // The AudioServicesAddSystemSoundCompletion method adds the CallBack function for audio playback. With the CallBack function, we can solve many problems, for example, System Sound Services does not support loop playback. AudioServicesAddSystemSoundCompletion (soundFileObject, NULL, NULL, & completionCallback, (_ bridge void *) self); // play the registered sound, (this sentence code, can be called anywhere in this class, not limited to this method) AudioServicesPlaySystemSound (soundFileObject); // shake the phone // AudioServicesPlaySystemSound (kSystemSoundID_Vibrate);} static void completionCallback (SystemSoundID, ssID, void * clientData) {// Play again after sound play completion NSLog (@ "playback complete-input ID:-% u, input parameter: % @", (unsigned int) ssID, clientData); AudioServicesPlaySystemSound (ssID);} void playFinished (SystemSoundID ssID, void * clientData) {// unsigned long ID = ssID; // The ssID cannot be printed directly as a parameter, NSLog (@ "playback complete-input ID is-% u, input parameter is % @", (unsigned int) ssID, clientData); AudioServicesPlaySystemSound (ssID ); // The function executed after the removal is complete // AudioServicesRemoveSystemSoundCompletion (ssID); // release the custom system sound Based on the ID // AudioServicesDisposeSystemSoundID (ssID);}-(void) didReceiveMemoryWarning {[super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated .} @ end

3. AVAudioPlayer: an advanced player that supports a wide range of audio formats, as shown below:

  • AAC
  • AMR (AdaptiveMulti-Rate, aformatforspeech)
  • ALAC (AppleLossless)
  • ILBC (internetLowBitrateCodec, anotherformatforspeech)
  • IMA4 (IMA/ADPCM)
  • LinearPCM (uncompressed)
  • MP3 MPEG-1audiolayer3)

4. Advantages of AVAudioPlayer:

  • 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.

5. development steps:

  • AVAudioPlayer is included in the AVFoundation framework. Therefore, you must first import the audio framework AVFoundation. framework during development. Note: import the header file # import <AVFoundation/AVFoundation. h>.
  • When initializing AVAudioPlayer, you need to give a playback file: AVAudioPlayer * avAudioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: file path error: nil];
  • Important attributes: 1. Set the initial volume size audioPlayer. volume = 1; (0.0 ~ 1.0); 2. Set the number of music playback times audioPlayer. numberOfLoops =-1 (loop playback as long as it is a negative number); 3. Playback progress audioPlayer. currentTime = 0;
  • Important methods: 1. Pre-play: [audioPlayer prepareToPlay]; 2. play: [audioPlayer play]; 3. pause: [audioPlayer pause]; 4. stop: [audioPlayer stop];
  • Protocol (AVAudioPlayerDelegate): 1. proxy method called when playback is complete:-(void) audioPlayerDidFinishPlaying :( AVAudioPlayer *) player successfully :( BOOL) flag; playback decoding failed:-(void) playback :( AVAudioPlayer *) player error :( NSError *) error.
  • Sample Code: github

Ii. 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. Development steps

  • 1) import the AVFoundation. framework that supports video playback, and introduce the header file # import <AVFoundation/AVFoundation. h>.
  • 2) obtain the playback address.
  • 3) create an AVPlayerItem object based on the playback URL (AVPlayerItem can obtain video information, current playback time, total time, and so on ).
  • 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.
  • 4) initialize the AVPlayer object based on AVPlayerItem.
  • 5) videos cannot be displayed simply by using the AVPlayer class. This class does not have a real view. You need to add the video Layer to the AVPlayerLayer. Finally, you need to add the AVPlayerLayer to the Layer on the playback page.
  • 6) AVPlayerLayer settings and playback.
  • 7) play the video at the specified time.
  • 8) Notification of playback completion.
  • The preceding six steps can be used to play a video on the iOS client.
  • Sample Code:
# Import "ViewController. h "# import <AVFoundation/AVFoundation. h> # import <AVKit/AVKit. h> @ interface ViewController () // controls video playback @ property (weak, nonatomic) IBOutlet UISlider * progressSlider; /// declare the control attribute of the video to be played (which can also be used to play audio) @ property (nonatomic, strong) AVPlayer * player; // The total playback duration @ property (nonatomic, assign) CGFloat sumPlayOperation; @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. // set the playing url NSString * playString = @ "http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"; NSURL * url = [NSURL URLWithString: playString]; // 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: self. player]; // sets the playback page size layer. frame = CGRectMake (0, 0, [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 self. view [self. view. layer addSublayer: layer]; // sets the default value of the playback progress self. progressSlider. value = 0; // sets the default playback volume value self. player. volume = 1.0f;} # pragma mark-start playing-(IBAction) startPlayer :( id) sender {[self. player play] ;}# pragma mark-(IBAction) stopPlayer :( id) sender {[self. player pause] ;}# pragma mark changes the playback progress-(IBAction) changePlaye :( id) sender {self. sumPlayOperation = self. player. currentItem. duration. value/self. player. currentItem. duration. timescale; // The first parameter is: current time // The second parameter is: there are fewer frames per second [self. player seekToTime: CMTimeMakeWithSeconds (self. progressSlider. value * self. sumPlayOperation, self. player. currentTime. timescale) completionHandler: ^ (BOOL finished) {[self. player play];}

3. video download

  • Multithreading is required for video download.
  • Idea: Inherit NSOperation to implement the download task class
  • Note: Enabling runLoop ensures that the download task ends.
  • The NSURLSessionConfiguration provided by iOS8 can be used for background download.
  • AFN does not provide multi-thread security during download.
  • Code example: github

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.