15

出于演示目的,该应用程序有一个简单地提供此功能的活动: 在此处输入图像描述

你点击一个按钮,观看一个奖励视频,你就会得到任何奖励。

问题

如何加载视频?从我所看到的你只能打电话mAd.loadAd()一次。有 3 个视频,每个视频都有自己的广告单元 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()那么我如何跟踪要给予什么奖励?

4

3 回答 3

11

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

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

常量.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 中奖励视频的一个广告单元...删除其余的。不需要其他单元,您可以在侦听器中跟踪您喜欢的任何内容。

于 2016-10-21T02:09:50.543 回答
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() {

}
于 2019-10-14T07:02:47.293 回答
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);
    }

}

奖励1.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) {}
    }

}

奖励2.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();

    }

}
于 2017-07-03T01:02:06.613 回答