我在一个应用程序上编码,用户可以观看各种各样的视频。我在按钮点击时全屏打开视频,用户可以使用播放控件来调整窗口大小。我遇到的问题是当用户通过热点共享他的互联网时,应用程序顶部有一个蓝色条。此时调整窗口大小会导致AVKit框架崩溃(我相信)。使用Swift 2.3,Xcode 7.3.1。我的手机,iPhone 6s,使用iOS 10,我也尝试过iOS 9 iPhone 6 Plus,同样的问题。
碰撞:
Terminating app due to uncaught exception 'UIViewControllerHierarchyInconsistency', reason: 'child view controller:<AVFullScreenPlaybackControlsViewController: 0x102933000> should have parent view controller:<AVPlayerViewController: 0x10212d800> but actual parent is:<AVFullScreenViewController: 0x10884d900>'
我的代码:
private let playerController = AVPlayerViewController()
private var player: AVPlayer!
private func playVideo(media: Media) {
player = AVPlayer(URL: NSURL(string: media.url)!)
playerController.player = player
presentViewController(playerController, animated: true, completion: {
self.playerController.player?.play()
self.playerController.delegate = self
})
}
如果我不在我的手机上共享我的互联网(因此没有蓝色条),那么它工作正常,没有任何问题。有人设法隐藏这个蓝色条,或遇到类似的问题?
我通过这段代码修复了它:
@interface YourAVPlayerViewController : AVPlayerViewController
@end
@implementation YourAVPlayerViewController
- (BOOL)prefersStatusBarHidden {
return YES;
}
@end
作为一种解决方法,您可以推送navigationControlelr堆栈或在呈现模态时使用以下内容:
@interface AVPlayerViewController ()
- (void)fullScreenButtonTapped:(id)arg1;
@end
@interface RCKPlayerViewController ()
@end
@implementation RCKPlayerViewController
- (void)fullScreenButtonTapped:(id)arg1 {
if ([[UIApplication sharedApplication] statusBarFrame].size.height >= 40) {
// Show alert that cannot enter full screen when in-call
} else {
[super fullScreenButtonTapped:arg1];
}
}
@end
--
然后只需使用RCKPlayerViewController
AVPlayerViewController *playerViewController = [[RCKPlayerViewController alloc] init];
AVPlayerItem *playerItem = [AVPlayerItem playerItemWithURL:[NSURL URLWithString:videoStringURL]];
AVPlayer *player = [AVPlayer playerWithPlayerItem:playerItem];
playerViewController.player = player;
[self presentViewController:playerViewController animated:YES completion:^{
[player play];
}];
我为AVPlayerViewController
编写了一个小扩展,修复了应用程序范围内的问题:
// AVPlayerViewController.swift
import AVKit
extension AVPlayerViewController {
// fixes app crash while using personal hotspot + watching a full screen video
override open var prefersStatusBarHidden: Bool {
return true
}
}