iOS Video Playback Code demo

Source: Internet
Author: User

Method One:-(void) viewdidload{[Super Viewdidload];      Do any additional setup after loading the view.    With the MPMoviePlayerController to implement video playback, the first thing to do is to import the Mediaplayer.framework framework package into your project. Import #import "Mediaplayer/mpmovieplayercontroller.h" uiimageview *imageview = [[Uiimageview alloc] in the attempt controller    Initwithframe:cgrectmake (0, 0, 320, 568)];    Imageview.image = [UIImage imagenamed:@ "00013"];    [Self.view Addsubview:imageview];        [ImageView release];    Play Video button UIButton *playbutton = [UIButton Buttonwithtype:uibuttontypesystem];    Playbutton.frame = CGRectMake (200, 30, 100, 30);    [PlayButton addtarget:self Action: @selector (Playclick:) forcontrolevents:uicontroleventtouchupinside];        [PlayButton settitle:@ "play video" Forstate:uicontrolstatenormal];    Playbutton.backgroundcolor = [Uicolor Greencolor];    PlayButton.layer.cornerRadius = 5;    PlayButton.layer.masksToBounds = YES; [Self.view Addsubview:playbutton];} -(void) Playclick: (UIButton *) btn{//Video file path, this video has been saved in the project package. belongs to local play   NSString *path = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "mp4"];    Video URL Nsurl *url = [Nsurl Fileurlwithpath:path];    Video Playback object MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initwithcontenturl:url];    Movie.controlstyle = Mpmoviecontrolstylefullscreen;    [Movie.view SetFrame:self.view.bounds];    Movie.initialplaybacktime =-1;    [Self.view AddSubview:movie.view]; Register a notification of the end of the play, when the end of the play, the supervisor hears and do some processing//player with the function of the play notification, only need to register the observer to monitor the notification can be [[Nsnotificationcenter Defaultcenter] Addobse Rver:self selector: @selector (mymoviefinishedcallback:) Name:mpmovieplayerplaybackdidfinishnotification object:    Movie]; [Movie Play];} -(void) Mymoviefinishedcallback: (nsnotification *) notify{//Video Playback object MPMoviePlayerController *themovie = [notify Objec    T]; Destroy playback notifications [[Nsnotificationcenter Defaultcenter] removeobserver:self name:mpmovieplayerplaybackdidfinishnotification    Object: (Themovie)]; Release the video object [Themovie.view release];} Method Two:-(voidviewdidload{//First import the Mediaplayer.framework framework package into the project.    Import #import <MediaPlayer/MediaPlayer.h> [Super Viewdidload] in the attempt controller;    Do any additional setup after loading the view.    Nsurl *videourl;    NSString *path = [[NSBundle mainbundle] pathforresource:@ "test" oftype:@ "mp4"];     Infer whether the network address or local play address if ([path hasprefix:@ "/http:/"]) {videourl = [nsurl Urlwithstring:path];    }else{Videourl = [Nsurl Fileurlwithpath:path]; } Mpmovieplayerviewcontroller *_movieplayercontroller= [[Mpmovieplayerviewcontroller alloc] InitWithContentURL:    Videourl];    [_movieplayercontroller.view Setframe:cgrectmake (0,100,320,200)];    _movieplayercontroller.movieplayer.moviesourcetype=mpmoviesourcetypefile;    [_movieplayercontroller.movieplayer Setscalingmode:mpmoviescalingmodenone];    [_movieplayercontroller.movieplayer Setrepeatmode:mpmovierepeatmodenone];    [_movieplayercontroller.movieplayer setcontrolstyle:mpmoviecontrolmodevolumeonly]; [_MOVIEPLAYERCONTROLLER.MOvieplayer Setfullscreen:no Animated:yes];    [_movieplayercontroller.movieplayer play];    Video playback components of the container, add this container is to be compatible with the iOS6, assuming no container under iOS7 no matter what the problem, assuming that the iOS6 the following video playback screen will be covered by their own active self.view;    UIView *movieplayview = [[UIView alloc]initwithframe:cgrectmake (0, 0, 320, 568)];    [Self.view Addsubview:movieplayview]; [Movieplayview addsubview:[_movieplayercontroller.movieplayer view];}


iOS Video Playback Code demo

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.