2017-07-05 109 views
1

iOS應用崩潰我已經在這個問題(link),但描述了同樣的問題在我的情況我使用AVPlayerViewController代替MPMoviePlayerController,所以我試圖以應用答案,但AVPlayer的相同的邏輯只有未知,readyToPlay和失敗狀態,我無法知道控制器什麼時候關閉。如何知道何時AVPlayerViewController已關閉,或者如何解決導致同一碰撞的問題?當談到返回到之前的UIViewController

我的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() 
    }) 
} 

UPDATE:

崩潰日誌:

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 
+0

我覺得你還沒有采取從currentviewcontroller disappering採取玩家。我想當你回到以前的viewcontroller停止播放器。 –

+0

@AshokLondhe崩潰說:致命的例外:UIApplicationInvalidInterfaceOrientation作爲我連接的問題 – Roran

+0

你沒有mentinoded問題,請檢查你的問題也在鏈接我似乎沒有。 –

回答

0

最佳解決方案是創建新的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:,在我的方法中,我實現了顯示/解除新窗口的邏輯。

如果您需要更多示例或解釋,請告訴我。 謝謝

+1

夫特3: '讓modalWindow =一個UIWindow(幀:UIScreen.main.bounds) modalWindow.tag = kWindowTag modalWindow.rootViewController = <播放機VC> modalWindow.makeKeyAndVisible()' –

+0

我嘗試這樣的代碼,但什麼是顯示 – Roran

+1

明天我會給你完整的例子。 –