Advanced iOS Learning-Multimedia

Source: Internet
Author: User
Tags set background

First, the audio

1, IOS has a total of four kinds of special implementation of the way to play audio:

    • System sound Services.
    • OpenAL (cross-platform open source audio processing interface).
    • Audio Queue Services (playback and recording of a voice service).
    • Avaudioplayer (advanced Audio player).
    • here we mainly introduce the system sound services and Avaudioplayer.

2. System Sound Services

  • System Sound Services is the lowest and simplest voice playback service, and you can play some simple audio files by calling the Audioservicesplaysystemsound function.
  • Application scenario: Suitable for playing some very small hints or warning tones, audiotoolbox.framework is a C-based framework that uses it to play the sound it is essentially registering short audio to the system sound service.
  • Limitations:
  • Sound length less than 30s
  • Format: Must be PCM or IMA4
  • Cannot control the progress of playback
  • Play sound immediately after calling method
  • No loop playback and stereo sound playback
  • Example code: (Create a button with storyboard, associate a click event)
#import "ViewController.h"//Introducing Header Files#import<AudioToolbox/AudioToolbox.h>@interfaceViewcontroller ()@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}#pragmaMark-Play Music-(Ibaction) Playaction: (ID) Sender {//get the main application packageCfbundleref Mainbundle;    Systemsoundid Soundfileobject; Mainbundle=Cfbundlegetmainbundle (); //the URL of the sound fileCfurlref soundfileurlref = Cfbundlecopyresourceurl (Mainbundle,cfstr ("Tsai Chin-love breaks the emotional wound"), Cfstr ("wav"), NULL); //register sound to SystemAudioservicescreatesystemsoundid (soundfileurlref,&soundfileobject); //The Audioservicesaddsystemsoundcompletion method adds a CallBack function for audio playback, and with the CallBack function we can solve a number of problems, such as overcoming the System sound Services The problem of looping playback is not supported by itself. Audioservicesaddsystemsoundcompletion (soundfileobject, NULL, NULL, &completioncallback, (__bridgevoid*) self); //play the registered sound, (this code can be called anywhere in this class, not limited to this method)Audioservicesplaysystemsound (Soundfileobject); //make your phone vibrate//Audioservicesplaysystemsound (ksystemsoundid_vibrate);}Static  voidCompletionCallback (Systemsoundid SsID,void*clientdata) {    //play again after sound Play completionNSLog (@"playback Complete-the incoming ID is-%u and the passed-in parameter is%@", (unsignedint) SsID, clientdata); Audioservicesplaysystemsound (SsID);}voidPlayfinished (Systemsoundid SsID,void*clientdata) {//unsigned long ID = SsID;//The SsID cannot be printed directly as a parameter and needs to be brokered onceNSLog (@"playback Complete-the incoming ID is-%u and the passed-in parameter is%@", (unsignedint) SsID, clientdata);    Audioservicesplaysystemsound (SsID); //functions executed after removal is complete//audioservicesremovesystemsoundcompletion (SsID); //Release custom system sounds based on ID//Audioservicesdisposesystemsoundid (SsID);}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}@end

3, Avaudioplayer: is an advanced player, it 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)

4, the advantages of Avaudioplayer:

    • Support for more formats
    • Can play audio files of any length
    • Supports loop playback
    • Multiple audio files can be played synchronously
    • Control playback progress and start playback from any point in the audio

5. Development steps:

  • Avaudioplayer is included in the Avfoundation framework, so when developing the frame Avfoundation.framework The audio is first imported. Note: The introduction of header files #import <AVFoundation/AVFoundation.h>.
  • Avaudioplayer initialization needs to give a playback file: Avaudioplayer *avaudioplayer = [[Avaudioplayer alloc] Initwithcontentsofurl: File path error : nil];
  • Important properties: 1. Set initial volume size Audioplayer.volume = 1; (0.0 ~ 1.0); 2. Set the number of music plays Audioplayer.numberofloops = 1 (as long as negative numbers are looped); 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. The proxy method that is called when playback finishes:-(void) audioplayerdidfinishplaying: (Avaudioplayer *) player successfully :(BOOL) flag; playback decoding failed:-(void) Audioplayerdecodeerrordidoccur: (Avaudioplayer *) Player ERROR: (NSERROR *) error.
  • Sample code: GitHub

