2

I have the same issue described in this question (link) but in my case I'm using AVPlayerViewController instead MPMoviePlayerController, so I tried to apply the same logic of the answer but AVPlayer has only unknow, readyToPlay and failed status and I can't know when the Controller is closed.How can I know when the AVPlayerViewController is closed or how can I solved the same crash caused by orientation?

my ViewController orientation methods:

override var shouldAutorotate : Bool {
    return false
}

override var supportedInterfaceOrientations : UIInterfaceOrientationMask {
    return UIInterfaceOrientationMask.portrait
}

this is the method that opens the 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:

Crash log:

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
Roran
  • 433
  • 1
  • 5
  • 24
  • I think you have not stoped player while disappering from currentviewcontroller. i think when you are going back to previous viewcontroller stop the player. – Ashok Londhe Jul 05 '17 at 10:58
  • @AshokLondhe the crash says: Fatal Exception: UIApplicationInvalidInterfaceOrientation as the question that I linked – Roran Jul 05 '17 at 11:27
  • That you didn't mentinoded in Question please check your question also in link i didn't seems it. – Ashok Londhe Jul 05 '17 at 12:28
  • @AshokLondhe if you are careful, you can see the link of the similar problem and at the end of my question I said the crash is caused by orientation – Roran Jul 05 '17 at 13:37
  • you have not shared any crash log so how can you say that crash is because of orientation? – Ashok Londhe Jul 05 '17 at 13:39

1 Answers1

0

Best solution is create new UIWindow and set AVPlayerViewController as rootViewController and configure orientations for new window.
I used this solution for images gallery with landscape and portrait orientations when app is used only portrait.
Example:

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

But you need after closing AVPlayerViewController set modalWindow.hidden = yes and call makeKeyAndVisible for previous window.

I used #import <objc/runtime.h> for overriding presentViewController:animated:completion: and dismissViewControllerAnimated:completion: and in my methods i implemented logic for showing/dismissing new window.

If you will need more examples or explanations, please let me know. Thanks