Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading multiple AdMob videos

For demonstration purposes, the app has one activity that simply offers this: enter image description here

You click a button, view a rewarded video, and you are rewarded with whatever.

The Problem

How can I load the videos? From what I have seen you can only call mAd.loadAd() once. There are 3 videos, each with their own AD UNIT ID. Each ad unit can have its own listener, but only one video loads so it doesn't matter...

When trying to load multiple videos

For example:

mAd1.loadAd("AD_UNIT_1", new AdRequest.Builder().build());
mAd2.loadAd("AD_UNIT_2", new AdRequest.Builder().build());
mAd3.loadAd("AD_UNIT_3", new AdRequest.Builder().build());

results in only the last video being loaded and this in log:

W/Ads: Loading already in progress, saving this object for future refreshes.

onCreate()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAd1 = MobileAds.getRewardedVideoAdInstance(this);
    mAd2 = MobileAds.getRewardedVideoAdInstance(this);
    mAd3 = MobileAds.getRewardedVideoAdInstance(this);

    listeners...

    mAd1.loadAd() etc
}

Thank you for your help

Edit: It's clear I am thinking about this problem wrong. I have 5+ ad zones that each will play a rewarded video and give a different reward (for example, one gives coins, one gives a level up, and so on..). There is no reason to load 5 videos. I should load one in onCreate(), so it's ready when needed, then load it again after the item is rewarded so it's ready for next time.

So the question remains, if there is just the one video, and thus one ad zone, being loaded onCreate() then how can I track what reward to give?

like image 413
zngb Avatar asked Oct 18 '16 02:10

zngb


People also ask

Does AdMob have video ads?

AdMob Mediation enables you to mediate ads from third-party ad networks, including those that serve in-app video ads.

Is there a limit for rewarded video ads?

So there is no direct cap on number of times rewarded video should be displayed but You should place no more than one interstitial ad after every two user actions within your app.


1 Answers

Here's a simple solution...

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAd = MobileAds.getRewardedVideoAdInstance(this);

    mAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {

        @Override
        public void onRewarded(RewardItem rewardItem) {
            switch(Constants.currentAd) {
                case("REWARD1"):
                    //do something
                    Constants.currentAd = "";
                    break;

                case("REWARD2"):
                    //do something
                    Constants.currentAd = "";
                    break;

                case("REWARD3"):
                    //do something
                    Constants.currentAd = "";
                    break;
            }
        }
    });

   mAd.loadAd("REWARDED_VIDEO_UNIT_ID", new AdRequest.Builder().build());
}

public void showRewardedVideo() {
        if (mAd.isLoaded()) {
            mAd.show();
        }
    }

Constants.java

public class Constants {
    public static String currentAd = "";
}

Showing the ad after button click

rewardButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Constants.currentAd = "REWARD1";
                dismiss();
                ((MainActivity) getActivity()).showRewardedVideo();
            }
        });

REWARDED_VIDEO_UNIT_ID is one ad unit for rewarded video in AdMob...remove the rest. No need for other units, you can track whatever you like in the listener.

like image 52
James Avatar answered Sep 22 '22 10:09

James