How to integrate Rewarded Video Ad in LibGDX

Check out Lines Galaxy Android game to view the results in action.

What is Rewarded Video Ad?

Rewarding users with different in-app coins or features can be done using Rewarded Video Ad.

This might be an advertising solution that could be accepted by users whilst supporting the development of a free mobile application.

Most games tend to place the user in a situation where he is stuck or he needs some coins in order to unlock a certain feature.

The user is then given the possibility to tap a button or an element which will open a full screen video.

At the end of this video called Rewarded Video Ad the player will receive his prize.

Let’s get started with Rewarded Video Ad!

Integrating Rewarded Video Ads in LibGDX is quite easy.

You’ll need to import a few packages (AndroidLauncher.java):

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.reward.RewardItem;
import com.google.android.gms.ads.reward.RewardedVideoAd;
import com.google.android.gms.ads.reward.RewardedVideoAdListener;

public class AndroidLauncher extends AndroidApplication implements GoogleServices, RewardedVideoAdListener {
    private RewardedVideoAd adRewardedVideoView;
    private static final String REWARDED_VIDEO_AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917";
    private VideoEventListener vel;
}

Our Android Launcher has one member which will store the RewardedVideoAd object.

Initialing the Rewarded Video Ad requires an ad unit. The one in this example contains a test value provided by AdMob.

While developing the application this is the unit id that you should use.

As soon as you’re ready to deploy into production you can switch to your real unit id obtained from AdMob website.

The vel member will be used for sharing Rewarded Video Ads events in other classes.

Next thing we need is to initiate the Rewarded Video Ad, to set the event listener and to start loading the first video so it will be ready when we need it (AndroidLauncher.java):

public void loadRewardedVideoAd() {
    adRewardedVideoView.loadAd(REWARDED_VIDEO_AD_UNIT_ID, new AdRequest.Builder().build());
}

public void setupRewarded() {
    adRewardedVideoView = MobileAds.getRewardedVideoAdInstance(this);
    adRewardedVideoView.setRewardedVideoAdListener(this);
    loadRewardedVideoAd();
}

Above code does not need any extra explications. Method names have been carefully chosen so they’re quite self explanatory.
While using Rewarded Video Ads we’ll need a way to know if the video is loaded and if it’s not loaded we need to load it.
In our Android Launcher class we’ll add a new private boolean is_video_ad_loaded (AndroidLauncher.java).

public boolean hasVideoLoaded(){
    if(is_video_ad_loaded) {
        return true;
    }
    runOnUiThread(new Runnable() {
        public void run() {
            if (!adRewardedVideoView.isLoaded()) {
                loadRewardedVideoAd();
            }
        }
    });
    return false;
}

Following method will show the add if it’s loaded or load an new ad (AndroidLauncher.java).

public void showRewardedVideoAd(){
    runOnUiThread(new Runnable() {
        public void run() {
            if (adRewardedVideoView.isLoaded()) {
                adRewardedVideoView.show();
            } else {
                loadRewardedVideoAd();
            }
        }
    });
}

The Android Launcher is the class that initiates your application and you should only have the code for this purpose here.

You’ll want to interact with the Rewarded Video Ad instance and one way of doing this is to implement some interfaces.

In my code there’s GoogleServices interface which looks like this (GoogleServices.java):

public interface GoogleServices {
    public boolean hasVideoLoaded();
    public void loadRewardedVideoAd();
    public void showRewardedVideoAd();
    public void setVideoEventListener(VideoEventListener listener);
}

Again not many details are required here. The VideoEventListener is another interface which only handles the Rewarded Video Ad events (VideoEventListener.java):

public interface VideoEventListener {
    void onRewardedEvent(String type, int amount);
    void onRewardedVideoAdLoadedEvent();
    void onRewardedVideoAdClosedEvent();
}

I only needed handlers for these events in my other classes but you can customize it for your needs.

We’ll need some handlers for the above events: (Android Launcher.java):

@Override
public void onRewarded(RewardItem reward) {
    if(vel != null) {
        // The type and the amount can be set in your AdMob console
        vel.onRewardedEvent(reward.getType(), reward.getAmount());
    }
}

// Each time the video ends we need to load a new one
@Override
public void onRewardedVideoAdClosed() {
    is_video_ad_loaded = false;
    loadRewardedVideoAd();
    if(vel != null) {
        vel.onRewardedVideoAdClosedEvent();
    }
}

@Override
public void onRewardedVideoAdLoaded() {
    if(vel != null) {
        vel.onRewardedVideoAdLoadedEvent();
    }
    is_video_ad_loaded = true;
}

Once this is added you can now attach the video event listener (AndroidLauncher.java):

public void setVideoEventListener (VideoEventListener listener) {
    this.vel = listener;
}

This is all the needed setup.
Last thing we need to do is initiate the Rewarded Video Ad in our onCreate method (AndroidLauncher.java):

@Override
protected void onCreate (Bundle savedInstanceState) {
    ...
    View gameView = initializeForView(new App(this), config);
    setupRewarded();
    ...
}

That’s all! The integration is now finished!

You can now use the Rewarded Video Ad in your applications classes (App.java):

public class App extends Game implements VideoEventListener {

    public GoogleServices googleServices;

    public App(GoogleServices googleServices) {

        this.googleServices = googleServices;
        this.googleServices.setVideoEventListener(this);

        // this probably should not be here but it's how you show the video
        if(this.googleServices.hasVideoLoaded()) {
            this.googleServices.showRewardedVideoAd();
        }

   }

    @Override
    public void onRewardedEvent(String type, int amount) {
        // player has just finished the video and was rewarded
    }

    @Override
    public void onRewardedVideoAdLoadedEvent() {
        // video is ready and can be presented to the player
    }

    @Override
    public void onRewardedVideoAdClosedEvent() {
        // player has closed the video so no reward for him
    }

}

Checkout my other LibGDX posts or Lines Galaxy Android game to view the results in action.

9 thoughts on “How to integrate Rewarded Video Ad in LibGDX

  1. AvatarJohn

    it doesn’t work for me !
    Attempt to invoke interface method ‘boolean Intreface.GoogleServices.hasVideoLoaded()’ on a null object reference

Leave a Reply