使用LibGDX实施AdMob奖励视频

时间:2018-07-25 11:03:36

标签: android libgdx admob admob-rewardedvideoad

我正在用LibGDX创建游戏,想在上面放一些奖励视频,但我不知道该怎么做。

我已经在本教程中显示了一条admob横幅:https://www.youtube.com/watch?v=cwAN4LMXo58

我已经看过这个问题,但是不知道将这段代码放在哪里:

GA

(首先没有声明mAd):rewarded video ad example in libGDX

使用更多的源,我做了最后一个实验:

AdHandler.java:

public void showVideoAd(){
        runOnUiThread(new Runnable() {
            public void run() {

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

public boolean hasVideoReward(){

    return isRewardLoaded; 
    //return mAd.isLoaded();    // -> must be called on the main UI thread.
}

AndroidLauncher:

public interface AdHandler {
    public void showVideo();
}

然后我在android类上有以下代码:

public class AndroidLauncher extends AndroidApplication implements AdHandler {
    RewardedVideoAd rewardedVideoAd;

    @Override
    public void showVideo() {
        if (rewardedVideoAd.isLoaded()) {
            rewardedVideoAd.show();
        }
    }

        Handler handler = new Handler();
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
            initialize(new Restart(this), config);

            rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
            rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
            @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) {

            }
        });

        loadRewardedVideoAd();
    }

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

最后我打电话给

AdHandler adHandler;

    public Restart(AdHandler handler) {
         this.adHandler = handler;
    }

render()会导致错误

  

java.lang.IllegalStateException:isLoaded必须在主调用   用户界面线程。

(我真的不知道是什么意思)

如果有人可以使用LibGDX + AdMob奖励视频给我一个完整的项目,那就太棒了。

[编辑]

因此,在进行了几次测试之后,我才能够使它工作,我只需要进行更改

    adHandler.showVideo();

    @Override
    public void showVideo() {
        if (rewardedVideoAd.isLoaded()) {
            rewardedVideoAd.show();
        }
    }

这解决了我之前说的错误。

所以现在的代码是这样的:

AdHandler.java:不变

Andoid启动器:

@Override
public void showVideo() {
    runOnUiThread(new Runnable() {
        @Override public void run() {
            if (rewardedVideoAd.isLoaded()) {
                rewardedVideoAd.show();
            }
        }
    });
}

Andoid类:

public class AndroidLauncher extends AndroidApplication implements AdHandler {
    RewardedVideoAd rewardedVideoAd;

    @Override
    public void showVideo() {
        runOnUiThread(new Runnable() {
            @Override public void run() {
                if (rewardedVideoAd.isLoaded()) {
                    rewardedVideoAd.show();
                }
            }
        });
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new Restart(this), config);

        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");

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

            }

            @Override
            public void onRewardedVideoAdOpened() {

            }

            @Override
            public void onRewardedVideoStarted() {

            }

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

            @Override
            public void onRewarded(RewardItem rewardItem) {

            }

            @Override
            public void onRewardedVideoAdLeftApplication() {

            }

            @Override
            public void onRewardedVideoAdFailedToLoad(int i) {

            }
        });
    }
}

但是,它用奇怪的消息和异常填充了我的日志:

  

广告:调用Firebase方法getInstance错误。          java.lang.ClassNotFoundException:在路径:

上找不到类“ com.google.android.gms.measurement.AppMeasurement”      

Google移动广告SDK将不会与Firebase集成。   Admob / Firbase集成需要最新的Firebase SDK jar,但是   Firebase SDK丢失或已过期

     

广告:服务器参数:{...}

     

W / DynamiteModule:找不到com.google.android.gms.ads.dynamite的本地模块描述符类。

     

W / OkHttpClient:与https://googleads.g.doubleclick.net/的连接已泄漏。您是否忘了关闭响应主体?

     

W / MessageQueue:处理程序(android.os.Handler){c84505c}在死线程上向处理程序发送消息                   java.lang.IllegalStateException:处理程序(android.os.Handler){c84505c}在死线程上向处理程序发送消息

     

W / ExoPlayerImplInternal:发布后发送消息(1)。消息被忽略。

     

E / ACodec:[OMX.google.h264.decoder]输出到DynamicANWBuffer的setPortMode失败,错误-1010

在播放视频时,此消息每秒出现一次:

  

W / zygote:尝试删除非JNI本地引用,转储线程

除了收到大量邮件之外,我还注意到,每个邮件都有两条消息:“广告已完成加载”,“广告打开”和“广告已关闭”

这正常吗?

0 个答案:

没有答案