0

I'm trying to use SwiftUI to create an album in which you can scroll through the video. Faced the problem if the video array players: [AVPlayer] is large (count somewhere > 5), then problems begin with the player control panel, which ceases to be displayed

import SwiftUI
import AVKit

struct ContentView: View {
    @State var players: [AVPlayer] = [
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        AVPlayer(url: URL(string: "https://test-videos.co.uk/vids/bigbuckbunny/mp4/h264/1080/Big_Buck_Bunny_1080_10s_1MB.mp4")!),
        
    ]

    var body: some View {
        TabView {
            ForEach(0..<players.count, id: \.self) { i in
                    VideoPlayer(player: players[i])
                        .frame(width: 400, height: 300, alignment: .center)
                        .clipped()
                        .onAppear() {
                            // Start the player going, otherwise controls don't appear
                            players[i].play()
                        }
                        .onDisappear() {
                            // Stop the player when the view disappears
                            players[i].pause()
                        }
//                .tag(UUID())

            }
        }
            .tabViewStyle(.page(indexDisplayMode: .never))
            .indexViewStyle(.page(backgroundDisplayMode: .interactive))

    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
  • This seems to be a simulator issue. I tested on the physical device and everything was fine. – BenTheProgrammer May 21 '23 at 18:16
  • I recall reading in the documentation that there is a limited number of instances of AVPlayer that can be active at the same time. The number is not available through an API call or something like that but if you exceed that number, playback fails silently. The number depends on the device (and, probably, on other factors such as whether other apps are using active instances of AVPlayer, say, in a picture-in-picture playback scenario). Here's a similar question: https://stackoverflow.com/questions/40474480/how-many-avplayers-are-allowed-to-be-created-at-the-same-time – Baglan May 22 '23 at 11:26
  • @Baglan - looks like you're right. But I don’t know how can to scroll through the list of videos and play them in this case by swiftui ((((( – Олег Мосейко May 22 '23 at 13:45
  • @ОлегМосейко One possibility would be to select one "central" video (that can be done with `GeometryReader` and preferences) and only play only that video. I think Apple uses that approach in the AppStore app. It would be nice to, at least show a frame of a video when scrolling, fortunately, that doesn't require an `AVPlayer` and can be done with an `AVAssetImageGenerator`. – Baglan May 23 '23 at 15:54

0 Answers0