Код: Выделить всё
import SwiftUI
import AVKit
struct VideoPlayerView: View {
let videoURL: URL
@State private var player: AVPlayer?
// how to set it true/false based on AVPlayer controls visiblity?
@State private var controlsAreVisible = true
@State private var title = ""
var body: some View {
VStack {
if let player = player {
VideoPlayer(player: player)
.onAppear {
player.play()
}
.onDisappear {
player.pause()
}
} else {
Text("Failed to load video")
}
}
.onAppear {
player = AVPlayer(url: videoURL)
}
.onDisappear {
player = nil
}
.navigationBarBackButtonHidden(!controlsAreVisible)
.navigationBarHidden(true)
.navigationBarTitle(title, displayMode: .inline)
.statusBar(hidden: true)
.edgesIgnoringSafeArea(.all)
.onChange(of: controlsAreVisible) { newValue in
if (newValue) {
title = videoURL.lastPathComponent
} else {
title = ""
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... ange-event