- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 100, 100);
[button setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button setTitle:@"播放" forState:UIControlStateNormal];
}
- (void)play:(UIButton*)button
{
[self playMovie]; //点击button调用视频播放
[playMovie.moviePlayer play];
}
//播放视频
- (void)playMovie
{
NSURL *url = [NSURL URLWithString:@"http://data.vod.itc.cn/?new=/102/196/G3DltnLK1nA73WYpi1XXF3.mp4&plat=3&mkey=dl_snx23d-ILpAMKn0ccasFqgx__OjJL"];
playMovie = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
playMovie.moviePlayer.view.frame = self.view.bounds;
playMovie.moviePlayer.fullscreen = YES;
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"moviePause.png"]];
imageView.frame = CGRectMake(270, 410, 30, 30);
[playMovie.moviePlayer.view addSubview:imageView]; //这个属性可以在控制栏添加按钮
playMovie.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
if (playMovie.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || playMovie.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
playMovie.view.frame = CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width); //如果是旋转的话 宽和高大小交换实现屏幕旋转
}else{
playMovie.view.frame = self.view.bounds;
}
[self.view addSubview:playMovie.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
// 视频播放完毕
- (void)moviePlayerPlaybackDidFinish:(NSNotification *)notification {
//此处播放完成做什么事
}
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(100, 100, 100, 100);
[button setImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(play:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
[button setTitle:@"播放" forState:UIControlStateNormal];
}
- (void)play:(UIButton*)button
{
[self playMovie]; //点击button调用视频播放
[playMovie.moviePlayer play];
}
//播放视频
- (void)playMovie
{
NSURL *url = [NSURL URLWithString:@"http://data.vod.itc.cn/?new=/102/196/G3DltnLK1nA73WYpi1XXF3.mp4&plat=3&mkey=dl_snx23d-ILpAMKn0ccasFqgx__OjJL"];
playMovie = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
playMovie.moviePlayer.view.frame = self.view.bounds;
playMovie.moviePlayer.fullscreen = YES;
UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"moviePause.png"]];
imageView.frame = CGRectMake(270, 410, 30, 30);
[playMovie.moviePlayer.view addSubview:imageView]; //这个属性可以在控制栏添加按钮
playMovie.moviePlayer.scalingMode = MPMovieScalingModeAspectFit;
if (playMovie.interfaceOrientation == UIInterfaceOrientationLandscapeLeft || playMovie.interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
playMovie.view.frame = CGRectMake(0, 0, self.view.bounds.size.height, self.view.bounds.size.width); //如果是旋转的话 宽和高大小交换实现屏幕旋转
}else{
playMovie.view.frame = self.view.bounds;
}
[self.view addSubview:playMovie.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerPlaybackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}
// 视频播放完毕
- (void)moviePlayerPlaybackDidFinish:(NSNotification *)notification {
//此处播放完成做什么事
}