Easy use of IOS video playback

Source: Internet
Author: User

Recently work useful to video playback content, share some simple usage to everyone (because of the speed problem, this example uses the local resources to play, to play the network to modify some URLs can be)

Video playback before 1.IOS9

You need to import MediaPlayer the framework first. There are two ways to play video before iOS9.

    1. One is with the view MPMoviePlayerViewController ;
// 1. Controller with view nsurl *url = [[NSBundle mainbundle] Urlforresource:@ "alizee_la_isla_bonita.mp4  "//   create video player Mpmovieplayerviewcontroller *MPVC =//    show [self PRESENTMOVIEPLAYERVIEWCONTROLLERANIMATED:MPVC];

2. One is not with the viewMPMoviePlayerController

 //1. Without view (here without view is the controller's name without a view, just make a distinction, but it needs to set its own view)Nsurl *url = [[NSBundle mainbundle] Urlforresource:@"Alizee_la_isla_bonita.mp4"Withextension:nil]; MPMoviePlayerController*MPC =[[MPMoviePlayerController alloc] initwithcontenturl:url];//the size is arbitrarily set here, depends on the work needsMpc.view.frame = CGRectMake ( +, -, $, $); //set the style of the Control toolbarMpc.controlstyle =mpmoviecontrolstyleembedded;//Add the playback view to the controller's view[Self.view AddSubview:mpc.view];//Start Playback[MPC play];//Strong ReferencesSELF.MPC = MPC;

3. Automatically play the next video by listening to the notification that is played out

//   Registration Notice object: nil];
  ///call this method when playback is finished- (void) Movieplayerplaybackdidfinishnotification: (Nsnotification *) Notification {NSLog (@"%@", Notification.userinfo); Mpmoviefinishreason reason=[Notification.userinfo[mpmovieplayerplaybackdidfinishreasonuserinfokey] intvalue]; //if it's a normal end, play the next song.      if(Reason = =mpmoviefinishreasonplaybackended) {Nsurl*url = [[NSBundle mainbundle] Urlforresource:@"cupid_ HD. mp4"Withextension:nil]; Self.mpc.contentURL=URL;      [SELF.MPC play]; }  }

Video playback after 2.ios9

IOS9 new AVKit frame, new class AVPlayerViewController for video playback. Note: You must import two frames AVKit and AVFoundation because you AVPlayerViewController do not have the ability to play video, you must give him one AVPlayer .

Code implementation:

    //Play Video//Note: 1. You must give him a play avplayer, and Avplayer in avfoundation, so you need to import the avfondation frame//2. Pip can be used in IPadAir2 and Ipadpro.-(Ibaction) Play: (ID) Sender {//Create a video playerAvplayerviewcontroller *PLAYERVC =[[Avplayerviewcontroller alloc] init]; //need to create a playback object to assign to this controllerNsurl *url = [[NSBundle mainbundle] Urlforresource:@"Alizee_la_isla_bonita.mp4"Withextension:nil]; //Set the playerPlayervc.player =[Avplayer Playerwithurl:url]; [Self presentviewcontroller:playervc animated:yes completion:^{     //Start Playback[Playervc.player play];     }]; //Set up proxyPLAYERVC.Delegate=Self ; }

Add: Some of the commonly used proxy methods in PIP

- (void) Playerviewcontrollerwillstartpictureinpicture: (Avplayerviewcontroller *) playerviewcontroller{NSLog (@"about to begin a picture-in-the -picture"); } - (void) Playerviewcontrollerdidstartpictureinpicture: (Avplayerviewcontroller *) playerviewcontroller{NSLog (@"have begun to draw in the picture"); } - (void) Playerviewcontrollerwillstoppictureinpicture: (Avplayerviewcontroller *) playerviewcontroller{NSLog (@"I'm about to stop PIP."); } - (void) Playerviewcontrollerdidstoppictureinpicture: (Avplayerviewcontroller *) Playerviewcontroller {NSLog (@"the picture has stopped."); } - (void) Playerviewcontroller: (Avplayerviewcontroller *) Playerviewcontroller Failedtostartpictureinpicturewitherror: ( Nserror *) Error {NSLog (@"failed to open picture in Pip:%@", error); } ///When the player is modal out, when painting in the picture, whether to close the pop-up playback controller; default Yes-(BOOL) Playerviewcontrollershouldautomaticallydismissatpictureinpicturestart: (Avplayerviewcontroller *) playerviewcontroller{NSLog (@"%s", __function__); returnNO;}

Easy use of IOS video playback

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.