> ## Documentation Index
> Fetch the complete documentation index at: https://docs.interactivelayers.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Media3 and ExoPlayer

> Add a graphics surface to the PlayerView on Android and Android-based TVs.

Use Android API 23 or later and Media3 1.6.0 with HLS. Keep `exo_content_frame` in a `FrameLayout` if your `PlayerView` has a custom layout. Point Gradle at your checkout and attach on the main thread after assigning the player.

```groovy theme={null}
// settings.gradle
includeBuild('../IL-VideoPlayers-SDK/android')

// App module build.gradle
dependencies {
    implementation 'com.interactivelayers:media3-overlays:0.1.0'
}
```

```java theme={null}
import com.interactivelayers.overlay.OverlayController;

playerView.setPlayer(player);
OverlayController overlays = OverlayController.attach(playerView);
// With a session feed, use attach(playerView, overlayFeedUrl, sessionId).

// Call at the actual boundaries of an inserted spot.
overlays.setLinearAdActive(true);
overlays.setLinearAdActive(false);
```

By default the adapter reads cues in the Media3 HLS variant. For a viewer-specific agenda, use the three-argument attach form with the same session identity as playback. Call `stop()` in `onStop()`, `start()` in `onStart()` and `close()` before releasing the view or player. The `isPlayingAd()` state covers ads that Media3 knows about. Use `setLinearAdActive` for externally stitched spots.

## Remote control and product panel

**On TV, the remote belongs to the ad.** While an ad with something to act on is up (product tiles, a buy button or an open panel), the ad takes focus on its first tile. The arrows move between tiles without leaving the ad, and OK or Enter opens the product. Back or Escape goes back one step in the panel. When the panel closes, focus returns to the tile, or to the ad's first tile if that one is gone; if there is no ad left, focus goes back to where it was before the ad took it. Back on a tile with no panel open stays the app's.

**What changes for your app on TV.** The `PlayerView` receives the keys before the ad, so with its controls in use an arrow would only show them. For that reason, while such an ad is up, the SDK sets the controller aside with `setUseController(false)` and gives back the app's value afterwards.

* If the app turns the controller on in the meantime, the app's value stays. If the app turns it off in the meantime, it comes back on after the ad, because the SDK cannot tell that change from its own.
* The app's `ControllerVisibilityListener` sees the controller hide (`GONE`). The viewer sees the controls go while the ad is on screen, even if they were visible, and return afterwards.
* Media keys (play/pause, play, pause, fast forward, rewind, next, previous) keep acting on the player meanwhile, as the Media3 controls would. The exception: if focus is on the `PlayerView` itself, or on an app view inside it, the media key reaches neither the SDK nor the controls, so handle it in the app (for example with a `MediaSession`).
* The app does not need to add code. Nothing changes on phones for this part.

If your app uses its own controls instead of the `PlayerView` ones (for example, Leanback transport controls), hide them or stop holding focus while an ad with something to act on is up, so that focus can reach the ad. The ad takes focus once when it appears, and takes it back if focus is left without an owner.

**How long the panel stays (Android, phones and TV).** The scene graphic lasts exactly its window. A panel the viewer opened stays open after its ad ends, with no time limit, until the viewer closes it. It also closes when:

* the viewer opens another product;
* a new ad break starts (in a break with several ads, each new ad closes the previous ad's open product);
* the media changes;
* the app calls `stop()` or `close()`;
* the `PlayerView` is detached from the window.

It does not close on a seek, a pause, buffering, another ad appearing behind it or the app's controls. The impression and the product view are still counted once. A panel left open for many minutes can outlive its offer. If the viewer presses Buy and the ad server answers that the offer is gone (the delivery expired, or that variant no longer exists), the panel says «This offer is no longer available.» and offers no retry for that variant: **Continue with the QR** on TV and **Buy** on the phone stay off while that variant is selected. Choosing another variant turns the button on again, and if that one is gone too it is turned off as well. Any other failure keeps the message that the cart could not be prepared, with its retry. The panel stays closable, and the app adds no code. The web SDK follows the same rule: see How long the product panel stays in the [Player SDK](https://docs.interactivelayers.com/player-sdk.md) guide.

This was tested on emulators (Android TV 14, an Android 15 phone and Android 9), not with a physical remote or a real TV.

From the SDK root, run `gradle -p android testDebugUnitTest connectedDebugAndroidTest` with a test device or emulator. On the app's target TV, check restore after an L, seek, background/foreground transitions and one collector receipt per visible cue. [Shared fixture](https://docs.interactivelayers.com/player-sdk.md) · [Android project details](https://github.com/InteractiveLayerscom/IL-VideoPlayers-SDK/blob/main/android/README.md)

The library includes the Montserrat font for the product panels, so the AAR grows from about 150 KB to about 452 KB. See Typeface of the product panels in the [Player SDK](https://docs.interactivelayers.com/player-sdk.md) guide.

## IL ads from the plan

`AdPlanPlayer` reads the IL plan, plays the breaks, draws the Super, the L, the Overlay 300×250 on phones and tablets (not on Android TV or Fire TV) and the product, and shows **Buy** during an iADServer ad. There are two ways to use it. [Which to choose](https://docs.interactivelayers.com/player-sdk.md#native-app-ads)

### The SDK requests the plan

In **Settings → General → My own player**, choose Media3 / ExoPlayer and register the value of `getPackageName()` in **Package name of the Android app**. `load` requests `GET https://console.interactivelayers.com/il-player/app-plan?project=…&asset=…&app=android:<package name>` off the main thread; `start` waits at most 5 s for the plan and then lets the content play.

```java theme={null}
AdPlanPlayer plan = AdPlanPlayer.load(overlays, player, "https://console.interactivelayers.com", projectId, assetId,
        null, "en", (type, format, id) -> Log.i("IL", type + " " + id));
plan.start(() -> player.play());
```

* Without a plan, the content continues without ads and a single `plan_unavailable` arrives with the reason (`http_204`, `http_404`, `timeout`, `network`, `app_id`, `request`, `stitched` or `invalid`) and the `app=` sent. A `404` is the same answer for an unknown project, video or app: check the registration first.
* A `version: 2` plan expires after 120 s. `load` requests it again 20 s before `expires_at` and, if that fails, at 12 and 5 s, with the same shopping session. It emits `plan_renewed`, or `plan_renew_failed` once, after the last attempt; on expiry, `plan_expired`, removes its graphics and returns to the content if it was in a break. If you bring the plan yourself, renew it with `plan.replacePlan(json)`.
* With `commerce.break_url` in the plan, the SDK requests the card of the iADServer ad playing and draws **Buy** until the ad ends; a panel the viewer opened stays.
* On the Google Ad Manager requests it makes itself, the SDK adds `msid` (package name) and `an` (app display name) when the tag lacks them; never `url` or `description_url`. IMA adds the app identity on its own.
* **Lifecycle.** Call `plan.close()` in `onDestroy()` (in a fragment, `onDestroyView()`). If the `Activity` is destroyed without `close()`, the SDK closes itself and emits `plan_closed` (`configuration_change` or `activity_destroyed`). After a configuration change, such as a rotation, the app must call `load` or `attach` again with the new `PlayerView`; otherwise there are no ads and no **Buy**.

### The app plays its breaks

If the app requests its breaks from the [per-break API](https://docs.interactivelayers.com/vod-operations.md#app-break-graphics), your backend creates the playback with `"graphics": true` and passes `GET /v1/vod/playbacks/{id}/plan` (`cuts: []`) to the app. Connect it with `attach` and tell the SDK about each break and each ad. This mode needs neither IMA nor Google Mobile Ads.

```java theme={null}
AdPlanPlayer plan = AdPlanPlayer.attach(overlays, player, planJson, "https://console.interactivelayers.com", null, "en", events);
plan.setBreaks(new long[] {0, 58000});           // your break positions, in content ms
plan.start(() -> player.play());

plan.breakStarted(58000);                        // your break starts (0 for the pre-roll)
plan.adStarted(adTagUrl, ad.getAdId(), ad.getCreativeId());  // or plan.adStarted(adTagUrl, vastXml)
plan.adEnded();                                  // complete, skip or error
plan.breakEnded();                               // content is back
```

* **Report every break and every ad.** The SDK applies `scenes.pacing.cut_clearance_seconds` only around the positions from `setBreaks` and `breakStarted`. Without reports, graphics follow content time and there is no **Buy**.
* **Pass `null` as the session.** **Buy** uses the plan's `commerce.session`; with your own `sessionId` the SDK emits `buy_unavailable` once. Fetch `ad_tag_url` exactly as given.
* The ad must show in the same `PlayerView` or under it, because **Buy** is drawn over the `PlayerView`.
* If a reported break gets no call for 180 s in the foreground, the SDK ends it, emits `break_error` and draws again. The SDK never touches your player during your break.

### Verification

Unit tests, and instrumented tests on an Android 15 phone emulator and an Android 14 Android TV emulator. Not tested on a physical device or with real Google Ad Manager, IMA or iADServer ads in the latest round. Fire TV was not tested: it is treated as Android TV by inference. A project with server-side insertion gets a plan the SDK refuses (`stitched`).

[Ad plan on Android](https://github.com/InteractiveLayerscom/IL-VideoPlayers-SDK/blob/main/android/README.md#plan-de-anuncios)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.