【发布时间】: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”