Cancel loading assets on switching backend

This commit is contained in:
Arkadiusz Fal 2022-11-13 13:28:25 +01:00
parent fe9b7c03ca
commit 815d0b3ae8
4 changed files with 18 additions and 1 deletions

View File

@ -62,7 +62,10 @@ final class AVPlayerBackend: PlayerBackend {
var aspectRatio: Double { var aspectRatio: Double {
#if os(iOS) #if os(iOS)
videoWidth! / videoHeight! guard let videoWidth, let videoHeight else {
return VideoPlayerView.defaultAspectRatio
}
return videoWidth / videoHeight
#else #else
VideoPlayerView.defaultAspectRatio VideoPlayerView.defaultAspectRatio
#endif #endif
@ -170,6 +173,11 @@ final class AVPlayerBackend: PlayerBackend {
avPlayer.replaceCurrentItem(with: nil) avPlayer.replaceCurrentItem(with: nil)
} }
func cancelLoads() {
asset?.cancelLoading()
composition.cancelLoading()
}
func seek(to time: CMTime, seekType _: SeekType, completionHandler: ((Bool) -> Void)?) { func seek(to time: CMTime, seekType _: SeekType, completionHandler: ((Bool) -> Void)?) {
guard !model.live else { return } guard !model.live else { return }

View File

@ -353,6 +353,10 @@ final class MPVBackend: PlayerBackend {
isPlaying ? pause() : play() isPlaying ? pause() : play()
} }
func cancelLoads() {
stop()
}
func stop() { func stop() {
client?.stop() client?.stop()
} }

View File

@ -69,6 +69,8 @@ protocol PlayerBackend {
func setNeedsDrawing(_ needsDrawing: Bool) func setNeedsDrawing(_ needsDrawing: Bool)
func setSize(_ width: Double, _ height: Double) func setSize(_ width: Double, _ height: Double)
func cancelLoads()
} }
extension PlayerBackend { extension PlayerBackend {

View File

@ -498,6 +498,9 @@ final class PlayerModel: ObservableObject {
let fromBackend: PlayerBackend = from == .appleAVPlayer ? avPlayerBackend : mpvBackend let fromBackend: PlayerBackend = from == .appleAVPlayer ? avPlayerBackend : mpvBackend
let toBackend: PlayerBackend = to == .appleAVPlayer ? avPlayerBackend : mpvBackend let toBackend: PlayerBackend = to == .appleAVPlayer ? avPlayerBackend : mpvBackend
toBackend.cancelLoads()
fromBackend.cancelLoads()
if !self.backend.canPlayAtRate(currentRate) { if !self.backend.canPlayAtRate(currentRate) {
currentRate = self.backend.suggestedPlaybackRates.last { $0 < currentRate } ?? 1.0 currentRate = self.backend.suggestedPlaybackRates.last { $0 < currentRate } ?? 1.0
} }