• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

回到之前的 UIViewController 时 iOS App 崩溃

[复制链接]
菜鸟教程小白 发表于 2022-12-11 20:02:58 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我在这个问题 (link) 中描述了同样的问题,但在我的情况下,我使用的是 AVPlayerViewController 而不是 MPMoviePlayerController,所以我尝试应用相同的答案的逻辑,但 AVPlayer 只有 unknow、readyToPlay 和 failed 状态,我不知道 Controller 何时关闭。我怎么知道 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



Best Answer-推荐答案


最佳解决方案是创建新的 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 来覆盖 presentViewController:animated:completion:dismissViewControllerAnimated:completion: 并在我的方法实现了显示/关闭新窗口的逻辑。

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

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

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap