简体   繁体   中英

Showing an interstitial ad after a sound has finished

I'm fairly new to android development and learning as I go, please go easy on me... I'm working on a soundboard where I want to wait until after a sound is finished playing before an interstitial ad is displayed, my current code below doesn't initialise the ad:

button1 = (findViewById(R.id.button));
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                stopPlaying();
                mp = MediaPlayer.create(MainActivity.this, R.raw.dontbeanidiot);
                clickCount = clickCount + 1;
                mp.start();
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        if (mp.isPlaying()) {
                            mp.stop();
                            mp.release();
                            if (clickCount == 10) {
                                mInterstitialAd.show();
                                clickCount = 0;
                            }
                        }
                    }
                });
            }
        });

This is the code that does show the interstitial but on button press rather than after. What am I doing wrong?

button2 = findViewById(R.id.button2);
        button2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                stopPlaying();
                mp = MediaPlayer.create(MainActivity.this, R.raw.wouldanidiot);
                clickCount=clickCount+1;
                if (clickCount==10) {
                    if (mInterstitialAd.isLoaded()) {
                        mInterstitialAd.show();
                        clickCount=0;
                    }

                }
                mp.start();
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        if (mp.isPlaying()) {
                            mp.stop();
                            mp.release();
                        }

                    }
                });
            }

        });

I changed it to this and now it works as I wanted. Yes, I'm an idiot.

 button1 = (findViewById(R.id.button));
        button1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                stopPlaying();
                mp = MediaPlayer.create(MainActivity.this, R.raw.dontbeanidiot);
                clickCount = clickCount + 1;
                mp.start();
                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    public void onCompletion(MediaPlayer mp) {
                        if (mp.isPlaying()) {
                            mp.stop();
                            mp.release();

                        }
                        if (clickCount > 10) {
                            mInterstitialAd.show();
                            clickCount = 0;
                        }
                    }
                });
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM