我正在使用AVPLayerViewController来传输内容。播放工作正常但我没有看到“完成”按钮来关闭视图。
以下是我如何设置和显示视图控制器
NSURL* url = [content localSessionUrl];
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc] init];
playerViewController.player = [AVPlayer playerWithURL:url];
[self.navigationController pushViewController:playerViewController animated:YES];
[self.navigationController setNavigationBarHidden:YES];
是因为我在导航控制器上推它吗?
答案 0 :(得分:22)
尝试呈现视图,而不是推送或添加子视图。完成按钮将自动显示。以下是代码段。
-(void)playVideoWithUrl:(NSURL *)url{
AVPlayerViewController *playerViewController = [[AVPlayerViewController alloc]init];
playerViewController.player = [[AVPlayer alloc]initWithURL:url];
[self presentViewController:playerViewController animated:YES completion:nil];
playerViewController.view.frame = self.view.frame;
[playerViewController.player play];
}
答案 1 :(得分:7)
是的,'完成'按钮可以解雇模态演示风格。如果您将AVPlayerViewController推送到导航堆栈,则需要自己处理弹出窗口。
答案 2 :(得分:2)
我认为问题是当您将AVPlayerViewController视图添加到占据整个屏幕的屏幕上的视图时,它将其视为已经处于全屏模式。诀窍是将其添加到占据屏幕较小部分的视图中。
最初我这样做了:
// Setup an AVPlayerViewController
self.avPlayerViewController = [[AVPlayerViewController alloc] init];
if ([self.avPlayerViewController respondsToSelector:@selector(allowsPictureInPicturePlayback)]) {
self.avPlayerViewController.allowsPictureInPicturePlayback = NO;
}
// Attach the Active Cloack AVPlayer to the AVPlayerViewController
AVPlayer *player = self.avPlayer;
self.avPlayerViewController.player = player;
self.avPlayerViewController.view.frame = self.view.bounds;
[self.view addSubview:self.avPlayerViewController.view];
// Setup the Parent Child relationshipo
[self addChildViewController:self.avPlayerViewController];
[self.avPlayerViewController willMoveToParentViewController:self];
[self.avPlayerViewController didMoveToParentViewController:self];
然而,我遇到了同样的问题,即我的播放器没有完成按钮,因此我无法退出视频。
然后我修改了我的xib / storyboard并使用完成按钮将UINavigationBar添加到顶部,并在其下面添加了UIView作为容器。然后我将avPlayerViewController添加到此容器视图中,控件神奇地变为无全屏模式。我有一个全屏按钮让玩家进入全屏模式。
然而,我必须创建代码以自动隐藏我的导航栏并为此添加完成按钮和标题等...
答案 3 :(得分:0)
对于iPad,我使用UIModalPresentationFormSheet来显示和完成按钮w丢失...所以我使用了自己的按钮:
AVPlayerViewController *playerViewController =
[[AVPlayerViewController alloc] init];
//...
UIViewController *controllerToPresent = playerViewController;
if (IS_IPAD){
SBNavigationController *nc = [[SBNavigationController alloc] initWithRootViewController:playerViewController];
[playerViewController.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"") style:UIBarButtonItemStylePlain target:self action:@selector(cancelPlayingPressed)]];
controllerToPresent = nc;
}
答案 4 :(得分:0)