Ios video demo

Source: Internet
Author: User

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: @]]; 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


Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.