From cd4cb28adec1c3dbb07ac1494db2055b9a0eaed2 Mon Sep 17 00:00:00 2001 From: Stephen Byatt <47413006+stephenb10@users.noreply.github.com> Date: Tue, 22 Jun 2021 11:07:44 +1000 Subject: [PATCH] Now playing centre changes --- .../VideoPlayerViewController.swift | 62 ++++++++++++++++--- 1 file changed, 54 insertions(+), 8 deletions(-) diff --git a/JellyfinPlayer tvOS/VideoPlayer/VideoPlayerViewController.swift b/JellyfinPlayer tvOS/VideoPlayer/VideoPlayerViewController.swift index 173f1f97..200961e9 100644 --- a/JellyfinPlayer tvOS/VideoPlayer/VideoPlayerViewController.swift +++ b/JellyfinPlayer tvOS/VideoPlayer/VideoPlayerViewController.swift @@ -236,7 +236,7 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, // If no default audio tracks select the first one if selectedAudioTrack == -1 && !audioTrackArray.isEmpty{ selectedAudioTrack = audioTrackArray.first!.id - } + } self.sendPlayReport() @@ -291,6 +291,7 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, commandCenter.seekForwardCommand.isEnabled = true commandCenter.seekBackwardCommand.isEnabled = true commandCenter.changePlaybackPositionCommand.isEnabled = true + commandCenter.enableLanguageOptionCommand.isEnabled = true // Add handler for Pause Command commandCenter.pauseCommand.addTarget { _ in @@ -340,18 +341,58 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, } } +// commandCenter.enableLanguageOptionCommand.addTarget { [weak self](remoteEvent) in +// guard let self = self else {return .commandFailed} +// +// +// +// } - MPNowPlayingInfoCenter.default().nowPlayingInfo = [ - MPMediaItemPropertyTitle: "TestVideo", - MPNowPlayingInfoPropertyPlaybackRate : 1.0, - MPNowPlayingInfoPropertyMediaType : AVMediaType.video, - MPMediaItemPropertyPlaybackDuration : manifest.runTimeTicks ?? 0 / 10_000_000, - MPNowPlayingInfoPropertyElapsedPlaybackTime : mediaPlayer.time.intValue/1000 - ] + var runTicks = 0 + var playbackTicks = 0 + + if let ticks = manifest.runTimeTicks { + runTicks = Int(ticks / 10_000_000) + } + + if let ticks = manifest.userData?.playbackPositionTicks { + playbackTicks = Int(ticks / 10_000_000) + } + + var nowPlayingInfo = [String: Any]() + + nowPlayingInfo[MPMediaItemPropertyTitle] = manifest.name! + nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = 0.0 + nowPlayingInfo[MPNowPlayingInfoPropertyMediaType] = AVMediaType.video + nowPlayingInfo[MPMediaItemPropertyPlaybackDuration] = runTicks + nowPlayingInfo[MPNowPlayingInfoPropertyElapsedPlaybackTime] = playbackTicks + + if let imageData = NSData(contentsOf: manifest.getPrimaryImage(maxWidth: 200)) { + let artworkImage = UIImage(data: imageData as Data) + nowPlayingInfo[MPMediaItemPropertyArtwork] = artworkImage + } + + MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo + UIApplication.shared.beginReceivingRemoteControlEvents() } + func updateNowPlayingCenter(time : Double?, playing : Bool?) { + + var nowPlayingInfo = MPNowPlayingInfoCenter.default().nowPlayingInfo ?? [String: Any]() + + if let playing = playing { + nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = playing ? 1.0 : 0.0 + } + if let time = time { + nowPlayingInfo[MPNowPlayingInfoPropertyPlaybackRate] = time + } + + MPNowPlayingInfoCenter.default().nowPlayingInfo = nowPlayingInfo + + } + // Grabs a refference to the info panel view controller @@ -381,6 +422,8 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, self.sendProgressReport(eventName: "pause") + self.updateNowPlayingCenter(time: nil, playing: false) + animateScrubber() self.scrubLabel.frame = CGRect(x: self.scrubberView.frame.minX - self.scrubLabel.frame.width/2, y:self.scrubLabel.frame.minY, width: self.scrubLabel.frame.width, height: self.scrubLabel.frame.height) @@ -390,6 +433,8 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, playing = true mediaPlayer.play() + self.updateNowPlayingCenter(time: nil, playing: true) + self.sendProgressReport(eventName: "unpause") animateScrubber() @@ -696,6 +741,7 @@ class VideoPlayerViewController: UIViewController, VideoPlayerSettingsDelegate, activityIndicator.isHidden = true activityIndicator.stopAnimating() } + updateNowPlayingCenter(time: nil, playing: true) } let time = mediaPlayer.position