AVPlayer Flashing When Switching Videos In SwiftUI

27 Views Asked by At

I have a tabView which is playing multiple videos. Every time I switch tab, video flash for a second before playing. I have tried to use AVPlayerItem but the result is same.

struct VideoView: View {

var videoObject: VideoModel

@State var videoPlayer : AVPlayer?
@Binding var currentTab: Int

var body: some View {
    ZStack {
         VideoPlayer(player: videoPlayer)
    }
    .onChange(of: currentTab) { _ in
        if videoObject.tag == currentTab {
             player = AVPlayer(url: url)
             player.play()
        } else {
             player.pause()
        }
    }
}

struct VideoListView: View {
@State var currentTab: Int
@State var videoObjects = //some array of video objects

var body: some View {
    ZStack {
        TabView(selection: $currentTab) {
            ForEach(videoObjects, id: \.id) { video in
                VideoView(videoModel: video, currentTab: $currentTab)
                    .tag(video.id)
            }
        }
        .tabViewStyle(.page(indexDisplayMode: .never))
      }
   }
}
0

There are 0 best solutions below