Skip to content

Commit

Permalink
Add AdsLoader.focusSkipButton()
Browse files Browse the repository at this point in the history
This method allows to call through to `StreamManager.focus()` of the currently playing SSAI stream.

PiperOrigin-RevId: 501399144
  • Loading branch information
marcbaechinger authored and rohitjoins committed Jan 17, 2023
1 parent 055ed77 commit 0ba0c0e
Showing 1 changed file with 31 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,7 @@ private static State fromBundle(Bundle bundle) {

private final ServerSideAdInsertionConfiguration configuration;
private final Context context;
private final Map<ImaServerSideAdInsertionMediaSource, MediaSourceResourceHolder>
mediaSourceResources;
private final Map<String, MediaSourceResourceHolder> mediaSourceResources;
private final Map<String, AdPlaybackState> adPlaybackStateMap;

@Nullable private Player player;
Expand All @@ -401,6 +400,35 @@ public void setPlayer(Player player) {
this.player = player;
}

/**
* Puts the focus on the skip button, if a skip button is present and an ad is playing.
*
* @see StreamManager#focus()
*/
public void focusSkipButton() {
if (player == null) {
return;
}
if (player.getPlaybackState() != Player.STATE_IDLE
&& player.getPlaybackState() != Player.STATE_ENDED
&& player.getMediaItemCount() > 0) {
int currentPeriodIndex = player.getCurrentPeriodIndex();
Object adsId =
player
.getCurrentTimeline()
.getPeriod(currentPeriodIndex, new Timeline.Period())
.getAdsId();
if (adsId instanceof String) {
MediaSourceResourceHolder mediaSourceResourceHolder = mediaSourceResources.get(adsId);
if (mediaSourceResourceHolder != null
&& mediaSourceResourceHolder.imaServerSideAdInsertionMediaSource.streamManager
!= null) {
mediaSourceResourceHolder.imaServerSideAdInsertionMediaSource.streamManager.focus();
}
}
}
}

/**
* Releases resources.
*
Expand All @@ -427,7 +455,7 @@ private void addMediaSourceResources(
StreamPlayer streamPlayer,
com.google.ads.interactivemedia.v3.api.AdsLoader adsLoader) {
mediaSourceResources.put(
mediaSource, new MediaSourceResourceHolder(mediaSource, streamPlayer, adsLoader));
mediaSource.adsId, new MediaSourceResourceHolder(mediaSource, streamPlayer, adsLoader));
}

private AdPlaybackState getAdPlaybackState(String adsId) {
Expand Down

0 comments on commit 0ba0c0e

Please sign in to comment.