好吧,使用Firebase奖励视频广告,我在logcat中遇到一些错误,并且未显示测试视频。
我也关注了他们的github examples和文档。这分别是我的示例活动代码和错误。
public class HomeActivity extends AppCompatActivity
implements RewardedVideoAdListener {
private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917";
private static final String APP_ID = "ca-app-pub-3940256099942544~3347511713";
private static final long COUNTER_TIME = 10;
private static final int GAME_OVER_REWARD = 1;
private int coinCount;
private TextView coinCountText;
private CountDownTimer countDownTimer;
private boolean gameOver;
private boolean gamePaused;
private RewardedVideoAd rewardedVideoAd;
private Button retryButton;
private Button showVideoButton;
private long timeRemaining;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_home);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, APP_ID);
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
rewardedVideoAd.setRewardedVideoAdListener(this);
loadRewardedVideoAd();
// Create the "retry" button, which tries to show an interstitial between game plays.
retryButton = findViewById(R.id.retry_button);
retryButton.setVisibility(View.INVISIBLE);
retryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startGame();
}
});
// Create the "show" button, which shows a rewarded video if one is loaded.
showVideoButton = findViewById(R.id.show_video_button);
showVideoButton.setVisibility(View.INVISIBLE);
showVideoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showRewardedVideo();
}
});
// Display current coin count to user.
coinCountText = findViewById(R.id.coin_count_text);
coinCount = 0;
coinCountText.setText("Coins: " + coinCount);
startGame();
}
@Override
public void onPause() {
super.onPause();
pauseGame();
rewardedVideoAd.pause(this);
}
@Override
public void onResume() {
super.onResume();
if (!gameOver && gamePaused) {
resumeGame();
}
rewardedVideoAd.resume(this);
}
private void pauseGame() {
countDownTimer.cancel();
gamePaused = true;
}
private void resumeGame() {
createTimer(timeRemaining);
gamePaused = false;
}
private void loadRewardedVideoAd() {
if (!rewardedVideoAd.isLoaded()) {
rewardedVideoAd.loadAd(AD_UNIT_ID, new AdRequest.Builder().
addTestDevice("7E225609CB8AD8CDA8FF496953D7DAB6").build());
}
}
private void addCoins(int coins) {
coinCount += coins;
coinCountText.setText("Coins: " + coinCount);
}
private void startGame() {
// Hide the retry button, load the ad, and start the timer.
retryButton.setVisibility(View.INVISIBLE);
showVideoButton.setVisibility(View.INVISIBLE);
loadRewardedVideoAd();
createTimer(COUNTER_TIME);
gamePaused = false;
gameOver = false;
}
// Create the game timer, which counts down to the end of the level
// and shows the "retry" button.
private void createTimer(long time) {
final TextView textView = findViewById(R.id.timer);
if (countDownTimer != null) {
countDownTimer.cancel();
}
countDownTimer = new CountDownTimer(time * 1000, 50) {
@Override
public void onTick(long millisUnitFinished) {
timeRemaining = ((millisUnitFinished / 1000) + 1);
textView.setText("seconds remaining: " + timeRemaining);
}
@Override
public void onFinish() {
if (rewardedVideoAd.isLoaded()) {
showVideoButton.setVisibility(View.VISIBLE);
}
textView.setText("You Lose!");
addCoins(GAME_OVER_REWARD);
retryButton.setVisibility(View.VISIBLE);
gameOver = true;
}
};
countDownTimer.start();
}
private void showRewardedVideo() {
showVideoButton.setVisibility(View.INVISIBLE);
if (rewardedVideoAd.isLoaded()) {
rewardedVideoAd.show();
}
}
@Override
public void onRewardedVideoAdLeftApplication() {
Toast.makeText(this, "onRewardedVideoAdLeftApplication", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdClosed() {
Toast.makeText(this, "onRewardedVideoAdClosed", Toast.LENGTH_SHORT).show();
// Preload the next video ad.
loadRewardedVideoAd();
}
@Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
Toast.makeText(this, "onRewardedVideoAdFailedToLoad", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdLoaded() {
Toast.makeText(this, "onRewardedVideoAdLoaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "onRewardedVideoAdOpened", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewarded(RewardItem reward) {
Toast.makeText(this,
String.format(" onRewarded! currency: %s amount: %d", reward.getType(),
reward.getAmount()),
Toast.LENGTH_SHORT).show();
addCoins(reward.getAmount());
}
@Override
public void onRewardedVideoStarted() {
Toast.makeText(this, "onRewardedVideoStarted", Toast.LENGTH_SHORT).show();
}
@Override
public void onRewardedVideoCompleted() {
Toast.makeText(this, "onRewardedVideoCompleted", Toast.LENGTH_SHORT).show();
}
Logcat中显示错误。
2018-10-08 18:52:27.109 5883-6197/shihab.com.rewardvideoad W/Ads: Fail to instantiate adapter com.google.DummyAdapter
android.os.RemoteException
at com.google.android.gms.internal.ads.zzxm.zzbp(Unknown Source:131)
at com.google.android.gms.internal.ads.zzxm.zzbo(Unknown Source:121)
at com.google.android.gms.internal.ads.zzxm.zzbm(Unknown Source:0)
at com.google.android.gms.internal.ads.zzxo.dispatchTransaction(Unknown Source:33)
at com.google.android.gms.internal.ads.zzek.onTransact(Unknown Source:22)
at android.os.Binder.transact(Binder.java:627)
at oa.a(:com.google.android.gms.dynamite_adsdynamite@14366051@14.3.66 (040408-213742215):8)
at com.google.android.gms.ads.internal.mediation.client.d.a(:com.google.android.gms.dynamite_adsdynamite@14366051@14.3.66 (040408-213742215):4)
at com.google.android.gms.ads.internal.reward.a.a(:com.google.android.gms.dynamite_adsdynamite@14366051@14.3.66 (040408-213742215):13)
at com.google.android.gms.ads.internal.reward.mediation.j.d_(:com.google.android.gms.dynamite_adsdynamite@14366051@14.3.66 (040408-213742215):20)
at com.google.android.gms.ads.internal.util.b.run(:com.google.android.gms.dynamite_adsdynamite@14366051@14.3.66 (040408-213742215):4)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:457)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
at java.lang.Thread.run(Thread.java:764)
2018-10-08 18:52:27.110 5883-5883 / shihab.com.rewardvideoad W / Ads:无法加载广告:3
我还添加了minifest互联网许可。
<uses-permission android:name="android.permission.INTERNET"/>