PlaybackActionCallback.java

Overview

`PlaybackActionCallback.java` defines a Java interface that serves as a contract for handling user and player actions related to media playback within the application. It provides a structured way to receive callbacks when specific playback events occur, such as progress updates, seeking, toggling play/pause, subtitle changes, and quality adjustments. This interface is designed to decouple the media player logic from UI updates or other system components that react to playback changes, facilitating modular and maintainable code.

The callback methods are invoked **after** the corresponding playback actions have been performed, enabling observers to update the UI or trigger other processes accordingly. This interface is particularly useful in scenarios such as reactive UI components, analytics tracking, or adaptive playback features.


Classes and Interface

PlaybackActionCallback

Description

An interface defining callbacks for media playback-related actions. Implementors of this interface receive notifications about playback events and user interactions with the media player.

Methods

Method Signature

Description

Parameters

Returns

`void onProgress(@Nullable MediaModel mediaModel, long milliseconds, long durationMillis)`

Invoked periodically or on significant playback progress updates.

  • mediaModel (nullable): The current media item being played.
    - milliseconds: Current playback position in milliseconds.
    - durationMillis: Total duration of the media in milliseconds.

void

`void onSeek(@Nullable MediaModel mediaModel, long oldPositionMillis, long newPositionMillis)`

Called after the playback position has been changed via seeking.

  • mediaModel (nullable): The current media item.
    - oldPositionMillis: The previous playback position.
    - newPositionMillis: The new playback position after seeking.

void

`void onPlayToggle(@Nullable MediaModel mediaModel, boolean playing)`

Triggered when playback is toggled between playing and paused states.

  • mediaModel (nullable): The current media item.
    - playing: true if playback started, false if paused.

void

`void onLearnMoreClick(@NonNull MediaModel mediaModel)`

Invoked when the user clicks a "Learn More" action related to the current media.

  • mediaModel (non-null): The current media item.

void

`void onSubtitles(@Nullable MediaModel mediaModel, boolean enabled)`

Called when subtitles are enabled or disabled during playback.

  • mediaModel (nullable): The current media item.
    - enabled: true if subtitles are enabled, else false.

void

`void onQuality(@Nullable MediaModel mediaModel)`

Invoked when the playback quality changes or needs to be handled.

  • mediaModel (nullable): The current media item.

void

`void onCuePointReceived(long[] cuePoints)`

Called when cue points (timestamps or markers) are received, possibly for triggering ads or chapters.

  • cuePoints: Array of cue point timestamps in milliseconds.

void

`boolean isActive()`

Indicates whether the callback implementation is currently active and should receive updates.

None

`true` if active, `false` otherwise


Implementation Details and Usage


Example Usage

public class PlaybackLogger implements PlaybackActionCallback {

    private boolean active = true;

    @Override
    public void onProgress(@Nullable MediaModel mediaModel, long milliseconds, long durationMillis) {
        if (mediaModel != null) {
            System.out.println("Playback progress for " + mediaModel.getTitle() + ": " + milliseconds + "/" + durationMillis);
        }
    }

    @Override
    public void onSeek(@Nullable MediaModel mediaModel, long oldPositionMillis, long newPositionMillis) {
        System.out.println("Seeked from " + oldPositionMillis + " to " + newPositionMillis);
    }

    @Override
    public void onPlayToggle(@Nullable MediaModel mediaModel, boolean playing) {
        System.out.println("Playback " + (playing ? "started" : "paused"));
    }

    @Override
    public void onLearnMoreClick(@NonNull MediaModel mediaModel) {
        System.out.println("Learn more clicked for: " + mediaModel.getTitle());
    }

    @Override
    public void onSubtitles(@Nullable MediaModel mediaModel, boolean enabled) {
        System.out.println("Subtitles " + (enabled ? "enabled" : "disabled"));
    }

    @Override
    public void onQuality(@Nullable MediaModel mediaModel) {
        System.out.println("Quality changed or requested for media");
    }

    @Override
    public void onCuePointReceived(long[] cuePoints) {
        System.out.println("Received cue points: " + Arrays.toString(cuePoints));
    }

    @Override
    public boolean isActive() {
        return active;
    }

    public void setActive(boolean active) {
        this.active = active;
    }
}

In this example, `PlaybackLogger` implements the interface to log playback events for debugging or analytics.


Interaction with Other Components


Mermaid Class Diagram

classDiagram
    interface PlaybackActionCallback {
        +void onProgress(MediaModel? mediaModel, long milliseconds, long durationMillis)
        +void onSeek(MediaModel? mediaModel, long oldPositionMillis, long newPositionMillis)
        +void onPlayToggle(MediaModel? mediaModel, boolean playing)
        +void onLearnMoreClick(MediaModel mediaModel)
        +void onSubtitles(MediaModel? mediaModel, boolean enabled)
        +void onQuality(MediaModel? mediaModel)
        +void onCuePointReceived(long[] cuePoints)
        +boolean isActive()
    }
    
    class MediaModel {
        <<external>>
    }
    
    PlaybackActionCallback ..> MediaModel : uses

Summary

`PlaybackActionCallback.java` is a fundamental interface in the media playback system that enables decoupled, event-driven communication about playback actions. Its design promotes modularity and flexibility by informing interested components post-action, supporting features like UI updates, analytics, and adaptive playback. The interface's reliance on `MediaModel` ensures contextual awareness for each callback, and the active state query optimizes event dispatch.

This interface plays a key role in integrating the media playback engine with user interaction layers and other system components, aligning with the overall modular architecture of the application.