Ios video demo
Today, I want to use the original ios ecosystem to play a piece of online video, and sort and share it here.
First, you must add the Framework MediaPlayer. Framework to the project.
# Import ViewController. h # import
@ Interface ViewController () {MPMoviePlayerViewController * playerViewController; MPMoviePlayerController * player; UIButton * _ playBtn; // playback button UIImageView * _ image; // playback button image UIImageView * _ thumbImgView; // capture the video image} @ end @ implementation ViewController-(void) viewDidLoad {[super viewDidLoad]; NSInteger VideoWidth = 320; NSInteger VideoHeight = 200; NSInteger VideoY = 44; // re-Initialize MPMoviePlayerController; otherwise, obtain The first frame of the image will cause a bug that cannot be played. MPMoviePlayerController * pc = [[MPMoviePlayerController alloc] initWithContentURL: [NSURL URLWithString: @ brief // obtain the first frame of the video. UIImage * videoThumbImg = [pc protocol: 0 timeOption: callback]; _ thumbImgView = [[UIImageView alloc] initWithFrame: CGRectMake (0.0f, VideoY, VideoWidth, VideoHeight)]; [_ thumbImgView setImage: videoThumbImg]; [self. view addSubview: _ thumbImgView]; // sets the play button _ playBtn = [UIButton buttonWithType: UIButtonTypeCustom]; _ playBtn. backgroundColor = [UIColor clearColor]; [_ playBtn setFrame: CGRectMake (0.0f, VideoY, VideoWidth, VideoHeight)]; [_ playBtn setTitleColor: [UIColor greenColor] forState: Unknown]; [_ playBtn addTarget: self action: @ selector (playVideo) forControlEvents: UIControlEventTouchUpInside]; [self. view addSubview: _ playBtn]; // sets the picture _ 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 the video-(void) playVideo {// Based on the video playback status, click the video, play Button image or hide if (player & player. playbackState = MPMoviePlaybackStatePlaying) {[player pause]; _ image. hidden = NO; return;} else if (player & player. playbackState = MPMoviePlaybackStatePaused) {_ image. hidden = YES; [player play]; return;} // The playback button displayed on the UI should be displayed. Therefore, the playback image should be hidden _ image during the call. 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; [[nsicationcenter center defacenter center] addObserver: self selector: @ selector (myMovieFinishedCallback :) name: MPMoviePlayerPlaybackDidFinishNotification object: player. view insertSubview: player. view belowSubview: _ playBtn]; [player play];} // callback of the end of the video playing-(void) myMovieFinishedCallback :( NSNotification *) Y {// video playback object MPMoviePlayerController * theMovie = [y object]; // destroy the playback notification [[nsicationicationcenter defaultCenter] removeObserver: self name: MPMoviePlayerPlaybackDidFinishNotification object: theMovie]; [theMovie stop]; [theMovie. view removeFromSuperview]; // if the video is stopped, the play button image if (player & player. playbackState = MPMoviePlaybackStateStopped) {_ image. hidden = NO; [player stop]; return ;}}-(BOOL) returns :( UIInterfaceOrientation) toInterfaceOrientation {return toInterfaceOrientation = Break ;}- (BOOL) shouldAutorotate {return YES ;} @ end