Admob奖励视频广告

时间:2017-12-09 17:50:45

标签: android admob

这是我的奖励视频代码。我想要做的是一个按钮,每次点击加载一个奖励视频。我面临的问题是,首先奖励视频加载需要花费很多时间来加载,其次奖励视频有时只需要一次,所以什么是在每次点击时立即开始奖励视频的正确方法?

rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(MainActivity.this);
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
        rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
            @Override
            public void onRewarded(RewardItem reward) {

            }

            @Override
            public void onRewardedVideoAdLeftApplication() {}

            @Override
            public void onRewardedVideoAdClosed() {}

            @Override
            public void onRewardedVideoAdFailedToLoad(int errorCode) {
                rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
            }

            @Override
            public void onRewardedVideoAdLoaded() {}

    @Override
    public void onRewardedVideoAdOpened() {}

    @Override
    public void onRewardedVideoStarted() {
        rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
    }
});

这是我点击代码上的按钮

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

1 个答案:

答案 0 :(得分:0)

我可以使用以下代码显示视频奖励广告(仅测试广告)。

public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;

private InterstitialAd mInterstitialAd;

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

    MobileAds.initialize(this, getResources().getString(R.string.app_id));

    // Use an activity context to get the rewarded video instance.
    mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            new AdRequest.Builder().build());
    //getResources().getString(R.string.Video_reward_ad_unit_id)
    mRewardedVideoAd.setRewardedVideoAdListener(this);

    findViewById(R.id.btVideo).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (mRewardedVideoAd.isLoaded()) {
                mRewardedVideoAd.show();
            }
        }
    });

    mInterstitialAd = new InterstitialAd(MainActivity.this);
    mInterstitialAd.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));

    mInterstitialAd.loadAd(new AdRequest.Builder().build());


    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            // Load the next interstitial.
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
            mInterstitialAd.show();
        }

    });

    findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {

                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
            mInterstitialAd.loadAd(new AdRequest.Builder().build());
        }
    });

}

@Override
protected void onStart() {
    loadTestRewardedVideoAd();
    MobileAds.setAppMuted(true);
    super.onStart();
}


private void loadTestRewardedVideoAd() {
    mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
            new AdRequest.Builder().build());
}

@Override
public void onRewardedVideoAdLoaded() {
    Log.i("onRewardedVideoAdLoaded", "Ad Loaded");
}

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

@Override
public void onRewardedVideoStarted() {

}

@Override
public void onRewardedVideoAdClosed() {

}

@Override
public void onRewarded(RewardItem reward) {

}

@Override
public void onRewardedVideoAdLeftApplication() {
    Log.i("onRewardedVideoAdLeft", "END");

}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {
    Log.i("onRewardedVideoAdLeft", "END");
}

但这仅适用于不在真实设备上的测试设备。