【问题标题】:AVPlayer exitFullScreenAVPlayer 退出全屏
【发布时间】:2022-09-28 19:48:51
【问题描述】:

我对 SwiftUI 和 AVPlayer 有疑问。当我在横向模式下旋转设备时,播放器进入全屏模式,但是当我纵向旋转时它不会退出。
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)
    }
}

这是我的观点:

 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();
    }

谁能帮我? 在纵向模式下旋转设备时,未调用函数“exitFullScreen”

    标签: ios swift mobile swiftui


    【解决方案1】:

    在这种情况下,SwiftUI 似乎失去了与可表示的联系......无论如何,在 UIKit 流程中处理 UIKit 事情会更好。可代表概念具有此类情况的协调员。

    因此,一种可能的修复方法是将所有内容移动到AVPlayerControllerRepresentable 中。

    这是主要部分(使用 Xcode 13.4 / iOS 15.5 测试):

        func makeUIViewController(context: UIViewControllerRepresentableContext<AVPlayerControllerRepresentable>) -> AVPlayerViewController {
            let controller  = AVPlayerViewController()
            controller.player = player
            controller.showsPlaybackControls = false;
    
            context.coordinator.playerController = controller
            return controller
        }
    
        class Coordinator: NSObject, AVPlayerViewControllerDelegate {
            weak var playerController: AVPlayerViewController? {
                didSet {
                    playerController?.delegate = self
                }
            }
    
            private var subscriber: AnyCancellable? = nil
            override init() {
                super.init()
                subscriber = NotificationCenter.default.publisher(for: UIDevice.orientationDidChangeNotification)
                    .sink { [weak self] _ in
                        self?.rotated()
                    }
            }
    
            func rotated() {
                if UIDevice.current.orientation.isLandscape {
                    self.enterFullScreen(animated: true)
                } else {
                    self.exitFullScreen(animated: true)
                }
            }
    
        //...
    

    Test module on GitHub

    【讨论】:

      【解决方案2】:

      @Asperi 很好的解决方案,谢谢! 但我在使用 iOS 15 的少数 iPhone 模拟器上遇到性能问题 - iPhone 13 Pro Max、iPhone 8 Plus,也在真实设备 iPhone 7 Plus 上以及仅在模拟器 iPod touch 第 7 代上 - 性能是预期的行为,意味着,每个旋转到横向 - 输入全屏实际上是全屏视频,对于所有其他视频都不会扩展到全屏!?见附件解释我的话。你能做一些笔记吗?提前致谢。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多