google / google/ExoPlayer

Allow setting AdsLoader (and AdViewProvider) dynamically/on-demand.

Open
#6,159 3 comments 0 reactions 1 assignee Claimed by @andrewlewis View on GitHub
enhancement
Dominant language
Java
Stars
21.9k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

### [REQUIRED] Use case description
The scenario is: I have a MediaSource that can be played in either list/background/full-screen and I would like to have ads content only in full-screen playback. The setup also require non-blocking playback, i.e I don't want to re-prepare the MediaSource at all.

Current implementation requires AdsLoader and AdViewProvider when constructing AdsMediaSource. If I use PlayerView as the AdViewProvider, the background playback may leak this instance (as the foreground service keeps playing after the Activity is destroyed). Acknowledge that I can have a custom implementation of AdViewProvider in a way that can avoid leaking, doing so is not trivial.

On the other hand, what AdsMediaSource is doing with AdsLoader are: (1) starting it (i.e kick-off the logic that involve ads content to be taken into account when playing the main content), (2) stop it, (3) recover on error. I think instead of having AdsLoader/AdViewProvider as fixed final fields, having a ``AdsMediaSource.setAdsStuff(nullable AdsLoader, nullable AdViewProvider)`` may give this class more flexibility.

### Proposed solution

- Making AdsLoader/AdViewProvider non-final.
- Introducing setter for these 2 fields (in one or 2 methods).
- Pre-condition: if AdsLoader is notnull then AdViewProvider must be notnull.
- When client call setter for AdsLoader/AdViewProvider, AdsMediaSource may call AdsLoader.start() if it is already prepared, or call AdsLoader.start() when it is being prepared.
- Other methods (stop/handle preparation error) can be kept unchanged.
- Setting AdsLoader to null = disabling ads playback. AdsMediaSource would call AdsLoader.stop() and do other cleanup for ads content.

### Alternatives considered
I plan to implement a draft following my proposal above, but if you guys think it is a reasonable request, I'm happy to wait.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.