Skip to content

Commit

Permalink
fix: Handle AVPlayer.currentItem.status error
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Weidmann <[email protected]>
  • Loading branch information
PhilippeWeidmann committed Nov 1, 2024
1 parent 8cb54f5 commit 85c0e12
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion kDriveCore/AudioPlayer/SingleTrackPlayer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] = [
Expand Down Expand Up @@ -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)
Expand All @@ -153,6 +161,22 @@ public final class SingleTrackPlayer {
}
}

override public func observeValue(

Check failure on line 164 in kDriveCore/AudioPlayer/SingleTrackPlayer.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Prefer the new block based KVO API with keypaths when using Swift 3.2 or later (block_based_kvo)
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<File>.fromOpaque(context).takeUnretainedValue()
print(error)
}

public func reset() {
removeAllObservers()
player?.pause()
Expand Down

0 comments on commit 85c0e12

Please sign in to comment.