MPMoviePlayerViewController的使用
- (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 {
//此處播放完成做什麼事
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.