From c00267128117ca099507e86bf3e753a0d7cec470 Mon Sep 17 00:00:00 2001 From: Francesco <100066266+cranci1@users.noreply.github.com> Date: Tue, 8 Apr 2025 14:25:40 +0200 Subject: [PATCH] fixed media player --- .../ContinueWatchingItem.swift | 1 + .../CustomPlayer/CustomPlayer.swift | 42 +++++++++---------- Sora/Utils/MediaPlayer/VideoPlayer.swift | 40 +++++++++--------- Sora/Views/LibraryView/LibraryView.swift | 1 + Sora/Views/MediaInfoView/MediaInfoView.swift | 1 + 5 files changed, 45 insertions(+), 40 deletions(-) diff --git a/Sora/Utils/ContinueWatching/ContinueWatchingItem.swift b/Sora/Utils/ContinueWatching/ContinueWatchingItem.swift index c91d677..7490295 100644 --- a/Sora/Utils/ContinueWatching/ContinueWatchingItem.swift +++ b/Sora/Utils/ContinueWatching/ContinueWatchingItem.swift @@ -16,5 +16,6 @@ struct ContinueWatchingItem: Codable, Identifiable { let streamUrl: String let fullUrl: String let subtitles: String? + let aniListID: Int? let module: ScrapingModule } diff --git a/Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift b/Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift index 72fa09e..c6e71b1 100644 --- a/Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift +++ b/Sora/Utils/MediaPlayer/CustomPlayer/CustomPlayer.swift @@ -29,6 +29,7 @@ class CustomMediaPlayerViewController: UIViewController { let episodeImageUrl: String let subtitlesURL: String? let onWatchNext: () -> Void + let aniListID: Int var player: AVPlayer! var timeObserverToken: Any? @@ -56,7 +57,6 @@ class CustomMediaPlayerViewController: UIViewController { var lastDuration: Double = 0.0 var watchNextButtonAppearedAt: Double? - // MARK: - Constraint Sets var portraitButtonVisibleConstraints: [NSLayoutConstraint] = [] var portraitButtonHiddenConstraints: [NSLayoutConstraint] = [] var landscapeButtonVisibleConstraints: [NSLayoutConstraint] = [] @@ -120,6 +120,7 @@ class CustomMediaPlayerViewController: UIViewController { episodeNumber: Int, onWatchNext: @escaping () -> Void, subtitlesURL: String?, + aniListID: Int, episodeImageUrl: String) { self.module = module @@ -130,6 +131,7 @@ class CustomMediaPlayerViewController: UIViewController { self.episodeImageUrl = episodeImageUrl self.onWatchNext = onWatchNext self.subtitlesURL = subtitlesURL + self.aniListID = aniListID super.init(nibName: nil, bundle: nil) @@ -268,28 +270,9 @@ class CustomMediaPlayerViewController: UIViewController { UserDefaults.standard.set(playbackSpeed, forKey: "lastPlaybackSpeed") } - if let currentItem = player.currentItem, currentItem.duration.seconds > 0 { - let progress = currentTimeVal / currentItem.duration.seconds - let item = ContinueWatchingItem( - id: UUID(), - imageUrl: episodeImageUrl, - episodeNumber: episodeNumber, - mediaTitle: titleText, - progress: progress, - streamUrl: streamURL, - fullUrl: fullUrl, - subtitles: subtitlesURL, - module: module - ) - ContinueWatchingManager.shared.save(item: item) - } } - override func observeValue(forKeyPath keyPath: String?, - of object: Any?, - change: [NSKeyValueChangeKey : Any]?, - context: UnsafeMutableRawPointer?) { - + override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { guard context == &playerItemKVOContext else { super.observeValue(forKeyPath: keyPath, of: object, change: change, context: context) return @@ -1011,6 +994,23 @@ class CustomMediaPlayerViewController: UIViewController { }) } } + + if let currentItem = player.currentItem, currentItem.duration.seconds > 0 { + let progress = currentTimeVal / currentItem.duration.seconds + let item = ContinueWatchingItem( + id: UUID(), + imageUrl: episodeImageUrl, + episodeNumber: episodeNumber, + mediaTitle: titleText, + progress: progress, + streamUrl: streamURL, + fullUrl: fullUrl, + subtitles: subtitlesURL, + aniListID: aniListID, + module: module + ) + ContinueWatchingManager.shared.save(item: item) + } } func repositionWatchNextButton() { diff --git a/Sora/Utils/MediaPlayer/VideoPlayer.swift b/Sora/Utils/MediaPlayer/VideoPlayer.swift index 88a5314..1f001c3 100644 --- a/Sora/Utils/MediaPlayer/VideoPlayer.swift +++ b/Sora/Utils/MediaPlayer/VideoPlayer.swift @@ -17,6 +17,7 @@ class VideoPlayerViewController: UIViewController { var streamUrl: String? var fullUrl: String = "" var subtitles: String = "" + var aniListID: Int = 0 var episodeNumber: Int = 0 var episodeImageUrl: String = "" @@ -87,25 +88,6 @@ class VideoPlayerViewController: UIViewController { player?.removeTimeObserver(timeObserverToken) self.timeObserverToken = nil } - - if let currentItem = player?.currentItem, currentItem.duration.seconds > 0, - let streamUrl = streamUrl { - let currentTime = UserDefaults.standard.double(forKey: "lastPlayedTime_\(fullUrl)") - let duration = currentItem.duration.seconds - let progress = currentTime / duration - let item = ContinueWatchingItem( - id: UUID(), - imageUrl: episodeImageUrl, - episodeNumber: episodeNumber, - mediaTitle: mediaTitle, - progress: progress, - streamUrl: streamUrl, - fullUrl: fullUrl, - subtitles: subtitles, - module: module - ) - ContinueWatchingManager.shared.save(item: item) - } } private func setInitialPlayerRate() { @@ -131,6 +113,26 @@ class VideoPlayerViewController: UIViewController { UserDefaults.standard.set(currentTime, forKey: "lastPlayedTime_\(fullURL)") UserDefaults.standard.set(duration, forKey: "totalTime_\(fullURL)") } + + if let currentItem = player.currentItem, currentItem.duration.seconds > 0, + let streamUrl = streamUrl { + let currentTime = UserDefaults.standard.double(forKey: "lastPlayedTime_\(fullUrl)") + let duration = currentItem.duration.seconds + let progress = currentTime / duration + let item = ContinueWatchingItem( + id: UUID(), + imageUrl: episodeImageUrl, + episodeNumber: episodeNumber, + mediaTitle: mediaTitle, + progress: progress, + streamUrl: streamUrl, + fullUrl: fullUrl, + subtitles: subtitles, + aniListID: aniListID, + module: module + ) + ContinueWatchingManager.shared.save(item: item) + } } override var supportedInterfaceOrientations: UIInterfaceOrientationMask { diff --git a/Sora/Views/LibraryView/LibraryView.swift b/Sora/Views/LibraryView/LibraryView.swift index ade2c46..9dc269b 100644 --- a/Sora/Views/LibraryView/LibraryView.swift +++ b/Sora/Views/LibraryView/LibraryView.swift @@ -248,6 +248,7 @@ struct ContinueWatchingCell: View { episodeNumber: item.episodeNumber, onWatchNext: { }, subtitlesURL: item.subtitles, + aniListID: item.aniListID ?? 0, episodeImageUrl: item.imageUrl ) customMediaPlayer.modalPresentationStyle = .fullScreen diff --git a/Sora/Views/MediaInfoView/MediaInfoView.swift b/Sora/Views/MediaInfoView/MediaInfoView.swift index 76a63d9..2c1788a 100644 --- a/Sora/Views/MediaInfoView/MediaInfoView.swift +++ b/Sora/Views/MediaInfoView/MediaInfoView.swift @@ -725,6 +725,7 @@ struct MediaInfoView: View { selectNextEpisode() }, subtitlesURL: subtitles, + aniListID: itemID ?? 0, episodeImageUrl: selectedEpisodeImage ) customMediaPlayer.modalPresentationStyle = .fullScreen