1.首先导入MediaPlayer.framework
2.在你要写的类文件里导入#import <MediaPlayer/MediaPlayer.h>
3.声明全局变量和初始化控制器,并设置视频宽,高,Y值的宏
#define VideoY 44 //视频Y值 #define VideoWidth 320 //视频宽度 #define VideoHeight 200 //视频高度 @interface ViewController () { MPMoviePlayerViewController *playerViewController; MPMoviePlayerController *player; UIButton *_playBtn; //播放按钮 UIImageView *_image; //播放按钮图片 UIImageView *_thumbImgView; //抓取视频的图片 }
4.设置播放按钮和播放按钮的图片
我在这里把播放按钮与视频设置的一样大,是为了更好的用户交互体验,方便用户点击播放,暂停。
//设置播放按钮 _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]; //设置播放按钮的图片 _image = [[UIImageView alloc]init]; _image.frame = CGRectMake(140,120, 50, 50); [_image setImage:[UIImage imageNamed:@"2.png"]]; [self.view addSubview:_image];
4.1 如果只有上面的方法,我们的控制界面是什么都没有,只有一张播放按钮图片,会让用户显得很不舒服,如下图所示。
为了避免这种情况,我们要获取视频的第一帧图片作为背景图片,方法如下。
//重新初始化MPMoviePlayerController,否则获取视频的第一帧图片,会引起无法播放的bug。 MPMoviePlayerController *pc = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:@"http://sm.domob.cn/ugc/151397.mp4"]]; //获取视频的第一帧图片 UIImage *videoThumbImg = [pc thumbnailImageAtTime:0 timeOption:MPMovieTimeOptionNearestKeyFrame]; _thumbImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0f, VideoY, VideoWidth, VideoHeight)]; [_thumbImgView setImage:videoThumbImg]; [self.view addSubview:_thumbImgView];
5.播放视频的方法
//播放视频 - (void) playVideo { //根据视频播放状态,点击视频,出现播放按钮图片或者隐藏 if (player && player.playbackState == MPMoviePlaybackStatePlaying ) { [player pause]; _image.hidden = NO; return; }else if (player && player.playbackState == MPMoviePlaybackStatePaused) { _image.hidden = YES; [player play]; return; } //界面刚显示播放按钮应显示,所以调用时播放图片应为隐藏 _image.hidden = YES; //播放视频 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(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:player]; [self.view insertSubview:player.view belowSubview:_playBtn]; [player play]; }
6.播放视频结束方法的回调
//播放视频结束的回调 -(void)myMovieFinishedCallback:(NSNotification*)notify { //视频播放对象 MPMoviePlayerController* theMovie = [notify object]; //销毁播放通知 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie]; [theMovie stop]; [theMovie.view removeFromSuperview]; //如果视频播放停止了,显示播放按钮图片 if (player && player.playbackState == MPMoviePlaybackStateStopped){ _image.hidden = NO; [player stop]; return; } }
7.禁止视频转向,只允许竖屏
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation { return toInterfaceOrientation == UIInterfaceOrientationPortrait; } - (BOOL)shouldAutorotate { return NO; }
8.完成,结果如下图所示。有任何问题随时欢迎指教。
9.本文代码Demo地址下载:
http://www.oschina.net/code/snippet_1410353_35947