首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVPlayer exitFullScreen

AVPlayer exitFullScreen
EN

Stack Overflow用户
提问于 2022-06-20 12:14:32
回答 1查看 333关注 0票数 2

我对SwiftUI和AVPlayer有个问题。当我在横向模式下旋转设备时,播放器进入全屏模式,但当我在纵向旋转时,它不会退出。

用于AVPlayer的结构:

代码语言:javascript
复制
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)
    }
}

这就是我的观点:

代码语言:javascript
复制
 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”函数

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-20 15:39:39

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

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

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

代码语言:javascript
复制
    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)
            }
        }

    //...

基于GitHub的测试模块

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72686807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档