AVPlayer Fullscreen

Hello everyone, I've a problem with SwiftUI and AVPlayer. When i rotate the device in landscape mode, the player go in fullscreen mode, but when I rotate in portrait it dosn't exit. The struct for AVPlayer:

import SwiftUI
import AVKit

struct AVPlayerControllerRepresentable: UIViewControllerRepresentable {
    @Binding var showFullScreen: Bool
    @Binding var player: AVPlayer
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<AVPlayerControllerRepresentable>) -> AVPlayerViewController {
        print("makeUIViewController->",showFullScreen)
        let controller  = AVPlayerViewController()
        controller.player = player
        controller.showsPlaybackControls = false;
        chooseScreenType(controller)
        return controller
    }
    
    func updateUIViewController(_  uiViewController: AVPlayerViewController , context: UIViewControllerRepresentableContext<AVPlayerControllerRepresentable>) {
        print("updateUIViewController->",showFullScreen)
        chooseScreenType(uiViewController)
    }
    
    private func chooseScreenType(_ controller: AVPlayerViewController) {
        print("chooseScreenType", self.showFullScreen)
        self.showFullScreen ? controller.enterFullScreen(animated: true) : controller.exitFullScreen(animated: true)
    }
    
}

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

And this is my View:

 VStack{
                       

         AVPlayerControllerRepresentable(showFullScreen: $fullScreen, player: $player)
                                    .ignoresSafeArea()
                                    .onReceive(NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)) { _ in
                                        DispatchQueue.main.async {
                                            print("change rotation->",UIDevice.current.orientation.rawValue)
                                            if UIDevice.current.orientation.isLandscape {
                                                print("landscape")
                                                self.fullScreen = true
                                            } else {
                                                print("portrait")
                                                self.fullScreen = false
                                            }
                                        }
                                    }
                                    .frame(width: 290, height: 220)
                                    .overlay {
                                       BoxTv()
                                    }
                                    .opacity(1.0)
                                    .padding([.bottom, .top], 40)
                            }.onAppear(){
                                self.player.play();
    }

Can anyone help me? When rotate device in portrait mode, the function 'exitFullScreen' isn't called

Replies

I also have the same problem and a very similar code.

Keeping an eye on this thread for possible solutions.

Looking for the answer as well... no one on either Stackoverflow or DevFourms seems to care to help