Skip to content

Surface end-of-media to hosts — the engine already tracks didReachEnd, then collapses it to .idle #63

Closed

Description

@rrgomes

A host that wants end-of-playback behavior (mark-watched, autoplay-next, return to the previous screen) can't distinguish "finished" from "idle" through the public API. Each playback host already tracks it — NativeAVPlayerHost.didReachEnd (@Published private(set)), and the Audio/Software hosts — but the engine consumes it internally (AetherEngine+Loading.swift: didReachEnd.filter { $0 }.sink { state = .idle }), so the public surface only ever shows .idle, indistinguishable from pre-load.

On the native path a host can work around it by observing the currentAVPlayer it's handed (AVPlayerItemDidPlayToEndTime). On the software-decode path there's no public AVPlayer to observe, so there's no host-side recourse at all.

Since the engine already has didReachEnd per host, could it surface end-of-media publicly — a terminal PlaybackState case (.ended), an engine-level @Published didReachEnd, or an onPlaybackEnded event? Any of those lets a host run end-of-playback handling on every backend without the AVPlayer workaround.

(The MinimalPlayer example treats .idle as stopped and re-enables Play, so it doesn't need the distinction — this only bites full hosts that act on end-of-media.)

Thanks!

Activity

  1. superuser404notfound commented on Jun 24, 2026

    @superuser404notfound
    Owner

    Shipped in 4.0.0.

    PlaybackState now has a dedicated terminal case, .ended, set on end-of-media across every backend (native, software, audio) through the same wireCommonHostSinks path that previously routed didReachEnd to .idle. stop() still goes to .idle, so the two are now distinct: .ended means the source played to completion, .idle means pre-load or stopped. .ended is terminal, so seek and togglePlayPause are no-ops, and the next load(...) clears it.

    player.$state.sink { state in
        switch state {
        case .ended: markWatched(); playNextEpisode()
        case .idle:  break   // pre-load / stopped
        default:     break
        }
    }

    Of your three suggestions I went with the terminal PlaybackState case rather than an engine-level @Published didReachEnd or an onPlaybackEnded event: end-of-media is mutually exclusive with playing/paused and fits the existing state machine cleanly, where the boolean overlays (isBuffering, isSeeking) are reserved for conditions that coexist with another state.

    This went out as a major (4.0.0) on purpose. Adding a case to the non-frozen PlaybackState enum is source-breaking for an exhaustive switch that lacks an @unknown default, and shipping it as a major keeps from:-pinned adopters opting in deliberately instead of breaking on a routine swift package update. Migration is just adding case .ended wherever you previously treated .idle as end-of-media.

    Thanks for the clear write-up, especially for calling out that the software-decode path has no public AVPlayer to observe, which is exactly what made this worth a first-class signal rather than a documented workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions