androidx / media

Jetpack Media3 support libraries for media use cases, including ExoPlayer, an extensible media player for Android
Apache License 2.0
1.34k stars 315 forks source link

How to use IMA extension with Leanback on Android TV and Media3 ExoPlayer? #1481

Open jstewart007 opened 1 week ago

jstewart007 commented 1 week ago

I am migrating a previously created Android TV app using MediaPlayerAdapter to use ExoPlayer and LeanbackPlayerAdapter. My goal is to play vast ads from our adserver as preroll ads prior to playing shows in our app. I have the playback of the Movie working fine but I cannot figure out how to properly configure using a PlayerView in a VideoSupportFragment. I have tried unsuccessfully to add it as a ViewGroup but I could not find any full demos of how to properly do this. I have no layouts for PlaybackActivity, so I have no clue where to add my PlayerView. I have a details activity with a play button that launches a FragmentActivity PlaybackActivity, this replaces the content of the page with a PlaybackVideoFragment extending VideoSupportFragment, plays movie fine but logcat says Playing media without ads. Configure ad support by calling setAdsLoaderProvider and setAdViewProvider.

import android.content.SharedPreferences; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager;

import androidx.annotation.OptIn; import androidx.leanback.app.VideoFragment; import androidx.leanback.app.VideoFragmentGlueHost; import androidx.leanback.app.VideoSupportFragment; import androidx.leanback.app.VideoSupportFragmentGlueHost; import androidx.leanback.media.MediaPlayerAdapter; import androidx.leanback.media.PlaybackTransportControlGlue; import androidx.leanback.widget.PlaybackControlsRow;

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL;

import androidx.media3.common.AdViewProvider; import androidx.media3.common.MediaItem; import androidx.media3.common.Player; import androidx.media3.common.util.UnstableApi; import androidx.media3.datasource.DataSource; import androidx.media3.datasource.DefaultDataSource; import androidx.media3.exoplayer.ExoPlayer; import androidx.media3.exoplayer.ExoPlayer; import androidx.media3.exoplayer.ima.ImaAdsLoader; import androidx.media3.exoplayer.source.DefaultMediaSourceFactory; import androidx.media3.exoplayer.source.MediaSource; import androidx.media3.exoplayer.source.MediaSourceFactory; import androidx.media3.ui.PlayerView; import androidx.media3.ui.leanback.LeanbackPlayerAdapter; import androidx.media3.exoplayer.source.ads.AdsLoader.Provider;

import com.google.ads.interactivemedia.v3.api.AdEvent;

/**

}

tonihei commented 4 days ago

Have you seen https://github.com/google/ExoPlayer/issues/10736? It sounds like this is basically the same question and the solution was to implement AdViewProvider on the VideoSupportFragment.

jstewart007 commented 4 days ago

Yes, that is the only string I can find that is close to what I need. They list some code in there but it is not enough. I tried so hard to make this happen using a viewgroup but without seeing more of their implementation I can't make it work. I was hoping someone would show more of their code they used to implement the following:

public class PlayerFragment extends VideoSupportFragment implements AdViewProvider{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { mPlayerView = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); }

@Override
public ViewGroup getAdViewGroup() {
    return mPlayerView;
}

}

tonihei commented 4 days ago

I can't make it work

What are the symptoms of "can't make it work" exactly? Is it not compiling, are there any runtime errors, is the ad playing but you don't see the overlay, is the ad skipped entirely, ... ? As far the ad extension is concerned it really just needs a view group to display its overlay in. Other than that ad playback should just work.