AdPlayingMonitor.java


Overview

`AdPlayingMonitor.java` defines the `AdPlayingMonitor` class, a specialized event listener that monitors the playback state of video advertisements (ads) within the media player environment. Its primary purpose is to observe the lifecycle of ad playback—detecting when an ad finishes or encounters errors—and to notify the finite state machine (FSM) controlling player states so it can transition appropriately. Additionally, it implements a workaround for corrupted ad streams that get stuck buffering by seeking forward to keep playback progressing.

This class extends `EventLogger`, inheriting the ability to log playback events, and integrates tightly with the FSM layer (`FsmAdController`/`FsmPlayer`) to maintain synchronization between ad playback status and player state transitions.


Class: AdPlayingMonitor

public class AdPlayingMonitor extends EventLogger

Description

`AdPlayingMonitor` listens to key playback events from the ad player, handling ad completion, playback errors, and dropped frames to ensure smooth ad playback and robust error recovery. It acts as an intermediary between ExoPlayer's ad playback events and the FSM controlling the ad state machine.


Properties

Property

Type

Description

`fsmPlayer`

`FsmAdController`

Reference to the FSM ad controller/player instance. Used to invoke FSM state transitions based on playback events.


Constructor

public AdPlayingMonitor(@NonNull FsmPlayer fsmPlayer)
FsmPlayer myFsmPlayer = ...; // existing FSM player instance
AdPlayingMonitor adMonitor = new AdPlayingMonitor(myFsmPlayer);

Methods

1. onPlayerStateChanged

@Override
public void onPlayerStateChanged(EventTime eventTime, boolean playWhenReady, int playbackState)

2. onPlayerError

@Override
public void onPlayerError(EventTime eventTime, ExoPlaybackException error)

3. onDroppedVideoFrames

@Override
public void onDroppedVideoFrames(final EventTime eventTime, final int droppedFrames, final long elapsedM)

4. seekOrSkip

private void seekOrSkip()

Important Implementation Details


Interaction with Other System Components


Usage Example

// Assume existing FSM player instance managing ad playback
FsmPlayer fsmPlayer = ...;

// Create the AdPlayingMonitor with FSM reference
AdPlayingMonitor adPlayingMonitor = new AdPlayingMonitor(fsmPlayer);

// Register the monitor as a listener to the ExoPlayer ad player instance
SimpleExoPlayer adPlayer = fsmPlayer.getController().getAdPlayer();
adPlayer.addListener(adPlayingMonitor);

Once registered, `AdPlayingMonitor` automatically handles the lifecycle events of the ad playback, triggering FSM transitions upon ad completion or errors.


Mermaid Class Diagram

classDiagram
    class AdPlayingMonitor {
        +FsmAdController fsmPlayer
        +AdPlayingMonitor(FsmPlayer fsmPlayer)
        +void onPlayerStateChanged(EventTime eventTime, boolean playWhenReady, int playbackState)
        +void onPlayerError(EventTime eventTime, ExoPlaybackException error)
        +void onDroppedVideoFrames(EventTime eventTime, int droppedFrames, long elapsedM)
        -void seekOrSkip()
    }
    
    AdPlayingMonitor --|> EventLogger

Summary

The `AdPlayingMonitor` class plays a critical role in the ad playback lifecycle within the media player architecture. By observing ad player events and integrating with the FSM, it ensures:

Its design leverages ExoPlayer callbacks and FSM state management, contributing to a smooth and resilient advertisement viewing experience in the application.