我对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”函数
发布于 2022-06-20 15:39:39
在这种情况下,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)
}
}
//...https://stackoverflow.com/questions/72686807
复制相似问题