我有一个MediaPlayer,并且music.start();工作正常。另外,我有一个间隙广告,并在onCreate加载。广告也运行得很好。我想在广告结束后播放音乐。目前,当我看到广告时,音乐正在播放。当我看到广告时,我想把音乐停下来。并在关闭add后再次播放。我该怎么做呢?
private AdRequest adRequest2;
private InterstitialAd minterstitialAd;
private MediaPlayer music;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level6);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
adRequest2 = new AdRequest.Builder().build();
InterstitialAd.load(this, "ca-app-pub-1245895864529409/518807", adRequest2, new InterstitialAdLoadCallback()
{
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
System.out.println("Ad Loaded");
minterstitialAd=interstitialAd;
minterstitialAd.show(Level6.this);
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
System.out.println("Ad Load Failed");
minterstitialAd=null;
}
});
music=MediaPlayer.create(getApplicationContext(),R.raw.oyun);
music.start;}}}发布于 2021-03-15 03:57:02
以下是您可以在间隙广告上调用的所有可用方法,在您的情况下,onAdOpened是您应该停止音乐的地方,而onAdClosed方法是您应该播放音乐的地方
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdFailedToLoad(LoadAdError adError) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
}因此,在您的代码中添加onAdClosed方法并在其中播放音乐
InterstitialAd.load(this, "ca-app-pub-1245895864529409/518807", adRequest2, new InterstitialAdLoadCallback()
{
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
System.out.println("Ad Loaded");
minterstitialAd=interstitialAd;
minterstitialAd.show(Level6.this);
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
System.out.println("Ad Load Failed");
minterstitialAd=null;
}
@Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
music=MediaPlayer.create(getApplicationContext(),R.raw.oyun);
music.start;
}
});https://stackoverflow.com/questions/66628628
复制相似问题