利用自带MPMoviePlayerController来实现视频播放,首先要在项目中导入MediaPlayer.Framework框架包。
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationController.navigationBar.hidden=YES;
//geomancy.jpg
UIImageView * nanshanImage=[[UIImageView alloc]initWithFrame:CGRectMake(0,0,1024,699)];
nanshanImage.image=[UIImage imageNamed:@"geomancy.jpg"];
[self.view addSubview:nanshanImage];
[nanshanImage release];
//播放视频按钮
UIButton* playButton= [[UIButton alloc]initWithFrame:CGRectMake(145, 250, 70, 80)];
[playButton addTarget:self action:@selector(PlayMovieAction:) forControlEvents:UIControlEventTouchUpInside];
playButton.backgroundColor=[UIColor redColor];
[self.view addSubview:playButton];
[playButton release];
}
-(void)PlayMovieAction:(id)sender{
// NSLog(@"PlayMovieAction====");
//视频文件路径,此视频已经存入项目包中。属于本地播放
NSString *path = [[NSBundle mainBundle] pathForResource:@"jinxiuMovie" ofType:@"mp4"];
//视频URL
NSURL *url = [NSURL fileURLWithPath:path];
//视频播放对象
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
movie.controlStyle = MPMovieControlStyleFullscreen;
[movie.view setFrame:self.view.bounds];
movie.initialPlaybackTime = -1;
[self.view addSubview:movie.view];
// 注册一个播放结束的通知,当播放结束时,监听到并且做一些处理
//播放器自带有播放结束的通知,在此仅仅只需要注册观察者监听通知即可。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:movie];
[movie play];
}
-(void)myMovieFinishedCallback:(NSNotification*)notify
{
//视频播放对象
MPMoviePlayerController* theMovie = [notify object];
//销毁播放通知
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
[theMovie.view removeFromSuperview];
// 释放视频对象,此对象由上面建立视频对象时候所alloc,在此做释放操作
[theMovie release];
// NSLog(@"视频播放完成");
}
本文转载至:http://blog.sina.com.cn/s/blog_945590aa0101bytk.html