From 7534b4951babc93d65fd526c45a9fc5763d8b506 Mon Sep 17 00:00:00 2001 From: Valentin Petrovych Date: Thu, 9 Dec 2021 14:08:57 +0200 Subject: [PATCH 1/2] fix(DemoJava): fix updated dependencies in PrebidDemoJava #312 --- .../app/RubiconInstreamVideoIMADemoActivity.java | 14 +++++++------- .../mobile/app/XandrInstreamVideoGamActivity.java | 12 ++++++------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/RubiconInstreamVideoIMADemoActivity.java b/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/RubiconInstreamVideoIMADemoActivity.java index f3b8936c7..672426a98 100644 --- a/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/RubiconInstreamVideoIMADemoActivity.java +++ b/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/RubiconInstreamVideoIMADemoActivity.java @@ -76,7 +76,7 @@ public void onComplete(ResultCode resultCode, Map unmodifiableMa HashSet sizes = new HashSet<>(); sizes.add(new AdSize(640, 480)); String uri = Util.generateInstreamUriForGam(Constants.DFP_VAST_ADUNIT_ID_RUBICON, sizes, unmodifiableMap); - adsLoader = new ImaAdsLoader(RubiconInstreamVideoIMADemoActivity.this, Uri.parse(uri)); +// adsLoader = new ImaAdsLoader(this, RubiconInstreamVideoIMADemoActivity.this, Uri.parse(uri)); initializePlayer(); } }); @@ -112,12 +112,12 @@ private void initializePlayer() { MediaSource mediaSource = mediaSourceFactory.createMediaSource(Uri.parse(getString(R.string.content_url))); - // Create the AdsMediaSource using the AdsLoader and the MediaSource. - AdsMediaSource adsMediaSource = - new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView); - - // Prepare the content and ad to be played with the SimpleExoPlayer. - player.prepare(adsMediaSource); +// // Create the AdsMediaSource using the AdsLoader and the MediaSource. +// AdsMediaSource adsMediaSource = +// new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView); +// +// // Prepare the content and ad to be played with the SimpleExoPlayer. +// player.prepare(adsMediaSource); // Set PlayWhenReady. If true, content and ads autoplay. player.setPlayWhenReady(false); diff --git a/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/XandrInstreamVideoGamActivity.java b/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/XandrInstreamVideoGamActivity.java index 61343fbe9..89c165bf6 100644 --- a/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/XandrInstreamVideoGamActivity.java +++ b/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/XandrInstreamVideoGamActivity.java @@ -75,7 +75,7 @@ public void onComplete(ResultCode resultCode, Map unmodifiableMa HashSet sizes = new HashSet<>(); sizes.add(new AdSize(640, 480)); String uri = Util.generateInstreamUriForGam("/19968336/Punnaghai_Instream_Video1", sizes, unmodifiableMap); - adsLoader = new ImaAdsLoader(XandrInstreamVideoGamActivity.this, Uri.parse(uri)); +// adsLoader = new ImaAdsLoader(XandrInstreamVideoGamActivity.this, Uri.parse(uri)); initializePlayer(); } }); @@ -112,11 +112,11 @@ private void initializePlayer() { mediaSourceFactory.createMediaSource(Uri.parse(getString(R.string.content_url))); // Create the AdsMediaSource using the AdsLoader and the MediaSource. - AdsMediaSource adsMediaSource = - new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView); - - // Prepare the content and ad to be played with the SimpleExoPlayer. - player.prepare(adsMediaSource); +// AdsMediaSource adsMediaSource = +// new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView); +// +// // Prepare the content and ad to be played with the SimpleExoPlayer. +// player.prepare(adsMediaSource); // Set PlayWhenReady. If true, content and ads autoplay. player.setPlayWhenReady(false); From 8ca947f8cfd08904383b701befda02c228260315 Mon Sep 17 00:00:00 2001 From: Valentin Petrovych Date: Mon, 13 Dec 2021 15:33:12 +0200 Subject: [PATCH 2/2] fix: update exoplayer API in DemoJava app #312 --- Example/PrebidDemoJava/build.gradle | 1 + .../RubiconInstreamVideoIMADemoActivity.java | 107 ++++++++--------- .../app/XandrInstreamVideoGamActivity.java | 109 ++++++++---------- 3 files changed, 95 insertions(+), 122 deletions(-) diff --git a/Example/PrebidDemoJava/build.gradle b/Example/PrebidDemoJava/build.gradle index b35761c75..053a74bc3 100644 --- a/Example/PrebidDemoJava/build.gradle +++ b/Example/PrebidDemoJava/build.gradle @@ -63,6 +63,7 @@ dependencies { implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'com.google.android.gms:play-services-ads:20.0.0' + implementation 'com.google.android.exoplayer:exoplayer:2.13.3' implementation 'com.google.android.exoplayer:exoplayer-core:2.13.3' implementation 'com.google.android.exoplayer:exoplayer-ui:2.13.3' implementation 'com.google.android.exoplayer:extension-ima:2.13.3' diff --git a/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/RubiconInstreamVideoIMADemoActivity.java b/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/RubiconInstreamVideoIMADemoActivity.java index 672426a98..5b89ed692 100644 --- a/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/RubiconInstreamVideoIMADemoActivity.java +++ b/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/RubiconInstreamVideoIMADemoActivity.java @@ -2,10 +2,9 @@ import android.net.Uri; import android.os.Bundle; - import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; - +import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.ext.ima.ImaAdsLoader; import com.google.android.exoplayer2.source.MediaSource; @@ -13,27 +12,31 @@ import com.google.android.exoplayer2.source.ads.AdsMediaSource; import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; - -import org.prebid.mobile.AdSize; -import org.prebid.mobile.AdUnit; -import org.prebid.mobile.Host; -import org.prebid.mobile.OnCompleteListener2; -import org.prebid.mobile.PrebidMobile; -import org.prebid.mobile.ResultCode; -import org.prebid.mobile.Signals; import org.prebid.mobile.Util; -import org.prebid.mobile.VideoAdUnit; +import org.prebid.mobile.*; -import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; -import java.util.Map; public class RubiconInstreamVideoIMADemoActivity extends AppCompatActivity { - AdUnit adUnit; + + VideoAdUnit adUnit; private PlayerView playerView; private SimpleExoPlayer player; private ImaAdsLoader adsLoader; + private Uri adsUri; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_instream_video); + playerView = findViewById(R.id.player_view); + + initPrebid(); + initVideoAdUnit(); + } @Override protected void onDestroy() { @@ -45,40 +48,30 @@ protected void onDestroy() { adsLoader.release(); } - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_instream_video); - playerView = findViewById(R.id.player_view); - + private void initPrebid() { PrebidMobile.setPrebidServerHost(Host.RUBICON); PrebidMobile.setPrebidServerAccountId(Constants.PBS_ACCOUNT_ID_RUBICON); PrebidMobile.setStoredAuctionResponse(Constants.PBS_STORED_RESPONSE_VAST_RUBICON); - VideoAdUnit.Parameters parameters = new VideoAdUnit.Parameters(); - parameters.setMimes(Arrays.asList("video/mp4")); - - parameters.setProtocols(Arrays.asList(Signals.Protocols.VAST_2_0)); - // parameters.setProtocols(Arrays.asList(new Signals.Protocols(2))); - - parameters.setPlaybackMethod(Arrays.asList(Signals.PlaybackMethod.AutoPlaySoundOff)); - // parameters.setPlaybackMethod(Arrays.asList(new Signals.PlaybackMethod(2))); + } + private void initVideoAdUnit() { + VideoAdUnit.Parameters parameters = new VideoAdUnit.Parameters(); + parameters.setMimes(Collections.singletonList("video/mp4")); + parameters.setProtocols(Collections.singletonList(Signals.Protocols.VAST_2_0)); + parameters.setPlaybackMethod(Collections.singletonList(Signals.PlaybackMethod.AutoPlaySoundOff)); parameters.setPlacement(Signals.Placement.InStream); - // parameters.setPlacement(new Signals.Placement(2)); - VideoAdUnit adUnit = new VideoAdUnit("1001-1", 640, 480); + adUnit = new VideoAdUnit("1001-1", 640, 480); adUnit.setParameters(parameters); - this.adUnit = adUnit; - // Create an AdsLoader with the ad tag url. - adUnit.fetchDemand(new OnCompleteListener2() { - @Override - public void onComplete(ResultCode resultCode, Map unmodifiableMap) { - HashSet sizes = new HashSet<>(); - sizes.add(new AdSize(640, 480)); - String uri = Util.generateInstreamUriForGam(Constants.DFP_VAST_ADUNIT_ID_RUBICON, sizes, unmodifiableMap); -// adsLoader = new ImaAdsLoader(this, RubiconInstreamVideoIMADemoActivity.this, Uri.parse(uri)); - initializePlayer(); - } + adUnit.fetchDemand((resultCode, keysMap) -> { + HashSet sizes = new HashSet<>(); + sizes.add(new AdSize(640, 480)); + adsUri = Uri.parse(Util.generateInstreamUriForGam(Constants.DFP_VAST_ADUNIT_ID_RUBICON, sizes, keysMap)); + + ImaAdsLoader.Builder imaBuilder = new ImaAdsLoader.Builder(RubiconInstreamVideoIMADemoActivity.this); + adsLoader = imaBuilder.build(); + + initializePlayer(); }); } @@ -93,34 +86,26 @@ private void releasePlayer() { player.release(); player = null; } - } private void initializePlayer() { - // Create a SimpleExoPlayer and set is as the player for content and ads. - player = new SimpleExoPlayer.Builder(this).build(); + SimpleExoPlayer.Builder playerBuilder = new SimpleExoPlayer.Builder(this); + player = playerBuilder.build(); playerView.setPlayer(player); adsLoader.setPlayer(player); - DataSource.Factory dataSourceFactory = - new DefaultDataSourceFactory(this, getString(R.string.app_name)); - - ProgressiveMediaSource.Factory mediaSourceFactory = - new ProgressiveMediaSource.Factory(dataSourceFactory); - - // Create the MediaSource for the content you wish to play. - MediaSource mediaSource = - mediaSourceFactory.createMediaSource(Uri.parse(getString(R.string.content_url))); + Uri uri = Uri.parse(getString(R.string.content_url)); + MediaItem mediaItem = MediaItem.fromUri(uri); + DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, getString(R.string.app_name)); + ProgressiveMediaSource.Factory mediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory); + MediaSource mediaSource = mediaSourceFactory.createMediaSource(mediaItem); -// // Create the AdsMediaSource using the AdsLoader and the MediaSource. -// AdsMediaSource adsMediaSource = -// new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView); -// -// // Prepare the content and ad to be played with the SimpleExoPlayer. -// player.prepare(adsMediaSource); + DataSpec dataSpec = new DataSpec(adsUri); + AdsMediaSource adsMediaSource = new AdsMediaSource(mediaSource, dataSpec, "ad", mediaSourceFactory, adsLoader, playerView); - // Set PlayWhenReady. If true, content and ads autoplay. - player.setPlayWhenReady(false); + player.setMediaSource(adsMediaSource); + player.setPlayWhenReady(true); + player.prepare(); } @Override diff --git a/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/XandrInstreamVideoGamActivity.java b/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/XandrInstreamVideoGamActivity.java index 89c165bf6..48a29adc1 100644 --- a/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/XandrInstreamVideoGamActivity.java +++ b/Example/PrebidDemoJava/src/main/java/org/prebid/mobile/app/XandrInstreamVideoGamActivity.java @@ -2,10 +2,9 @@ import android.net.Uri; import android.os.Bundle; - import androidx.annotation.Nullable; import androidx.appcompat.app.AppCompatActivity; - +import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.ext.ima.ImaAdsLoader; import com.google.android.exoplayer2.source.MediaSource; @@ -13,27 +12,32 @@ import com.google.android.exoplayer2.source.ads.AdsMediaSource; import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.upstream.DataSource; +import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; +import org.prebid.mobile.*; -import org.prebid.mobile.AdSize; -import org.prebid.mobile.AdUnit; -import org.prebid.mobile.Host; -import org.prebid.mobile.OnCompleteListener2; -import org.prebid.mobile.PrebidMobile; -import org.prebid.mobile.ResultCode; -import org.prebid.mobile.Signals; -import org.prebid.mobile.Util; -import org.prebid.mobile.VideoAdUnit; - -import java.util.Arrays; +import java.util.Collections; import java.util.HashSet; -import java.util.Map; + +import static org.prebid.mobile.Util.generateInstreamUriForGam; public class XandrInstreamVideoGamActivity extends AppCompatActivity { - AdUnit adUnit; + + VideoAdUnit adUnit; private PlayerView playerView; private SimpleExoPlayer player; private ImaAdsLoader adsLoader; + private Uri adsUri; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_instream_video); + playerView = findViewById(R.id.player_view); + + initPrebid(); + initVideoAdUnit(); + } @Override protected void onDestroy() { @@ -45,39 +49,29 @@ protected void onDestroy() { adsLoader.release(); } - @Override - protected void onCreate(@Nullable Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_instream_video); - playerView = findViewById(R.id.player_view); - + private void initPrebid() { PrebidMobile.setPrebidServerHost(Host.APPNEXUS); PrebidMobile.setPrebidServerAccountId("aecd6ef7-b992-4e99-9bb8-65e2d984e1dd"); - VideoAdUnit.Parameters parameters = new VideoAdUnit.Parameters(); - parameters.setMimes(Arrays.asList("video/mp4")); - - parameters.setProtocols(Arrays.asList(Signals.Protocols.VAST_2_0)); - // parameters.setProtocols(Arrays.asList(new Signals.Protocols(2))); - - parameters.setPlaybackMethod(Arrays.asList(Signals.PlaybackMethod.AutoPlaySoundOff)); - // parameters.setPlaybackMethod(Arrays.asList(new Signals.PlaybackMethod(2))); + } + private void initVideoAdUnit() { + VideoAdUnit.Parameters parameters = new VideoAdUnit.Parameters(); + parameters.setMimes(Collections.singletonList("video/mp4")); + parameters.setProtocols(Collections.singletonList(Signals.Protocols.VAST_2_0)); + parameters.setPlaybackMethod(Collections.singletonList(Signals.PlaybackMethod.AutoPlaySoundOff)); parameters.setPlacement(Signals.Placement.InStream); - // parameters.setPlacement(new Signals.Placement(2)); - VideoAdUnit adUnit = new VideoAdUnit("2c0af852-a55d-49dc-a5ca-ef7e141f73cc", 300, 250); + adUnit = new VideoAdUnit("2c0af852-a55d-49dc-a5ca-ef7e141f73cc", 300, 250); adUnit.setParameters(parameters); - this.adUnit = adUnit; - // Create an AdsLoader with the ad tag url. - adUnit.fetchDemand(new OnCompleteListener2() { - @Override - public void onComplete(ResultCode resultCode, Map unmodifiableMap) { - HashSet sizes = new HashSet<>(); - sizes.add(new AdSize(640, 480)); - String uri = Util.generateInstreamUriForGam("/19968336/Punnaghai_Instream_Video1", sizes, unmodifiableMap); -// adsLoader = new ImaAdsLoader(XandrInstreamVideoGamActivity.this, Uri.parse(uri)); - initializePlayer(); - } + adUnit.fetchDemand((resultCode, unmodifiableMap) -> { + HashSet sizes = new HashSet<>(); + sizes.add(new AdSize(640, 480)); + adsUri = Uri.parse(generateInstreamUriForGam("/19968336/Punnaghai_Instream_Video1", sizes, unmodifiableMap)); + + ImaAdsLoader.Builder imaBuilder = new ImaAdsLoader.Builder(XandrInstreamVideoGamActivity.this); + adsLoader = imaBuilder.build(); + + initializePlayer(); }); } @@ -96,30 +90,23 @@ private void releasePlayer() { } private void initializePlayer() { - // Create a SimpleExoPlayer and set is as the player for content and ads. - player = new SimpleExoPlayer.Builder(this).build(); + SimpleExoPlayer.Builder playerBuilder = new SimpleExoPlayer.Builder(this); + player = playerBuilder.build(); playerView.setPlayer(player); adsLoader.setPlayer(player); - DataSource.Factory dataSourceFactory = - new DefaultDataSourceFactory(this, getString(R.string.app_name)); - - ProgressiveMediaSource.Factory mediaSourceFactory = - new ProgressiveMediaSource.Factory(dataSourceFactory); - - // Create the MediaSource for the content you wish to play. - MediaSource mediaSource = - mediaSourceFactory.createMediaSource(Uri.parse(getString(R.string.content_url))); + Uri uri = Uri.parse(getString(R.string.content_url)); + MediaItem mediaItem = MediaItem.fromUri(uri); + DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, getString(R.string.app_name)); + ProgressiveMediaSource.Factory mediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory); + MediaSource mediaSource = mediaSourceFactory.createMediaSource(mediaItem); - // Create the AdsMediaSource using the AdsLoader and the MediaSource. -// AdsMediaSource adsMediaSource = -// new AdsMediaSource(mediaSource, dataSourceFactory, adsLoader, playerView); -// -// // Prepare the content and ad to be played with the SimpleExoPlayer. -// player.prepare(adsMediaSource); + DataSpec dataSpec = new DataSpec(adsUri); + AdsMediaSource adsMediaSource = new AdsMediaSource(mediaSource, dataSpec, "ad", mediaSourceFactory, adsLoader, playerView); - // Set PlayWhenReady. If true, content and ads autoplay. - player.setPlayWhenReady(false); + player.setMediaSource(adsMediaSource); + player.setPlayWhenReady(true); + player.prepare(); } @Override