From 16285ca5dfd4461334f5e97d4de47ae07e49e883 Mon Sep 17 00:00:00 2001 From: bachinger Date: Wed, 11 Jan 2023 23:46:58 +0000 Subject: [PATCH] Add AdsLoader.focusSkipButton() This method allows to call through to `StreamManager.focus()` of the currently playing SSAI stream. PiperOrigin-RevId: 501399144 --- RELEASENOTES.md | 3 ++ .../ImaServerSideAdInsertionMediaSource.java | 34 +++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index f1abb0e2df..f9a9f23ced 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -73,6 +73,9 @@ Release notes * Add a property `focusSkipButtonWhenAvailable` to the `ImaServerSideAdInsertionMediaSource.AdsLoader.Builder` to request focusing the skip button on TV devices and set it to true by default. + * Add a method `focusSkipButton()` to the + `ImaServerSideAdInsertionMediaSource.AdsLoader` to programmatically + request to focus the skip button. * Bump IMA SDK version to 3.29.0. * Demo app * Request notification permission for download notifications at runtime diff --git a/libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaServerSideAdInsertionMediaSource.java b/libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaServerSideAdInsertionMediaSource.java index 831bf183be..959d873cf8 100644 --- a/libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaServerSideAdInsertionMediaSource.java +++ b/libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/ImaServerSideAdInsertionMediaSource.java @@ -376,8 +376,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou private final ServerSideAdInsertionConfiguration configuration; private final Context context; - private final Map - mediaSourceResources; + private final Map mediaSourceResources; private final Map adPlaybackStateMap; @Nullable private Player player; @@ -403,6 +402,35 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou 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. * @@ -429,7 +457,7 @@ public final class ImaServerSideAdInsertionMediaSource extends CompositeMediaSou 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) {