iOS Play Video Demo

Source: Internet
Author: User

Today to use the original iOS play a network video, organized in this sharing out

White Hand Tour column http://blog.csdn.net/u010229677

First you need to add a frame to the project Mediaplayer.framework

#import "ViewController.h" #import <MediaPlayer/MediaPlayer.h> @interface Viewcontroller () {    Mpmovieplayerviewcontroller *playerviewcontroller;        MPMoviePlayerController *player; UIButton *_playbtn;   Play button Uiimageview *_image; Play button picture Uiimageview *_thumbimgview;    Grab the picture of the video} @end @implementation viewcontroller-(void) viewdidload{[Super Viewdidload];    Nsinteger videowidth = 320;    Nsinteger videoheight = 200;    Nsinteger Videoy = 44;    Re-initialize the MPMoviePlayerController, otherwise get the first frame of the video picture, will cause the bug that can't play. MPMoviePlayerController *pc = [[MPMoviePlayerController alloc] initwithcontenturl:[nsurl urlwithstring:@ "/http/        Sm.domob.cn/ugc/151397.mp4 "];    Get the first frame picture of the video UIImage *videothumbimg = [pc thumbnailimageattime:0 timeoption:mpmovietimeoptionnearestkeyframe];    _thumbimgview = [[Uiimageview alloc] Initwithframe:cgrectmake (0.0f, Videoy, Videowidth, Videoheight)];    [_thumbimgview setimage:videothumbimg]; [Self.view AddSubView:_thumbimgview];    Set play button _playbtn = [UIButton buttonwithtype:uibuttontypecustom];    _playbtn.backgroundcolor = [Uicolor Clearcolor];    [_playbtn Setframe:cgrectmake (0.0f, Videoy, Videowidth, Videoheight)];    [_playbtn Settitlecolor:[uicolor Greencolor] forstate:uicontrolstatenormal];    [_playbtn addtarget:self Action: @selector (PlayVideo) forcontrolevents:uicontroleventtouchupinside];        [Self.view ADDSUBVIEW:_PLAYBTN];    Set the picture of the play button _image = [[Uiimageview alloc]init];    _image.frame = CGRectMake (140,120, 50, 50);    [_image setimage:[uiimage imagenamed:@ "Yss_ios_hy_huodong_touxiang.png"];    [Self.view Addsubview:_image]; }//Play video-(void) playvideo{//Depending on the video playback status, click on the video, the play button picture appears or hide if (player && player.playbackstate = = Mpmovieplaybac        kstateplaying) {[player pause];        _image.hidden = NO;    Return        }else if (player && player.playbackstate = = mpmovieplaybackstatepaused) {_image.hidden = YES; [Player play];    Return    }//Interface just shows the play button should be displayed, so the call when playing the picture should be hidden _image.hidden = YES;    Nsinteger videowidth = 320;    Nsinteger videoheight = 200;    Nsinteger Videoy = 44; Play Video player = [[MPMoviePlayerController alloc] Initwithcontenturl:[nsurl urlwithstring:@ "http://sm.domob.cn/ugc/    151397.mp4 "];    Player.view.frame = CGRectMake (0, Videoy, videowidth,videoheight);    Player.controlstyle = Mpmoviecontrolstylenone;    Player.repeatmode = Mpmovierepeatmodenone;    [Player Setfullscreen:yes Animated:yes];    Player.scalingmode = Mpmoviescalingmodeaspectfit; [[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (Mymo                                               Viefinishedcallback:) name:mpmovieplayerplaybackdidfinishnotification    Object:player];    [Self.view InsertSubview:player.view belowsubview:_playbtn]; [Player play];} Playback end of video callback-(void) Mymoviefinishedcallback: (Nsnotification*) notify{//Video Playback object mpmovieplayercontroller* Themovie = [notify Object]; Destroy playback notifications [[Nsnotificationcenter Defaultcenter] removeobserver:self n    Ame:mpmovieplayerplaybackdidfinishnotification Object:themovie];    [Themovie stop];    [Themovie.view Removefromsuperview];  If the video playback is stopped, show the play button picture if (player && player.playbackstate = = mpmovieplaybackstatestopped) {_image.hidden =        NO;        [Player stop];    Return }}-(BOOL) shouldautorotatetointerfaceorientation: (uiinterfaceorientation) tointerfaceorientation{return Tointerfaceorientation = = uiinterfaceorientationportrait;} -(BOOL) shouldautorotate{return YES; @end


iOS Play Video Demo

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.