88import com .newrelic .videoagent .core .NRVideoPlayerConfiguration ;
99import androidx .appcompat .app .AppCompatActivity ;
1010import androidx .media3 .common .MediaItem ;
11- import androidx .media3 .common .util .Util ;
1211import androidx .media3 .datasource .DataSource ;
13- import androidx .media3 .datasource .DefaultDataSourceFactory ;
14- import androidx .media3 .exoplayer .SimpleExoPlayer ;
12+ import androidx .media3 .datasource .DefaultDataSource ;
13+ import androidx .media3 .exoplayer .ExoPlayer ;
1514import androidx .media3 .exoplayer .ima .ImaAdsLoader ;
1615import androidx .media3 .exoplayer .source .DefaultMediaSourceFactory ;
1716import androidx .media3 .ui .PlayerView ;
17+ import androidx .annotation .OptIn ;
18+ import androidx .media3 .common .util .UnstableApi ;
1819
1920import android .util .Log ;
2021import com .newrelic .videoagent .core .NewRelicVideoAgent ;
2122import com .newrelic .videoagent .ima .tracker .NRTrackerIMA ;
2223
24+ /**
25+ * @OptIn is required for Media3 IMA ads integration APIs which are marked as @UnstableApi.
26+ * This is by design as per Google's Media3 documentation.
27+ * @see <a href="https://developer.android.com/media/media3/exoplayer/customization#unstable-api">Media3 Unstable API Documentation</a>
28+ */
29+ @ OptIn (markerClass = UnstableApi .class )
2330public class VideoPlayerAds extends AppCompatActivity implements AdErrorEvent .AdErrorListener , AdEvent .AdEventListener {
2431
25- private SimpleExoPlayer player ;
32+ private ExoPlayer player ;
2633 private Integer trackerId ;
2734 private ImaAdsLoader adsLoader ;
2835 private PlayerView playerView ;
@@ -67,13 +74,12 @@ protected void onDestroy() {
6774
6875 private void playVideo (String videoUrl ) {
6976 // Set up the factory for media sources, passing the ads loader and ad view providers.
70- DataSource .Factory dataSourceFactory =
71- new DefaultDataSourceFactory (this , Util .getUserAgent (this , getString (R .string .app_name )));
77+ DataSource .Factory dataSourceFactory = new DefaultDataSource .Factory (this );
7278 DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory (dataSourceFactory );
7379 mediaSourceFactory .setAdsLoaderProvider (unusedAdTagUri -> adsLoader );
7480 mediaSourceFactory .setAdViewProvider (playerView );
7581
76- player = new SimpleExoPlayer .Builder (this ).setMediaSourceFactory (mediaSourceFactory ).build ();
82+ player = new ExoPlayer .Builder (this ).setMediaSourceFactory (mediaSourceFactory ).build ();
7783 NRVideoPlayerConfiguration playerConfiguration = new NRVideoPlayerConfiguration ("test-player-something-else" , player , true , null );
7884 trackerId = NRVideo .addPlayer (playerConfiguration );
7985 adTracker = (NRTrackerIMA ) NewRelicVideoAgent .getInstance ().getAdTracker (trackerId );
@@ -93,7 +99,7 @@ private void playVideo(String videoUrl) {
9399 Uri contentUri = Uri .parse (videoUrl );
94100 Uri adTagUri = Uri .parse (getString (R .string .ad_tag_url ));
95101 MediaItem mediaItem = new MediaItem .Builder ().setUri (contentUri ).setAdTagUri (adTagUri ).build ();
96- // Prepare the content and ad to be played with the SimpleExoPlayer .
102+ // Prepare the content and ad to be played with the ExoPlayer .
97103 player .setMediaItem (mediaItem );
98104 // Set PlayWhenReady. If true, content and ads will autoplay.
99105 player .setPlayWhenReady (true );
0 commit comments