'How to implement VAST Tag in Exoplayer using HLS

I need help on the Exo player to implement pre-roll and mid-roll ads using the VAST tag. I have to use third-party ads not google ads

I already implemented the HLS player to play videos and it is working as our expectation.

    private void initializePlayer() {
    if (player != null) {
        player.release();
    }
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, "exoplayer-demo");
    trackSelector = new DefaultTrackSelector(/* context= */ this);

    com.google.android.exoplayer2.MediaItem mediaItem = new MediaItem.Builder()
            .setUri(/*Uri.parse("https://www.abc.in/video-data/2/master.m3u8")*/mediauri)
            .build();
    HlsMediaSource hlsMediaSource = new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(mediaItem);


    player = new ExoPlayer.Builder(CustomVideo.this).setTrackSelector(trackSelector).build();
    player.addListener(new Player.Listener() {
        @Override
        public void onEvents(Player player, Player.Events events) {

        }

        @Override
        public void onIsPlayingChanged(boolean isPlaying) {
            Log.e(TAG, "onIsPlayingChanged: " + isPlaying);
        }

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            Log.e(TAG, "onTimelineChanged: " + timeline.getPeriodCount());
        }
    });

    trackSelectionParameters =
            new DefaultTrackSelector.ParametersBuilder(/* context= */ this).build();

    player.setTrackSelectionParameters(trackSelectionParameters);
    player.setMediaItems(Collections.singletonList(mediaItem), /* resetPosition= */false);
    player.prepare();
    playerView.setPlayer(player);
    try {
        if (resumePosition != -1) {
            player.seekTo(resumePosition);
        }

        player.prepare(hlsMediaSource, false, true);
        player.setPlayWhenReady(true);
     
}

If need any more information please let me know.

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source