A
A
Alexey Medvedev2015-12-28 22:01:24
Java
Alexey Medvedev, 2015-12-28 22:01:24

Stopping MediaPlayer on fullscreen ads?

On the first activity, a full-screen ad (AdMob) is loaded
. There is a second activity. The game itself. It starts playing music as soon as the activity starts. Music is played using MedaiPlayer, also I pause the music when the application is minimized and turn it off when the activity is closed.
Then in the Intent after startActivity() I show a full-screen ad. When it is closed, the music is superimposed on itself twice. Moreover, one thread then simply falls silent after 4-7 seconds. The next time the game shows a full screen (not at the start of the activity), the music is paused, and continues to play when the ad is closed.
1 Activity

Intent intent = new Intent(this, Game.class);
 startActivity(intent);
if (interstitial.isLoaded()) interstitial.show();

2 Activities
MediaPlayer mp;
int music;

//в onCreate
music = R.raw.m1;
playMusic();

//далее после onCreate
 void playMusic(){
        new Thread(){
            public void run(){
                MediaPlayer.create(Game.this, music);
                mp.start();

                mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){
                    public void onCompletion(MediaPlayer arg0){
                        arg0.start();
                    }
                });
            }
        }.start();
    }

 @Override
    public void onPause() {
        Game.super.onPause();
        if (mp != null) mp.pause();
    }

    @Override
    public void onResume() {
        Game.super.onResume();
        playMusic();
    }

Q: What is the reason for re-overlaying music when opening an activity and a full-screen ad? After all, with the further display, everything is in order. And what is the reason for the subsequent independent shutdown of one thread?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Perelygin, 2015-12-29
@RockBearLTD

Well, apparently, you call playMusic in onCreate and in onResume, respectively, two Thread objects are created and inside each object a player is created. Here they are all playing together. and you stop only the one to which the last created one refers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question