Skip to content

Instantly share code, notes, and snippets.

@amandeeprehal-lab49
Last active May 14, 2024 12:45
Show Gist options
  • Save amandeeprehal-lab49/70af0baa50f78e07ab88aefc7646c516 to your computer and use it in GitHub Desktop.
Save amandeeprehal-lab49/70af0baa50f78e07ab88aefc7646c516 to your computer and use it in GitHub Desktop.
TWL-Lab49/AVPlayer auto rotate and programmatically enter full screen when device switched to landscape orientation with SwiftUI

iOS 14 version plus, AVPlayer that supports auto rotate and programmatically enter full screen when device switched to landscape orientation with SwiftUI

import SwiftUI
import AVKit

@available(iOS 14.0, *)
struct AutoRotate: View {
    @State private var showFullScreen = false
    let url: URL
    private let player: AVPlayer
    
    init(url: URL) {
        self.url = url
        self.player = AVPlayer(url: url)
    }
    
    var body: some View {
        playerView
            .ignoresSafeArea()
            .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
                DispatchQueue.main.async {
                    if UIDevice.current.orientation.isLandscape {
                        self.showFullScreen = true
                    } else {
                        self.showFullScreen = false
                    }
                }
            }
    }
    
    private var playerView: some View {
        showFullScreen ? AVPlayerControllerRepresentable(showFullScreen: true, player: player) : AVPlayerControllerRepresentable(showFullScreen: false, player: player)
    }
}

@available(iOS 14.0, *)
struct AutoRotate_Previews: PreviewProvider {
    static var previews: some View {
        AutoRotate(url: URL(fileURLWithPath: "https://wolverine.raywenderlich.com/content/ios/tutorials/video_streaming/foxVillage.mp4"))
    }
}


struct AVPlayerControllerRepresentable: UIViewControllerRepresentable {
    let showFullScreen: Bool
    let player: AVPlayer
    
    func makeUIViewController(context: Context) -> AVPlayerViewController {
        let controller  = AVPlayerViewControllerRotatable()
        controller.player = player
        chooseScreenType(controller)
        return controller
    }
    
    func updateUIViewController(_ controller: AVPlayerViewController, context content: Context) {
        chooseScreenType(controller)
    }
    
    private func chooseScreenType(_ controller: AVPlayerViewController) {
        showFullScreen ? controller.enterFullScreen(animated: true) : controller.exitFullScreen(animated: true)
        
    }
    
    
}

class AVPlayerViewControllerRotatable: AVPlayerViewController {
    
    override var shouldAutorotate: Bool {
        return true
    }
    
}

extension AVPlayerViewController {
    func enterFullScreen(animated: Bool) {
        perform(NSSelectorFromString("enterFullScreenAnimated:completionHandler:"), with: animated, with: nil)
    }
    
    func exitFullScreen(animated: Bool) {
        perform(NSSelectorFromString("exitFullScreenAnimated:completionHandler:"), with: animated, with: nil)
    }
}


@rd-wits
Copy link

rd-wits commented May 14, 2024

crashes for me on uiViewController.enterFullScreen(animated: true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment