AsyncPublisher for AVPlayerItem not working

Hello, I'm trying to subscribe to AVPlayerItem status updates using Combine and it's bridge to Swift Concurrency – .values.

This is my sample code.


struct ContentView: View {
    @State var player: AVPlayer?
    @State var loaded = false

    var body: some View {
        VStack {
            if let player {
                Text("loading status: \(loaded)")
                Spacer()

                VideoPlayer(player: player)
                Button("Load") {
                    Task {
                        let item = AVPlayerItem(
                            url: URL(string: "https://sample-videos.com/video321/mp4/360/big_buck_bunny_360p_5mb.mp4")!
                        )
                        player.replaceCurrentItem(with: item)
                        let publisher = player.publisher(for: \.status)

                        for await status in publisher.values {
                            print(status.rawValue)

                            if status == .readyToPlay {
                                loaded = true
                                break
                            }
                        }
                        print("we are out")
                    }

                }
            }
            else {
                Text("No video selected")
            }
        }
        .task {
            player = AVPlayer()
        }
    }
}

After I click on the "load" button it prints out 0 (as the initial status of .unknown) and nothing after – even when the video is fully loaded.

At the same time this works as expected (loading status is set to true):


struct ContentView: View {
    @State var player: AVPlayer?
    @State var loaded = false
    @State var cancellable: AnyCancellable?

    var body: some View {
        VStack {
            if let player {
                Text("loading status: \(loaded)")
                Spacer()

                VideoPlayer(player: player)
                Button("Load") {
                    Task {
                        let item = AVPlayerItem(
                            url: URL(string: "https://sample-videos.com/video321/mp4/360/big_buck_bunny_360p_5mb.mp4")!
                        )
                        player.replaceCurrentItem(with: item)
                        let stream = AsyncStream { continuation in
                            cancellable = item.publisher(for: \.status)
                                .sink {
                                    if $0 == .readyToPlay {
                                        continuation.yield($0)
                                        continuation.finish()
                                    }
                                }
                        }

                        for await _ in stream {
                            loaded = true
                            cancellable?.cancel()
                            cancellable = nil
                            break
                        }
                    }

                }
            }
            else {
                Text("No video selected")
            }
        }
        .task {
            player = AVPlayer()
        }
    }
}

Is this a bug or something?

AsyncPublisher for AVPlayerItem not working
 
 
Q