diff --git a/kDriveCore/AudioPlayer/SingleTrackPlayer.swift b/kDriveCore/AudioPlayer/SingleTrackPlayer.swift index 86948791b..dd612e9ae 100644 --- a/kDriveCore/AudioPlayer/SingleTrackPlayer.swift +++ b/kDriveCore/AudioPlayer/SingleTrackPlayer.swift @@ -23,7 +23,7 @@ import kDriveResources import MediaPlayer /// Track one file been played -public final class SingleTrackPlayer { +public final class SingleTrackPlayer: NSObject { @LazyInjectService private var orchestrator: MediaPlayerOrchestrator let registeredCommands: [NowPlayableCommand] = [ @@ -143,6 +143,14 @@ public final class SingleTrackPlayer { await self.onCurrentTrackMetadata.send(self.extractTrackMetadata(from: asset)) self.player = AVPlayer(playerItem: AVPlayerItem(asset: asset)) self.setUpObservers() + + let context = UnsafeMutableRawPointer(Unmanaged.passUnretained(playableFile).toOpaque()) + self.player?.addObserver( + self, + forKeyPath: #keyPath(AVPlayer.currentItem.status), + options: [.new, .initial], + context: context + ) } } else { self.onPlaybackError.send(.previewLoadErrorNoToken) @@ -153,6 +161,22 @@ public final class SingleTrackPlayer { } } + override public func observeValue( + forKeyPath keyPath: String?, + of object: Any?, + change: [NSKeyValueChangeKey: Any]?, + context: UnsafeMutableRawPointer? + ) { + guard keyPath == #keyPath(AVPlayer.currentItem.status), + let error = player?.currentItem?.error, + let avError = error as? AVError, + avError.code == .fileFormatNotRecognized, + let context else { return } + + let playableFile = Unmanaged.fromOpaque(context).takeUnretainedValue() + print(error) + } + public func reset() { removeAllObservers() player?.pause()