PlayerControllerUI.java
Overview
`PlayerControllerUI` is a custom Android UI component designed to provide media playback controls within the application. It extends `FrameLayout` and acts as a controller interface for video playback, allowing users to interact with the media player through play/pause toggling, rewinding, and fast-forwarding actions.
This class leverages Android Data Binding to connect the UI controls with a `UserController` instance, which encapsulates the actual media control logic. User interactions via button clicks trigger corresponding commands on the `UserController`, and playback state or position updates are logged for debugging and monitoring purposes.
Class: PlayerControllerUI
public class PlayerControllerUI extends FrameLayout implements View.OnClickListener
Description
A UI component that houses playback controls (rewind, play/pause, fast-forward).
Implements
View.OnClickListenerto handle button click events.Uses Android Data Binding with the layout
R.layout.example_ui_control.Communicates user commands to the media through the
UserControllerinterface.
Properties
Property | Type | Description |
|---|---|---|
`private static final String TAG` | `String` | Tag used for logging, typically the class name. |
`private UserController mUserController` | `UserController` | Reference to the media controller managing playback. |
`private boolean playOrPause` | `boolean` | Tracks play/pause state toggle (true = play, false = pause). Defaults to `true`. |
`ExampleUiControlBinding binding` | `ExampleUiControlBinding` | Data binding instance linked to the UI layout. |
Constructors
public PlayerControllerUI(Context context)
public PlayerControllerUI(Context context, @Nullable AttributeSet attrs)
public PlayerControllerUI(Context context, @Nullable AttributeSet attrs, int defStyleAttr)
Purpose: Initialize the UI component with different constructor signatures to support XML inflation and programmatic instantiation.
Each constructor delegates to the most specific one, which calls
initLayout(context)to inflate and bind the layout.
Methods
View setController(UserController controller)
public View setController(UserController controller)
Description: Associates the
PlayerControllerUIwith aUserControllerinstance to delegate playback commands.Parameters:
controller: An instance ofUserControllerthat controls video playback.
Returns:
this(thePlayerControllerUIinstance itself) to allow chaining or direct usage as aView.Usage Example:
PlayerControllerUI playerUI = new PlayerControllerUI(context);
playerUI.setController(userControllerInstance);
void onClick(View v)
@Override
public void onClick(final View v)
Description: Handles click events for rewind, play/pause, and fast-forward buttons.
Parameters:
v: The clickedViewobject.
Behavior:
Checks if
mUserControlleris set; if not, ignores the click.Reads the tag from the clicked view to determine the action:
"rewind": Seeks backward 15 seconds."play_pause": Toggles play/pause state."fastford": Seeks forward 15 seconds.
After each action, logs the event and prints current video details via
printVideoDetail().
Implementation Notes:
Utilizes
mUserController.seekBy(long milliseconds)for seeking.Uses
mUserController.triggerPlayOrPause(boolean play)to control playback.Relies on
ExoPlayerLoggerfor logging.
private void initLayout(Context context)
private void initLayout(Context context)
Description: Inflates the UI layout
example_ui_control.xmlwith data binding and sets click listeners on control buttons.Parameters:
context: Android context to access resources and inflater.
Implementation Details:
Uses
DataBindingUtil.inflate()to bind the layout.Sets this class as the click listener for buttons with IDs:
rewind,play_pause, andfastford.
Usage: Called internally from constructors to set up the UI.
private void printVideoDetail()
private void printVideoDetail()
Description: Logs detailed information about the current video playback state.
Details Logged:
Current video name (
mUserController.getCurrentVideoName())Current video duration (
mUserController.currentDuration())Current playback position (
mUserController.currentProgressPosition())
Purpose: Aids debugging by providing real-time playback info upon user interaction.
Implementation Details
Data Binding: The UI is bound through
ExampleUiControlBindinglinking the XML layoutexample_ui_controlto this component, facilitating reactive UI updates.UserController Interface: Abstracts media player control logic, isolating UI from playback implementation.
Seek Operations: Seeking is done by fixed 15-second increments backward or forward.
Play/Pause State Management: Maintains an internal boolean flag (
playOrPause) toggled on each play/pause button click, which is passed to the controller.Logging: All user interactions and playback details are logged using
ExoPlayerLoggerfor monitoring and troubleshooting.
Interaction with Other Components
UserController: Core interface used by this UI to control media playback. Expected to implement methods like
seekBy(),triggerPlayOrPause(), and getters for video details.Data Binding Layout (
example_ui_control): Defines the UI elements (buttons) and their tags used to identify actions.ExoPlayerLogger: Utility class used for logging playback actions and states.
Application UI Layer: This component integrates into the app's UI hierarchy, providing visible and interactive controls for video playback.
Usage Example
// In an Activity or Fragment
PlayerControllerUI playerControllerUI = new PlayerControllerUI(context);
UserController userController = mediaPlayer.getUserController(); // hypothetical media player controller
playerControllerUI.setController(userController);
// Add playerControllerUI to the view hierarchy
rootLayout.addView(playerControllerUI);
Visual Diagram
classDiagram
class PlayerControllerUI {
-UserController mUserController
-boolean playOrPause
-ExampleUiControlBinding binding
+PlayerControllerUI(Context)
+PlayerControllerUI(Context, AttributeSet)
+PlayerControllerUI(Context, AttributeSet, int)
+View setController(UserController)
+void onClick(View)
-void initLayout(Context)
-void printVideoDetail()
}
PlayerControllerUI ..> UserController : uses
PlayerControllerUI ..> ExampleUiControlBinding : binds to
PlayerControllerUI ..> ExoPlayerLogger : calls logging
Summary
`PlayerControllerUI.java` is a robust, reusable component encapsulating video playback controls in an Android app. It cleanly separates UI concerns from playback logic via the `UserController` interface, uses data binding for UI management, and provides detailed logging for debugging. This component is integral to the media playback user experience and interacts closely with the media control backend and UI layout resources.