From b5f8a0fba2812791c09e216903ddde2e7a9cafbc Mon Sep 17 00:00:00 2001 From: Arkadiusz Fal Date: Sat, 20 Aug 2022 22:28:31 +0200 Subject: [PATCH] Minor performance improvement --- Model/Player/Backends/AVPlayerBackend.swift | 12 +++++ Model/Player/Backends/MPVBackend.swift | 7 +++ Model/Player/PlayerModel.swift | 58 +++++++++++---------- 3 files changed, 49 insertions(+), 28 deletions(-) diff --git a/Model/Player/Backends/AVPlayerBackend.swift b/Model/Player/Backends/AVPlayerBackend.swift index acad5b5a..6858c1d0 100644 --- a/Model/Player/Backends/AVPlayerBackend.swift +++ b/Model/Player/Backends/AVPlayerBackend.swift @@ -489,6 +489,18 @@ final class AVPlayerBackend: PlayerBackend { if let currentTime = self.currentTime { self.model.handleSegments(at: currentTime) } + + #if !os(macOS) + guard UIApplication.shared.applicationState != .background else { + print("not performing controls updates in background") + return + } + #endif + + if self.controlsUpdates { + self.playerTime.duration = self.playerItemDuration ?? .zero + self.playerTime.currentTime = self.currentTime ?? .zero + } } } diff --git a/Model/Player/Backends/MPVBackend.swift b/Model/Player/Backends/MPVBackend.swift index 45d2d25c..ab82bde5 100644 --- a/Model/Player/Backends/MPVBackend.swift +++ b/Model/Player/Backends/MPVBackend.swift @@ -342,6 +342,13 @@ final class MPVBackend: PlayerBackend { return } + #if !os(macOS) + guard UIApplication.shared.applicationState != .background else { + self.logger.info("not performing controls updates in background") + return + } + #endif + self.playerTime.currentTime = self.currentTime ?? .zero self.playerTime.duration = self.playerItemDuration ?? .zero } diff --git a/Model/Player/PlayerModel.swift b/Model/Player/PlayerModel.swift index 046c8b42..bea4ac4d 100644 --- a/Model/Player/PlayerModel.swift +++ b/Model/Player/PlayerModel.swift @@ -789,36 +789,38 @@ final class PlayerModel: ObservableObject { } func updateNowPlayingInfo() { - guard let video = currentItem?.video else { - MPNowPlayingInfoCenter.default().nowPlayingInfo = .none - return - } - - let currentTime = (backend.currentTime?.seconds.isFinite ?? false) ? backend.currentTime!.seconds : 0 - var nowPlayingInfo: [String: AnyObject] = [ - MPMediaItemPropertyTitle: video.title as AnyObject, - MPMediaItemPropertyArtist: video.author as AnyObject, - MPNowPlayingInfoPropertyIsLiveStream: live as AnyObject, - MPNowPlayingInfoPropertyElapsedPlaybackTime: currentTime as AnyObject, - MPNowPlayingInfoPropertyPlaybackQueueCount: queue.count as AnyObject, - MPNowPlayingInfoPropertyPlaybackQueueIndex: 1 as AnyObject, - MPMediaItemPropertyMediaType: MPMediaType.anyVideo.rawValue as AnyObject - ] - - if !currentArtwork.isNil { - nowPlayingInfo[MPMediaItemPropertyArtwork] = currentArtwork as AnyObject - } - - if !video.live { - let itemDuration = (backend.playerItemDuration ?? .zero).seconds - let duration = itemDuration.isFinite ? Double(itemDuration) : nil - - if !duration.isNil { - nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration as AnyObject + #if !os(tvOS) + guard let video = currentItem?.video else { + MPNowPlayingInfoCenter.default().nowPlayingInfo = .none + return } - } - MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo + let currentTime = (backend.currentTime?.seconds.isFinite ?? false) ? backend.currentTime!.seconds : 0 + var nowPlayingInfo: [String: AnyObject] = [ + MPMediaItemPropertyTitle: video.title as AnyObject, + MPMediaItemPropertyArtist: video.author as AnyObject, + MPNowPlayingInfoPropertyIsLiveStream: live as AnyObject, + MPNowPlayingInfoPropertyElapsedPlaybackTime: currentTime as AnyObject, + MPNowPlayingInfoPropertyPlaybackQueueCount: queue.count as AnyObject, + MPNowPlayingInfoPropertyPlaybackQueueIndex: 1 as AnyObject, + MPMediaItemPropertyMediaType: MPMediaType.anyVideo.rawValue as AnyObject + ] + + if !currentArtwork.isNil { + nowPlayingInfo[MPMediaItemPropertyArtwork] = currentArtwork as AnyObject + } + + if !video.live { + let itemDuration = (backend.playerItemDuration ?? .zero).seconds + let duration = itemDuration.isFinite ? Double(itemDuration) : nil + + if !duration.isNil { + nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = duration as AnyObject + } + } + + MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo + #endif } func updateCurrentArtwork() {