Second, video

  1, Avplayer

Video playback in IOS is Avplayer (contained within the avfoundation framework) that is somewhat similar to Avaudioplayer, but Avplayer is more powerful and can be used to play audio as well as to play video. And in the audio aspect of playing Avplayer can play the network audio directly.

2. Development steps

  • 1) Import the frame avfoundation.framework which supports video playback, introduce the header file #import <AVFoundation/AVFoundation.h>.
  • 2) Gets the address of the playback.
  • 3) Create the Avplayeritem object (Avplayeritem can get the video information, the current play time, the total time, etc.) according to the playing URL.
  • Important attributes of Avplayeritem:
  • State Status:
  • Avplayerstatusunknown, (there is an unknown problem on behalf of the video playback)
  • Avplayerstatusreadytoplay, (on behalf of the video can play, can call the play method)
  • Avplayerstatusfailed (on behalf of video not playing)
  • Loadedtimerange: Represents the progress that has been cached, listening to this property can update the cache progress in the UI and is also a useful property.
  • 4) Initialize the Avplayer object according to Avplayeritem.
  • 5) Simply use the Avplayer class to not display the video; This class does not have a real view; you need to add the video layer to the Avplayerlayer, and finally you need to add avplayerlayer to the layer where you want the page to play
  • 6) Avplayerlayer related settings and playback.
  • 7) play at the specified time.
  • 8) play the completed notification.
  • The six steps above are already available for video playback on IOS clients.
  • code example:
#import "ViewController.h"#import<AVFoundation/AVFoundation.h>#import<AVKit/AVKit.h>@interfaceViewcontroller ()///controls for controlling video playback@property (Weak, nonatomic) Iboutlet UISlider *Progressslider;///declares the control property that plays the video (which can also be used to play audio)@property (nonatomic, strong) Avplayer *player;///total duration of playback@property (nonatomic, assign) cgfloat sumplayoperation;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //set the playback URLNSString *playstring =@"Http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"; Nsurl*url =[Nsurl urlwithstring:playstring]; //set the item to playAvplayeritem *item =[[Avplayeritem alloc] initwithurl:url]; //Initialize the Player objectSelf.player =[[Avplayer alloc] initwithplayeritem:item]; //set the playback pageAvplayerlayer *layer =[Avplayerlayer PlayerLayerWithPlayer:self.player]; //set the size of the playback pageLayer.frame = CGRectMake (0,0, [UIScreen Mainscreen].bounds.size.width, -); //Set Background colorLayer.backgroundcolor =[Uicolor Cyancolor].    Cgcolor; //sets the scale display between the playback window and the current viewLayer.videogravity =Avlayervideogravityresizeaspect; //Add playback view to Self.view[Self.view.layer Addsublayer:layer]; //set the default value for playback progressSelf.progressSlider.value =0; //set the default volume value for playbackSelf.player.volume =1.0f; }#pragmaMark-Start Playback-(Ibaction) Startplayer: (IDSender {[Self.player play];}#pragmaMark stops playing-(ibaction) Stopplayer: (IDSender {[self.player pause];}#pragmaMark changes Playback Progress-(Ibaction) Changeplaye: (ID) Sender {self.sumplayoperation= Self.player.currentItem.duration.value/Self.player.currentItem.duration.timescale; //The first parameter is: the 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

    • Video download requires multithreading
    • Idea: Inherit nsoperation, implement download task class
    • Note: Turn on Runloop to ensure the end of the download task
    • Use the nsurlsessionconfiguration provided by iOS8 to enable background downloads
    • AFN does not provide multi-threaded security when downloading
    • code example: GitHub

Advanced iOS Learning-Multimedia

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.