Reputation: 5469
I am using ExoPlayer
2.7.1. I have to show ads at certain intervals. These intervals are in an array of longs as milliseconds. How do I show ad markers in the ExoPlayer controller?
Upvotes: 0
Views: 2369
Reputation: 2789
To integrate your own ads solution, I'd in general recommend to look into the AdsMediaSource and the AdsLoader of the ExoPlayer library which provide ways to integrate your ads nicely into a content media source. This way the player is aware of ads playback and makes sure ads can't be skipped, markers are shown and the like.
The AdsMediaSource is used to implement the IMA extension of ExoPlayer but you can take advantage of these for your solution.
To answer your specific question about the ad markers:
The PlayerControlView
has a method which lets you set ad marker positions and booleans to indicate whether the ads has been shown or not:
playerControlView.setExtraAdGroupMarkers(
new long[]{10 * 1000, 120 * 1000},
new boolean[]{false, false});
However, if you are using the PlayerView out-of-the-box, it is a little bit difficult to get access to the PlaybackControlView which is inserted by the player view programmatically. Hence you can't use findById and the PlayerView does not provide a getter for the controller I'm afraid.
So if you are using the PlayerView the only way I see to that is to provide your own layout file exo_player_view.xml in which you set a custom controller:
Create a file res/layout/exo_player_view.xml in your project and copy the layout file from here into it.
In this layout file search for a view element with id exo_controller_placeholder. Replace it with this element:
<com.google.android.exoplayer2.ui.PlayerControlView android:id="@id/exo_controller"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
Lookup the player control view in onCreate:
playerControlView = findViewById(R.id.exo_controller);
Whenever you want to update the markers you call:
playerControlView.setExtraAdGroupMarkers(
new long[]{10 * 1000, 120 * 1000},
new boolean[]{true, false});
The second parameter let's you mark ads as shown.
Upvotes: 5