Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VideoPlayer instance in a collection view causes CPU to 100% #66

Open
stefanosn opened this issue Jul 17, 2024 · 2 comments
Open

VideoPlayer instance in a collection view causes CPU to 100% #66

stefanosn opened this issue Jul 17, 2024 · 2 comments

Comments

@stefanosn
Copy link

stefanosn commented Jul 17, 2024

Hello sir. I have implemented a collection view. An instance of the VideoPlayer is created for each one cell. In each cell i have this code from your example.

struct ContentView : View {
https://github.com/State private var autoReplay: Bool = true
https://github.com/State private var mute: Bool = false
https://github.com/State private var play: Bool = true
https://github.com/State private var time: CMTime = .zero

var body: some View {
    VideoPlayer(url: someVideoURL, play: $play, time: $time)
        .autoReplay(autoReplay)
        .mute(mute)
        .onBufferChanged { progress in
            // Network loading buffer progress changed
        }
        .onPlayToEndTime { 
            // Play to the end time.
        }
        .onReplay { 
            // Replay after playing to the end. 
        }
        .onStateChanged { state in 
            switch state {
            case .loading:
                // Loading...
            case .playing(let totalDuration):
                // Playing...
            case .paused(let playProgress, let bufferProgress):
                // Paused...
            case .error(let error):
                // Error...
            }
        }
}
}

Let me explain what is my issue exactly. The collection view has 20 items. If i tap on a cell i notice that the video starts playing and the CPU is at around 30%. I load at collection view more items. 200 items to be exact. If i tap on a cell and the collection view has 200 items, i am noticing that the video starts playing but the CPU is at 100% for more than 5 seconds. I am noticing that $time binding constantly triggering even when the video is not playing. Do you have any suggestion or idea what this might be?

Any help appreciated.

@akardas16
Copy link

I have same issue.

@n41l
Copy link

n41l commented Dec 24, 2024

Have you ever try to trun off autoReplay? I've found that this option may cause the $time exceeding the total duration of the video which will trigger off onPlayToEnd and onReplay callbacks again and again, finally boosting the usage of CPU.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants