From 5449b2ed5db86d3b610aa74a3df70f689ec9e03a Mon Sep 17 00:00:00 2001 From: Alberto Sendra Date: Wed, 23 Oct 2024 12:20:11 +0200 Subject: [PATCH] Fix canPlay check for PlayableOfflinedMediaProduct --- Sources/Player/Mocks/PlaybackEngine/PlayerMock.swift | 7 ++++++- .../Internal/Storage/PlayableOfflinedMediaProduct.swift | 2 ++ .../Internal/AVQueuePlayer/AVQueuePlayerWrapper.swift | 4 +++- .../AVQueuePlayer/Legacy/AVQueuePlayerWrapperLegacy.swift | 4 +++- .../PlaybackEngine/Internal/InternalPlayerLoader.swift | 4 ++++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Sources/Player/Mocks/PlaybackEngine/PlayerMock.swift b/Sources/Player/Mocks/PlaybackEngine/PlayerMock.swift index 3025c8c0..22ef95d4 100644 --- a/Sources/Player/Mocks/PlaybackEngine/PlayerMock.swift +++ b/Sources/Player/Mocks/PlaybackEngine/PlayerMock.swift @@ -42,7 +42,12 @@ public final class PlayerMock: GenericMediaPlayer { public init(cachePath: URL, featureFlagProvider: FeatureFlagProvider) {} - public func canPlay(productType: PlayerProductType, codec: PlayerAudioCodec?, mediaType: String?, isOfflined: Bool) -> Bool { + public func canPlay( + productType: PlayerProductType, + codec: PlayerAudioCodec?, + mediaType: String?, + isOfflined: Bool + ) -> Bool { true } diff --git a/Sources/Player/OfflineEngine/Internal/Storage/PlayableOfflinedMediaProduct.swift b/Sources/Player/OfflineEngine/Internal/Storage/PlayableOfflinedMediaProduct.swift index 53a76fd5..602ce0bd 100644 --- a/Sources/Player/OfflineEngine/Internal/Storage/PlayableOfflinedMediaProduct.swift +++ b/Sources/Player/OfflineEngine/Internal/Storage/PlayableOfflinedMediaProduct.swift @@ -11,6 +11,7 @@ struct PlayableOfflinedMediaProduct: Equatable { let audioSampleRate: Int? let audioBitDepth: Int? let videoQuality: VideoQuality? + let mediaType: String? let mediaURL: URL let licenseURL: URL? let albumReplayGain: Float? @@ -36,6 +37,7 @@ struct PlayableOfflinedMediaProduct: Equatable { audioSampleRate = offlineEntry.audioSampleRate audioBitDepth = offlineEntry.audioBitDepth videoQuality = offlineEntry.videoQuality + mediaType = offlineEntry.mediaType mediaURL = URL licenseURL = offlineEntry.licenseURL albumReplayGain = offlineEntry.albumReplayGain diff --git a/Sources/Player/PlaybackEngine/Internal/AVQueuePlayer/AVQueuePlayerWrapper.swift b/Sources/Player/PlaybackEngine/Internal/AVQueuePlayer/AVQueuePlayerWrapper.swift index 156018b4..39e7a86b 100644 --- a/Sources/Player/PlaybackEngine/Internal/AVQueuePlayer/AVQueuePlayerWrapper.swift +++ b/Sources/Player/PlaybackEngine/Internal/AVQueuePlayer/AVQueuePlayerWrapper.swift @@ -81,7 +81,9 @@ final class AVQueuePlayerWrapper: GenericMediaPlayer { guard let codec else { return false } - return supportedCodecs.contains(codec) || (codec == .FLAC && !isOfflined) + // Because we need to support items downloaded with both the legacy and the new OfflineEngine + // We can't just add .FLAC to the 'supportedCodecs' array, and we need to depend on the mediaType + return supportedCodecs.contains(codec) || (codec == .FLAC && mediaType != MediaTypes.BTS) } func load( diff --git a/Sources/Player/PlaybackEngine/Internal/AVQueuePlayer/Legacy/AVQueuePlayerWrapperLegacy.swift b/Sources/Player/PlaybackEngine/Internal/AVQueuePlayer/Legacy/AVQueuePlayerWrapperLegacy.swift index 653c5886..3eb1b181 100644 --- a/Sources/Player/PlaybackEngine/Internal/AVQueuePlayer/Legacy/AVQueuePlayerWrapperLegacy.swift +++ b/Sources/Player/PlaybackEngine/Internal/AVQueuePlayer/Legacy/AVQueuePlayerWrapperLegacy.swift @@ -88,7 +88,9 @@ final class AVQueuePlayerWrapperLegacy: GenericMediaPlayer { guard let codec else { return false } - return supportedCodecs.contains(codec) || (codec == .FLAC && !isOfflined) + // Because we need to support items downloaded with both the legacy and the new OfflineEngine + // We can't just add .FLAC to the 'supportedCodecs' array, and we need to depend on the mediaType + return supportedCodecs.contains(codec) || (codec == .FLAC && mediaType != MediaTypes.BTS) } } diff --git a/Sources/Player/PlaybackEngine/Internal/InternalPlayerLoader.swift b/Sources/Player/PlaybackEngine/Internal/InternalPlayerLoader.swift index d1c9b6d8..08e7d85d 100644 --- a/Sources/Player/PlaybackEngine/Internal/InternalPlayerLoader.swift +++ b/Sources/Player/PlaybackEngine/Internal/InternalPlayerLoader.swift @@ -71,7 +71,9 @@ final class InternalPlayerLoader: PlayerLoader { let player = try getPlayer( for: offlinedProduct.audioMode, and: offlinedProduct.audioQuality, + with: offlinedProduct.mediaType, audioCodec: offlinedProduct.audioCodec, + isOfflined: true, type: offlinedProduct.productType ) @@ -100,6 +102,7 @@ final class InternalPlayerLoader: PlayerLoader { let player = try getPlayer( for: storedMediaProduct.audioMode, and: storedMediaProduct.audioQuality, + with: MediaTypes.BTS, audioCodec: storedMediaProduct.audioCodec, isOfflined: true, type: storedMediaProduct.productType @@ -134,6 +137,7 @@ final class InternalPlayerLoader: PlayerLoader { and: playbackInfo.audioQuality, with: playbackInfo.mediaType, audioCodec: playbackInfo.audioCodec, + isOfflined: false, type: playbackInfo.productType ) return await loadTrack(using: playbackInfo, with: loudnessNormalizer, and: licenseLoader, player: player)