如何从我们的视图中删除AVPlayerViewController?

时间:2017-01-31 05:29:41

标签: ios objective-c

这是我的代码,我如何从我们的视图中删除AVPlayerViewController

[self.avPlayerViewController.player pause];

self.avPlayerViewController.view.hidden  = YES; 

我使用上面的代码来隐藏,但它有时没有工作....

-(void)playTrailer {
[self.avPlayerViewController.player pause];
NSString *videoFilePath = [[NSBundle mainBundle]pathForResource:self.trailerString ofType:@"mp4" ];
self.avPlayer = [[AVPlayer alloc]initWithURL:[NSURL fileURLWithPath:videoFilePath]];
self.avPlayerViewController = [[AVPlayerViewController alloc]init];
self.avPlayerViewController.view.frame = CGRectMake(50,50,200,300);
self.avPlayerViewController.player = self.avPlayer;
[self.view addSubview:self.avPlayerViewController.view];
[self.avPlayerViewController.player play];
}


- (IBAction)playTrailerButton:(UIButton *)sender {
self.trailerString = @"myTrailerName";
[self playTrailer];
}

1 个答案:

答案 0 :(得分:2)

  

您的代码中的原因是您将self.avPlayerViewController.view添加到主视图self.view

如果您要删除,请使用

 [self.avPlayerViewController.view removeFromSuperview];

选择2

如果你想取消隐藏播放器,请使用

-(void)playTrailer {
[self.avPlayerViewController.player pause];
self.avPlayerViewController.view.hidden = NO;

 }

想在主视图中隐藏播放器然后使用

 self.avPlayerViewController.view.hidden = YES;

更新回答

-(IBAction)segmentControl:(UISegmentedControl *)sender 
{ 
 [self.avPlayerViewController.view removeFromSuperview]; 
 if (self.segmentControl.selectedSegmentIndex == 0) 
 { 
 [self.avPlayerViewController.player pause]; 
 // [self.avPlayerViewController.view setHidden:YES]; 

 [self playTrailer]; 
 self.animationScrollView.hidden = NO; 
 } 
 else if (self.segmentControl.selectedSegmentIndex == 1) 
 { 
 self.animationScrollView.hidden = YES; 
} 
}