iOS elearning------9 Play Network Video

Source: Internet
Author: User

iOS offers two classes called MPMoviePlayerController Mpmovieplayerviewcontroller that can be easily used for video playback. Mpmovieplayerviewcontroller can only play video in full screen.

#import "MainViewController.h" #import <MediaPlayer/MediaPlayer.h> @interface Mainviewcontroller ()// Video player @property (strong, nonatomic) MPMoviePlayerController *player; @property (Strong, nonatomic) Uiimageview *    ImageView; @end @implementation mainviewcontroller-(void) viewdidload{[Super Viewdidload];        Instantiate video player Nsurl *url = [[NSBundle mainbundle]urlforresource:@ "Promo_full" withextension:@ "mp4"]; Video playback is a streaming media playback mode, so-called streaming media is the video data like water, load, change play. Tip: If the URL contains Chinese, you need to add a percent semicolon. NSString *urlstring = @ "Http:www.xxx.com/video/xxx.mp4";//Nsurl *url = [Nsurl urlwithstring:[urlstring StringByA        Ddingpercentescapesusingencoding:nsutf8stringencoding]];    Self.player = [[MPMoviePlayerController Alloc]initwithcontenturl:url]; 1 set the size of the player [Self.player.view setframe:cgrectmake (0, 0, 320, 180)];        16:9 is the style of the mainstream media//2 add the player view to the root view [Self.view AddSubview:self.player.view];    4 Play [Self.player play];    [Self.player stop]; Monitor video playback status//1 monitor playback via notification hubs in viewer modeState [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (statechange) Name:    Mpmovieplayerplaybackstatedidchangenotification Object:nil]; 2 listening playback complete [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (finishedplay) Name:    Mpmovieplayerplaybackdidfinishnotification Object:nil]; 3 video [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (caputerimage:) Name:    Mpmovieplayerthumbnailimagerequestdidfinishnotification Object:nil]; 3 video [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (caputerimage:) Name:        Mpmovieplayerthumbnailimagerequestdidfinishnotification Object:nil]; 4 Exit full-screen notification [[Nsnotificationcenter defaultcenter]addobserver:self selector: @selector (exitfullscreen) Name:        Mpmovieplayerdidexitfullscreennotification Object:nil];    Asynchronous video, you can specify one or more times in attimes.        [Self.player requestthumbnailimagesattimes:@[@10.0f, @20.0f] timeoption:mpmovietimeoptionnearestkeyframe]; UIImageView *thumbnailimageview = [[Uiimageview alloc]initwithframe:cgrectmake (80, 200, 160, 90)];    Self.imageview = Thumbnailimageview; [Self.view Addsubview:thumbnailimageview];} #pragma mark exits full screen-(void) exitfullscreen{NSLog (@ "exit full screen");} #pragma mark-player event listener #pragma mark Video This method is asynchronous method-(void) Caputerimage: (nsnotification *) notification{NSLog (@ "%@", Notifi    cation);    UIImage *image = notification.userinfo[@ "Mpmovieplayerthumbnailimagekey"]; [Self.imageview setimage:image];} #pragma mark player event listener #pragma mark play complete-(void) finishedplay{NSLog (@ "play complete");} #pragma mark player video monitor #pragma mark playback status Change/* mpmovieplaybackstatestopped,//Stop mpmovieplaybackstateplaying,//Play Mpmoviep laybackstatepaused,//Pause mpmovieplaybackstateinterrupted,//Interrupt Mpmovieplaybackstateseekingforward,//Fast Forward Mpmovieplaybackstateseekingbackward//Rewind */-(void) statechange{switch (self.player.playbackState) {case Mpmov            Ieplaybackstatepaused:nslog (@ "pause");        Break Case MpmovieplayBackstateplaying://Set full screen playback [Self.player Setfullscreen:yes animated:yes];            NSLog (@ "Play");        Break            Case mpmovieplaybackstatestopped://Note: The Mpmovieplaybackstatestopped event is not triggered when normal playback is complete.            Call [Self.player stop]; method can trigger this event.            NSLog (@ "Stop");        Break    Default:break; }} @end


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.