From 1d5d920f88a642bc503b3c7cd47d1dd139a565f9 Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sun, 3 Apr 2022 16:46:33 +0200 Subject: [PATCH] Improve EOF handling --- Model/Player/Backends/MPVBackend.swift | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index 8d218831..b15fcf51 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -17,7 +17,17 @@ final class MPVBackend: PlayerBackend { var loadedVideo = false var isLoadingVideo = true { didSet { DispatchQueue.main.async { [weak self] in - self?.controls.isLoadingVideo = self?.isLoadingVideo ?? true + guard let self = self else { + return + } + + self.controls.isLoadingVideo = self.isLoadingVideo + + if !self.isLoadingVideo { + DispatchQueue.main.asyncAfter(deadline: .now() + 2) { [weak self] in + self?.handleEOF = true + } + } } }} @@ -39,6 +49,7 @@ final class MPVBackend: PlayerBackend { private var clientTimer: RepeatingTimer! + private var handleEOF = false private var onFileLoaded: (() -> Void)? private var controlsUpdates = false @@ -65,6 +76,7 @@ final class MPVBackend: PlayerBackend { } func playStream(_ stream: Stream, of video: Video, preservingTime: Bool, upgrading _: Bool) { + handleEOF = false let updateCurrentStream = { DispatchQueue.main.async { [weak self] in self?.stream = stream @@ -299,7 +311,7 @@ final class MPVBackend: PlayerBackend { } func handleEndOfFile(_: UnsafePointer!) { - guard !isLoadingVideo else { + guard handleEOF, !isLoadingVideo else { return }