The following type of problem has appeared. I need to flip through the video. Imagine you have, say, 3 videos, and you can scroll through them and choose which video you want to watch.
For this, I decided to use a TabView with the .page style. But it turned out that it didn't work. And I found myself in a stupor.
The TabView itself starts to lag, the scrolling starts to lag, the videos do not start the first time, and sometimes the control panel does not even appear on some videos, which is why it is impossible to expand the video to full screen.
The code will be below, maybe someone has encountered this problem, how did he solve it, maybe there are some other options to make a similar logic?
let videos: [String] = ["burpee", "squat", "step-up", "sun-salute"]
var body: some View {
TabView {
ForEach(videos, id: \.self) { videoName in
VideoPlayerView(videoName: videoName)
.clipShape(RoundedRectangle(cornerRadius: 25))
}
}
.frame(width: 375, height: 230)
}
struct VideoPlayerView: View {
let videoName: String
var body: some View {
if let videoURL = Bundle.main.url(forResource: videoName, withExtension: "mp4") {
VideoPlayerWrapper(player: AVPlayer(url: videoURL))
} else {
Text("No Video \(videoName)")
}
}
}
#Preview {
VideoPlayerView(videoName: "squat")
}
struct VideoPlayerWrapper: UIViewControllerRepresentable {
let player: AVPlayer
func makeUIViewController(context: Context) -> AVPlayerViewController {
let controller = AVPlayerViewController()
controller.player = player
controller.showsPlaybackControls = true
return controller
}
func updateUIViewController(_ uiViewController: AVPlayerViewController, context: Context) {}
}