Main thread blocked using AVPlayer in SwiftUI

Hi folks,

iOS 17.4 - iPhone 14 Pro - Real device

I'm facing an error when I try to implement a simple VideoPlayer view:

Main thread blocked by synchronous property query on not-yet-loaded property (PreferredTransform) for HTTP(S) asset. This could have been a problem if this asset were being read from a slow network.

This is the code I'm using to show the video player:

struct ContentView: View {

    let player = AVPlayer(url: URL(string: "https://video.twimg.com/amplify_video/1760315643142750208/vid/avc1/640x360/-1etorSK7w2g9Nlc.mp4?tag=16")!)

    var body: some View {
        VideoPlayer(player: player)
    }
}

I noticed that the bundle initi method of AVPlayer doesn't trigger this error:

AVPlayer(url: Bundle.main.url(forResource: "video", withExtension: "mp4")!)

I also tried to load metadata's asset with:

try await AVAsset.load(_:)

Before giving It to AVPlayer via AVPlayerItem with no success :/

Does anyone is facing this error too ? Or is It a bug from the SDK ?

Replies

Hello :)

First things first: that piece of code is working on a fast internet connection, and I don't get any error message. As far as I can tell, the problem is that the loading process is placed to the Main Thread. That causes some blocking on slow internet connections. Some others had the same issue (more to find here).

You can try to use this:

struct ContentView: View {
    
    @State private var player = AVPlayer()
    
    var videoUrl: String = "https://video.twimg.com/amplify_video/1760315643142750208/vid/avc1/640x360/-1etorSK7w2g9Nlc.mp4?tag=16"
    
    var body: some View {
        VideoPlayer(player: player)
            .task {
                player = AVPlayer(url: URL(string: videoUrl)!)
            }
        
    }
}

Please let me now, if this fixes your error message. If not, we will find a solution.

Thanks for sharing your piece of code @Timo2303, unfortunately I get the same error. BUT, if you let write:

@State private var player: AVPlayer?

Here I don't get any issues 🤔

Plus, all the posts related to that problem are recents and targeting iOS 17