VideoPlayer instance in a collection view causes CPU to 100%
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.
I have same issue.
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.