简体   繁体   中英

Rewarded Videos doesn't load the first time but loads after

My App has a button to open reward videos >> when this button is pressed for the first time, it toasts "Check your Internet connection", when hit the second or third time, it shows the video with no problems

    MobileAds.initialize(getActivity(), getString(R.string.VID_App_ID));
    mRewardVideoAd = MobileAds.getRewardedVideoAdInstance(getActivity());
    mRewardVideoAd.loadAd(getString(R.string.VID_App_Unit_ID), new AdRequest.Builder()
            .addTestDevice(getString(R.string.TestDeviceID))
            .build());
    mRewardVideoAd.setRewardedVideoAdListener(this);
    loadRewardedVideoAd();   

Here are the methods used :

 private void loadRewardedVideoAd() {
        mRewardVideoAd.loadAd(getString(R.string.VID_App_Unit_ID), new AdRequest.Builder()
                .addTestDevice(getString(R.string.TestDeviceID))
                .build());
    }



   @OnClick(R.id.button_more_money)
    public void more_money() {
        if (mRewardVideoAd.isLoaded()) {
            mRewardVideoAd.show();
        } else
            { 
             Toast.makeText(getActivity(), "Check your internet connection", Toast.LENGTH_LONG).show();
            loadRewardedVideoAd();
               }

    }


    @Override
    public void onResume() {
        mRewardVideoAd.resume(getActivity());
        super.onResume();
        loadRewardedVideoAd();
    }

Edit: Solved - It took some time and the solution was to load at onCreate() Thanks to Martin De Simone and Avi Levin

Rewarded videos take time to load, your code is fine, the first time you are pressing, the video is loading and then when you press probably the video has already loaded.

Try to toast something in the onAdLoaded to check this

As Martin said, RV ad takes a time to load. basically, it needs to download ~30-second video which takes sometimes few second.

I recommend using the SDK provided RewardedVideoAdListener interface which will help you know when the ad is ready to show. furthermore, it will help you to understand the AdMob rewarded video ad's life cycle.

In order to use it you need to do the following phases:

  1. Implement RewardedVideoAdListener listener in your java class
  2. Override the following methods (I used Toast UI messages for visualization, it can be deleted)

Code:

@Override
public void onRewarded(RewardItem reward) {
    Toast.makeText(this, "onRewarded! currency: " + reward.getType() + "  amount: " +
        reward.getAmount(), Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLeftApplication() {
    Toast.makeText(this, "onRewardedVideoAdLeftApplication",
            Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdClosed() {
    Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
    Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdLoaded() {
    Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoAdOpened() {
    Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}

@Override
public void onRewardedVideoStarted() {
    Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}

BTW: before a show call, I recommend using isLoaded() method in order to if the AdMob is ready to show something, for example:

if (mAd.isLoaded()) {
    mAd.show();
}

More information can be found inside Google AdMob doc's

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM