Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add player item monitor for when an item reaches the end when action at end is none #133

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ final class AVPlayerItemMonitor {
private let completelyDownloadedMonitor: CompletelyDownloadedMonitor
private let readyToPlayMonitor: ReadyToPlayMonitor
private let inStreamMetadataMonitor: DJSessionTransitionMonitor
private let playerItemDidPlayToEndTimeMonitor: ItemPlayedToEndMonitor

init(
_ playerItem: AVPlayerItem,
onFailure: @escaping (AVPlayerItem, Error?) -> Void,
onStall: @escaping (AVPlayerItem) -> Void,
onCompletelyDownloaded: @escaping (AVPlayerItem) -> Void,
onReadyToPlayToPlay: @escaping (AVPlayerItem) -> Void,
onItemPlayedToEnd: @escaping (AVPlayerItem) -> Void,
onDjSessionTransition: @escaping (AVPlayerItem, DJSessionTransition) -> Void
) {
failureMonitor = FailureMonitor(playerItem, onFailure)
failedToPlayToEndMonitor = FailedToPlayToEndMonitor(playerItem, onFailure)
stallMonitor = StallMonitor(playerItem, onStall)
completelyDownloadedMonitor = CompletelyDownloadedMonitor(playerItem, onCompletelyDownloaded)
readyToPlayMonitor = ReadyToPlayMonitor(playerItem, onReadyToPlayToPlay)
playerItemDidPlayToEndTimeMonitor = ItemPlayedToEndMonitor(playerItem: playerItem, onPlayedToEnd: onItemPlayedToEnd)
inStreamMetadataMonitor = DJSessionTransitionMonitor(
with: playerItem,
and: DispatchQueue.global(qos: .default),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,7 @@ private extension AVQueuePlayerWrapper {
onStall: stalled,
onCompletelyDownloaded: downloaded,
onReadyToPlayToPlay: loaded,
onItemPlayedToEnd: playedToEnd,
onDjSessionTransition: receivedDjSessionTransition
)
}
Expand Down Expand Up @@ -631,6 +632,12 @@ private extension AVQueuePlayerWrapper {
self.delegates.djSessionTransition(asset: asset, transition: transition)
}
}

func playedToEnd(playerItem: AVPlayerItem) {
if featureFlagProvider.shouldNotPerformActionAtItemEnd() {
self.player.remove(playerItem)
}
}
}

// MARK: AssetFactoryDelegate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ private extension AVQueuePlayerWrapperLegacy {
onStall: stalled,
onCompletelyDownloaded: downloaded,
onReadyToPlayToPlay: loaded,
onItemPlayedToEnd: playedToEnd,
onDjSessionTransition: receivedDjSessionTransition
)
}
Expand Down Expand Up @@ -530,6 +531,8 @@ private extension AVQueuePlayerWrapperLegacy {
let asset = self.playerItemAssets.removeValue(forKey: oldPlayerItem)
self.delegates.completed(asset: asset)

self.player.remove(oldPlayerItem)

guard let newPlayerItem = self.player.currentItem else {
self.reset()
return
Expand Down Expand Up @@ -560,6 +563,12 @@ private extension AVQueuePlayerWrapperLegacy {
self.delegates.djSessionTransition(asset: asset, transition: transition)
}
}

func playedToEnd(playerItem: AVPlayerItem) {
if featureFlagProvider.shouldNotPerformActionAtItemEnd() {
self.player.remove(playerItem)
}
}
}

private extension AVQueuePlayerWrapperLegacy {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import AVFoundation
import Foundation

final class ItemPlayedToEndMonitor {
private let playerItem: AVPlayerItem
private let onPlayedToEnd: (AVPlayerItem) -> Void

init(playerItem: AVPlayerItem, onPlayedToEnd: @escaping (AVPlayerItem) -> Void) {
self.playerItem = playerItem
self.onPlayedToEnd = onPlayedToEnd

NotificationCenter.default.addObserver(
self,
selector: #selector(playerItemDidPlayToEndTime(notification:)),
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: playerItem
)
}

deinit {
NotificationCenter.default.removeObserver(
self,
name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
object: playerItem
)
}

@objc
private func playerItemDidPlayToEndTime(notification: Notification) {
if let playerItem = notification.object as? AVPlayerItem {
print("--> PlayerItemDidPlayToEndTimeMonitor.playerItemDidPlayToEndTime(notification: \(notification))")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be deleted, or convert into a proper Player Log?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, deleted, I always add a lot of prints to get the whole flow, will delete it.

onPlayedToEnd(playerItem)
}
}
}
Loading