I am embedding SwiftUI VideoPlayer
in a VStack
and see that the screen goes black (i.e. the content disappears even though video player gets autorotated) when the device is rotated. The issue happens even when I use AVPlayerViewController
(as UIViewControllerRepresentable). Is this a bug or I am doing something wrong?
var videoURL:URL
let player = AVPlayer()
var body: some View {
VStack {
VideoPlayer(player: player)
.frame(maxWidth:.infinity)
.frame(height:300)
.padding()
.ignoresSafeArea()
.background {
Color.black
}
.onTapGesture {
player.rate = player.rate == 0.0 ? 1.0 : 0.0
}
Spacer()
}
.ignoresSafeArea()
.background(content: {
Color.black
})
.onAppear {
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSession.Category.playback, mode: AVAudioSession.Mode.default, options: AVAudioSession.CategoryOptions.duckOthers)
} catch {
NSLog("Unable to set session category to playback")
}
let playerItem = AVPlayerItem(url: videoURL)
player.replaceCurrentItem(with: playerItem)
}
}