加载多个AdMob视频

时间:2016-10-18 02:47:45

标签: java android admob rewardedvideoad

出于演示目的,该应用程序只有一项活动可以提供: enter image description here

您点击一个按钮,查看奖励视频,您将获得任何奖励。

问题

如何加载视频?根据我所看到的情况,您只能拨打mAd.loadAd()一次。共有3个视频,每个视频都有自己的AD UNIT ID。每个广告单元都可以拥有自己的监听器,但只有一个视频加载,所以它并不重要...

尝试加载多个视频时

例如:

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());

仅导致最后一个视频被加载,这在日志中显示:

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
}

感谢您的帮助

编辑:很明显我在想这个问题是错的。我有5个以上的广告区域,每个区域都会播放奖励视频并给予不同的奖励(例如,一个给出硬币,一个给出一个等级,等等......)。没有理由加载5个视频。我应该在onCreate()中加载一个,以便在需要时准备好,然后在项目获得奖励后再次加载,以便下次准备好。

所以问题仍然存在,如果只有一个视频,一个广告区域被加载onCreate(),那么我该如何追踪要给予的奖励?

3 个答案:

答案 0 :(得分:10)

这是一个简单的解决方案......

<强> 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 = "";
}

点击按钮后显示广告

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

REWARDED_VIDEO_UNIT_ID是AdMob中获奖视频的一个广告单元...删除其余部分。不需要其他单位,你可以在听众中追踪你喜欢的任何内容。

答案 1 :(得分:0)

其他简单的解决方案......

<强> AbstractRewardVideo.java

public abstract class AbstractRewardVideo {
    private RewardedVideoAd mAd;
    private String adId = "ca-app-pub...";
    private Activity activity;

    abstract protected RewardedVideoAdListener getListener();

    public void init(Activity activity) {
        this.activity = activity;
        mAd = MobileAds.getRewardedVideoAdInstance(activity);
        setAdId(adId);
        loadRewardedVideoAd();
    }

    public Activity getActivity(){
        return this.activity;
    }

    public void loadRewardedVideoAd() {
        mAd.loadAd(adId, new AdRequest.Builder().build());
    }

    public void showVideo(){
        setListener(getListener());
        if (mAd.isLoaded()) {
            mAd.show();
        } else {
            Utils.exibirToast("Don't loaded!");
        }
    }

    public void setAdId(@NonNull String id){
        this.adId = id;
    }

    public void setListener(RewardedVideoAdListener listener){
        mAd.setRewardedVideoAdListener(listener);
    }

}

<强> Reward1.java

public class Reward1 extends AbstractRewardVideo {

    public Reward1(Activity activity) {
        init(activity);
    }

    @Override
    protected RewardedVideoAdListener getListener() {
        return new Listener();
    }

    private class Listener implements RewardedVideoAdListener {
        @Override
        public void onRewarded(RewardItem rewardItem) {
            //Do something...
        }
        public void onRewardedVideoAdLoaded() {}
        public void onRewardedVideoAdOpened() {}
        public void onRewardedVideoStarted() {}
        public void onRewardedVideoAdClosed() { loadRewardedVideoAd(); }
        public void onRewardedVideoAdLeftApplication() {}
        public void onRewardedVideoAdFailedToLoad(int i) {}
    }

}

<强> Reward2.java

public class Reward2 extends AbstractRewardVideo {

    public Reward2(Activity activity) {
        init(activity);
    }

    @Override
    protected RewardedVideoAdListener getListener() {
        return new Listener();
    }

    private class Listener implements RewardedVideoAdListener {
        @Override
        public void onRewarded(RewardItem rewardItem) {
            //Do something...
        }
        public void onRewardedVideoAdLoaded() {}
        public void onRewardedVideoAdOpened() {}
        public void onRewardedVideoStarted() {}
        public void onRewardedVideoAdClosed() { loadRewardedVideoAd(); }
        public void onRewardedVideoAdLeftApplication() {}
        public void onRewardedVideoAdFailedToLoad(int i) {}
    }

}

<强> MainActivity.java

public class MainActivity extends AppCompatActivity{
    Reward1 reward1;
    Reward2 reward2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        reward1 = new Reward1(this);
        reward2 = new Reward1(this);
        ...
        reward1.showVideo();
        ...
        reward2.showVideo();

    }

}

答案 2 :(得分:0)

    MobileAds.initialize ( this, "ca-app-pub-4761500786576152~8215465788" );

    RewardedVideoAd mAd = MobileAds.getRewardedVideoAdInstance(this);
    mAd.setRewardedVideoAdListener(Video_Ad.this);

}

@Override
public void onRewardedVideoAdLoaded() {

}

@Override
public void onRewardedVideoAdOpened() {

}

@Override
public void onRewardedVideoStarted() {

}

@Override
public void onRewardedVideoAdClosed() {

}

@Override
public void onRewarded(RewardItem rewardItem) {

}

@Override
public void onRewardedVideoAdLeftApplication() {

}

@Override
public void onRewardedVideoAdFailedToLoad(int i) {

}

@Override
public void onRewardedVideoCompleted() {

}