问题
I am creating a game with LibGDX and wanted to put rewarded videos on it but I can't figure out how I do that.
I have already displayed an admob banner with this tutorial: https://www.youtube.com/watch?v=cwAN4LMXo58
I have already seen this question but don't understand where to put this code:
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.
}
(first of all there is no mAd declared in it): rewarded video ad example in libGDX
And using a few more sorces I've made a few experiments being the last one:
AdHandler.java:
public interface AdHandler {
public void showVideo();
}
AndroidLauncher:
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());
}
}
Then I have this code on the android class:
AdHandler adHandler;
public Restart(AdHandler handler) {
this.adHandler = handler;
}
And finnally I just call
adHandler.showVideo();
on the render() what gives the error
java.lang.IllegalStateException: isLoaded must be called on the main UI thread.
(that I really don't know what means)
If anyone could give me a full working project with LibGDX + AdMob Rewarded Videos would be awesome.
[EDIT]
So after a few tests I've been able to make it work, I only had to change
@Override
public void showVideo() {
if (rewardedVideoAd.isLoaded()) {
rewardedVideoAd.show();
}
}
to
@Override
public void showVideo() {
runOnUiThread(new Runnable() {
@Override public void run() {
if (rewardedVideoAd.isLoaded()) {
rewardedVideoAd.show();
}
}
});
}
and that solved the error I've said earlier.
So now the code is like this:
AdHandler.java: unchanged
Andoid Launcher:
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) {
}
});
}
}
Andoid Class:
public class RewVideo extends ApplicationAdapter {
AdHandler adHandler;
public Restart(AdHandler handler) {
this.adHandler = handler;
}
@Override
public void create () {
}
@Override
public void render () {
if (Gdx.input.justTouched()) {
adHandler.showVideo();
}
}
However, it fills my log with strange messages and exceptions:
W/Ads: Invoke Firebase method getInstance error. java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.measurement.AppMeasurement" on path:
The Google Mobile Ads SDK will not integrate with Firebase. Admob/Firbase integration requires the latest Firebase SDK jar, but Firebase SDK is either missing or out of date
W/Ads: Server parameters: {...}
W/DynamiteModule: Local module descriptor class for com.google.android.gms.ads.dynamite not found.
W/OkHttpClient: A connection to https://googleads.g.doubleclick.net/ was leaked. Did you forget to close a response body?
W/MessageQueue: Handler (android.os.Handler) {c84505c} sending message to a Handler on a dead thread java.lang.IllegalStateException: Handler (android.os.Handler) {c84505c} sending message to a Handler on a dead thread
W/ExoPlayerImplInternal: Sent message(1) after release. Message ignored.
E/ACodec: [OMX.google.h264.decoder] setPortMode on output to DynamicANWBuffer failed w/ err -1010
And while video is playing this message appears like once a second:
W/zygote: Attempt to remove non-JNI local reference, dumping thread
And more than being tons of messages, I've also noticed that there are two messages for each of these: "Ad finished loading", "Ad opening" and "Ad closed"
Is this normal?
来源:https://stackoverflow.com/questions/51517317/implementing-admob-rewarded-video-with-libgdx