MoviePlayingState.java
Overview
`MoviePlayingState.java` defines the **MoviePlayingState** class, a concrete state within the media player's Finite State Machine (FSM). This state represents the playback of the main movie content after advertisements have finished playing. Its primary function is to manage and coordinate the transition from ad playback back to content playback, ensuring a seamless viewing experience. It handles preparation of the movie player, resuming playback positions, subtitle visibility, and UI updates related to hiding ad components.
This class extends `BaseState` and overrides key methods to implement the behavior specific to playing movie content, including responding to inputs that trigger transitions to other states like making an ad call or finishing playback.
Class: MoviePlayingState
Inheritance
Extends:
BaseStateImplements:
State(via BaseState)
Purpose
Manage playback of the main movie content.
Transition to other states based on input events such as starting an ad call or finishing the movie.
Handle UI and player updates necessary when resuming movie playback after ads.
Manage subtitle visibility.
Clean up ad-related UI components and players after ads finish.
Public Methods
State transformToState(Input input, StateFactory factory)
Determines the next FSM state based on the input event.
Parameters:
Input input: The event/input triggering a potential transition.StateFactory factory: Factory to create instances of States.
Returns:
State: The next state if a valid transition exists; otherwise,null.
Behavior:
If the input is
MAKE_AD_CALL, transitions toMakingAdCallState.If the input is
MOVIE_FINISH, transitions toFinishState.For other inputs, returns
null(no state change).
Example:
State nextState = moviePlayingState.transformToState(Input.MAKE_AD_CALL, factory); // nextState will be an instance of MakingAdCallState
void performWorkAndUpdatePlayerUI(@NonNull FsmPlayer fsmPlayer)
Executes the work associated with this state and updates the player UI accordingly.
Parameters:
FsmPlayer fsmPlayer: The FSM player instance containing controllers and playback state.
Behavior:
Calls the superclass implementation.
If
fsmPlayerisnull, returns early.Calls
stopAdandPlayerMovie()to stop any ad playback and prepare the content player.Updates UI components (e.g., subtitles, VPAID WebView visibility).
Usage:
Typically invoked by the FSM player when this state becomes active to ensure the movie plays properly.
Private Methods
void stopAdandPlayerMovie(PlayerUIController controller, PlayerAdLogicController componentController, MediaModel movieMedia)
Stops ad playback and prepares the movie player for playback.
Parameters:
PlayerUIController controller: Controls UI and player instances.PlayerAdLogicController componentController: Controls ad logic.MediaModel movieMedia: The media model representing the movie content.
Behavior:
Removes analytics listener from the ad player.
Pauses the ad player if the device uses a single player for both ads and content.
Updates the ExoPlayer view to use the movie player and media model.
Prepares the movie player if needed:
Checks for resume position or player idle state.
Seeks to resume position if available.
Starts movie playback.
Marks that ads are no longer playing.
Hides VPAID WebView and shows the movie player UI.
Shows subtitles if enabled.
void updatePlayerPosition(SimpleExoPlayer moviePlayer, PlayerUIController controller)
Updates the movie player's playback position based on history or resume position.
Parameters:
SimpleExoPlayer moviePlayer: The ExoPlayer instance for movie playback.PlayerUIController controller: UI controller holding playback state.
Behavior:
If playback history exists, seeks to the history position and clears it.
Otherwise, seeks to the saved movie resume position if available.
void hideVpaidNShowPlayer(final PlayerUIController controller)
Hides the VPAID WebView and shows the movie player view.
Parameters:
PlayerUIController controller: UI controller managing views.
Behavior:
Sets the ExoPlayer view visibility to visible.
If the VPAID WebView exists:
Sets its visibility to gone.
Loads an empty URL to stop any running VPAID content.
Clears the WebView history.
boolean shouldShowSubtitle()
Determines whether subtitles should be visible in the current playback context.
Returns:
trueif subtitles are available for the video and enabled by the user.falseotherwise.
Behavior:
Retrieves the
UserControllerfrom the player controller.Checks flags:
videoHasSubtitleandisSubtitleEnabled.
Important Implementation Details
Single Player Optimization:
The methodstopAdandPlayerMoviechecks if the device uses a single player instance for both ads and content (PlayerDeviceUtils.useSinglePlayer()). If so, it carefully pauses the ad player without releasing it to reuse the player efficiently.Resume Position Handling:
The class respects previously saved playback positions (movieResumePosition) or playback history to resume movie playback where the user left off.UI Updates:
Manages visibility of subtitles and VPAID ad WebView to ensure proper user experience. Subtitles are only shown if both the video has subtitles and the user has enabled them.State Transitions:
This state only explicitly handles two inputs for transitions:MAKE_AD_CALL(to start fetching midroll ads) andMOVIE_FINISH(to end playback).
Usage Example
// Assuming fsmPlayer is an initialized FSM player object
MoviePlayingState moviePlayingState = new MoviePlayingState();
StateFactory factory = new StateFactory();
// Perform movie playback setup and UI updates
moviePlayingState.performWorkAndUpdatePlayerUI(fsmPlayer);
// Transition to making ad call state if triggered
State nextState = moviePlayingState.transformToState(Input.MAKE_AD_CALL, factory);
if (nextState != null) {
fsmPlayer.setCurrentState(nextState);
nextState.performWorkAndUpdatePlayerUI(fsmPlayer);
}
Interaction with Other System Components
FsmPlayer:
MoviePlayingStateis one of many concrete states managed by theFsmPlayerfinite state machine. The FSM delegates playback control and UI updates to this state when the player is actively playing movie content.PlayerUIController & PlayerAdLogicController:
These controllers manage the ExoPlayer instances, UI components, and ad playback logic.MoviePlayingStatecalls their methods to switch players, prepare media sources, and update UI elements.MediaModel:
Represents the movie content metadata and media source used for preparing the ExoPlayer instance.VPAID Integration:
The state handles hiding the VPAID WebView and restoring the movie player UI after interactive ads.StateFactory:
Used to instantiate other states when transitioning, e.g., toMakingAdCallStateorFinishState.
Mermaid Class Diagram
classDiagram
class MoviePlayingState {
+State transformToState(Input input, StateFactory factory)
+void performWorkAndUpdatePlayerUI(FsmPlayer fsmPlayer)
-void stopAdandPlayerMovie(PlayerUIController, PlayerAdLogicController, MediaModel)
-void updatePlayerPosition(SimpleExoPlayer, PlayerUIController)
-void hideVpaidNShowPlayer(PlayerUIController)
-boolean shouldShowSubtitle()
}
MoviePlayingState --|> BaseState
Summary
`MoviePlayingState` plays a critical role in the FSM playback system by managing the content playback phase. It ensures smooth transitions from ads back to movie playback, handles player preparation and resume logic, and updates UI components responsibly. Its design encapsulates the specifics of movie playback within the broader FSM, facilitating modular, maintainable, and testable playback state management.