AVPlayerViewController

AVPlayerViewController

我在这个问题中描述了相同的问题(link),但就我而言,我使用的是AVPlayerViewController而不是MPMoviePlayerController,因此我尝试应用相同的答案逻辑,但是AVPlayer仅具有未知状态,readyToPlay和失败状态,而我无法知道控制器何时关闭。如何知道AVPlayerViewController何时关闭?如何解决定向导致的相同崩溃?

我的ViewController定位方法:

override var shouldAutorotate : Bool {
    return false
}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
}

这是打开AVPlayerViewController的方法:
func playVideo(at videoUrl: URL) {
    let controller = AVPlayerViewController()
    controller.player = AVPlayer(playerItem: AVPlayerItem(url: videoUrl))
    self.present(controller, animated: true, completion: {
        controller.player?.play()
    })
}

更新:

崩溃日志:
Fatal Exception: UIApplicationInvalidInterfaceOrientation
0  CoreFoundation                 0x183dc2fe0 __exceptionPreprocess
1  libobjc.A.dylib                0x182824538 objc_exception_throw
2  CoreFoundation                 0x183dc2f28 -[NSException initWithCoder:]
3  UIKit                          0x189fe7f78 -[UIViewController _preferredInterfaceOrientationForPresentationInWindow:fromInterfaceOrientation:]
4  UIKit                          0x18a8c5b68 -[_UIFullscreenPresentationController _adjustOrientationIfNecessaryInWindow:forViewController:preservingViewController:]
5  UIKit                          0x18a2421a4 -[UIPresentationController _dismissWithAnimationController:interactionController:target:didEndSelector:]
6  UIKit                          0x18a2689a4 -[UIViewController _dismissViewControllerWithAnimationController:interactionController:completion:]
7  UIKit                          0x18a01dc04 -[UIViewController _dismissViewControllerWithTransition:from:completion:]
8  UIKit                          0x189fc5a98 -[UIViewController dismissViewControllerWithTransition:completion:]
9  UIKit                          0x18a267c4c -[UIViewController _performCoordinatedPresentOrDismiss:animated:]
10 UIKit                          0x189fc55ec -[UIViewController dismissViewControllerAnimated:completion:]
11 AVKit                          0x191667f28 -[AVPlayerViewController(AVPlaybackControlsViewControllerActions) doneButtonTapped:]
12 AVKit                          0x19168d614 -[AVPlaybackControlsViewController doneButtonTapped:]
13 UIKit                          0x189f29010 -[UIApplication sendAction:to:from:forEvent:]
14 UIKit                          0x189f28f90 -[UIControl sendAction:to:forEvent:]
15 UIKit                          0x189f13504 -[UIControl _sendActionsForEvents:withEvent:]
16 UIKit                          0x189f28874 -[UIControl touchesEnded:withEvent:]
17 UIKit                          0x189f28390 -[UIWindow _sendTouchesForEvent:]
18 UIKit                          0x189f23728 -[UIWindow sendEvent:]
19 UIKit                          0x189ef433c -[UIApplication sendEvent:]
20 UIKit                          0x18a6ee014 __dispatchPreprocessedEventFromEventQueue
21 UIKit                          0x18a6e8770 __handleEventQueue
22 UIKit                          0x18a6e8b9c __handleHIDEventFetcherDrain
23 CoreFoundation                 0x183d7142c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__
24 CoreFoundation                 0x183d70d9c __CFRunLoopDoSources0
25 CoreFoundation                 0x183d6e9a8 __CFRunLoopRun
26 CoreFoundation                 0x183c9eda4 CFRunLoopRunSpecific
27 GraphicsServices               0x185708074 GSEventRunModal
28 UIKit                          0x189f59058 UIApplicationMain
29 selftv                         0x10006c124 main (AppDelegate.swift:15)
30 libdyld.dylib                  0x182cad59c start

最佳答案

最好的解决方案是创建新的UIWindow并将AVPlayerViewController设置为rootViewController并为新窗口配置方向。
当应用程序仅用于纵向时,我将此解决方案用于横向和纵向方向的图库。
例:

UIWindow *modalWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
modalWindow.tag = kWindowTag;
modalWindow.rootViewController = <your player vc>;
[modalWindow makeKeyAndVisible];

但是您需要在关闭AVPlayerViewController后设置modalWindow.hidden = yes并为上一个窗口调用makeKeyAndVisible

我使用#import <objc/runtime.h>覆盖了presentViewController:animated:completion:dismissViewControllerAnimated:completion:,在我的方法中,我实现了显示/关闭新窗口的逻辑。

如果您需要更多示例或说明,请告诉我。
谢谢

关于ios - 回到以前的UIViewController时,iOS App崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44923825/

10-13 04:07