简体   繁体   中英

Android Media Player Double Playing Issue

I am making a Tic-Tac-Toe.

I have a MusicManager class which manages the music and a ThemesActivity class which starts the music based on 6 themes. Whenever a button is clicked, it stops the other mediaPlayers as expected. However, when double tapping a theme, it plays the music twice. I have tried to stop it using isPlaying() but it didnt worked out.

Here is the following code of MusicManager:

//Mediaplayer -- For All 6 themes
    private MediaPlayer mp;
    public static MediaPlayer normalMP;
    public static MediaPlayer loveMP;
    public static MediaPlayer tomAndJerryMP;
    public static MediaPlayer clashOfClansMP;
    public static MediaPlayer cricketMP;
    public static MediaPlayer iceAgeMP;

public static void playMusic(Context context) {

    int chosenTheme = Utils.loadPreferences(context, "theme", 0);

    if(chosenTheme == 0){
        normalMP = MediaPlayer.create(context, R.raw.darude_sandstorm);
        normalMP.setLooping(true);
        normalMP.start();

        if(loveMP != null) {
            loveMP.release();
        }
        if(tomAndJerryMP != null) {
            tomAndJerryMP.release();
        }
        if(clashOfClansMP != null) {
            clashOfClansMP.release();
        }
        if(cricketMP != null) {
            cricketMP.release();
        }
        if(iceAgeMP != null) {
            iceAgeMP.release();
        }
    }

    if(chosenTheme == 1){
        loveMP = MediaPlayer.create(context, R.raw.love);
        loveMP.setLooping(true);
        loveMP.start();

        if(normalMP != null) {
            normalMP.release();
        }
        if(tomAndJerryMP != null) {
            tomAndJerryMP.release();
        }
        if(clashOfClansMP != null) {
            clashOfClansMP.release();
        }
        if(cricketMP != null) {
            cricketMP.release();
        }
        if(iceAgeMP != null) {
            iceAgeMP.release();
        }
    }

    if(chosenTheme == 2){
        tomAndJerryMP = MediaPlayer.create(context, R.raw.tommy);
        tomAndJerryMP.setLooping(true);
        tomAndJerryMP.start();

        if(normalMP != null) {
            normalMP.release();
        }
        if(loveMP != null) {
            loveMP.release();
        }
        if(clashOfClansMP != null) {
            clashOfClansMP.release();
        }
        if(cricketMP != null) {
            cricketMP.release();
        }
        if(iceAgeMP != null) {
            iceAgeMP.release();
        }
    }

    if(chosenTheme == 3){
        clashOfClansMP = MediaPlayer.create(context, R.raw.pinkpanther);
        clashOfClansMP.setLooping(true);
        clashOfClansMP.start();

        if(normalMP != null) {
            normalMP.release();
        }
        if(loveMP != null) {
            loveMP.release();
        }
        if(tomAndJerryMP != null) {
            tomAndJerryMP.release();
        }
        if(cricketMP != null) {
            cricketMP.release();
        }
        if(iceAgeMP != null) {
            iceAgeMP.release();
        }
    }

    if(chosenTheme == 4){
        cricketMP = MediaPlayer.create(context, R.raw.cricket);
        cricketMP.setLooping(true);
        cricketMP.start();

        if(normalMP != null) {
            normalMP.release();
        }
        if(loveMP != null) {
            loveMP.release();
        }
        if(tomAndJerryMP != null) {
            tomAndJerryMP.release();
        }
        if(clashOfClansMP != null) {
            clashOfClansMP.release();
        }
        if(iceAgeMP != null) {
            iceAgeMP.release();
        }
    }

    if(chosenTheme == 5) {
        iceAgeMP = MediaPlayer.create(context, R.raw.ice_age);
        iceAgeMP.setLooping(true);
        iceAgeMP.start();

        if (normalMP != null) {
            normalMP.release();
        }
        if (loveMP != null) {
            loveMP.release();
        }
        if (tomAndJerryMP != null) {
            tomAndJerryMP.release();
        }
        if (clashOfClansMP != null) {
            clashOfClansMP.release();
        }
        if (cricketMP != null) {
            cricketMP.release();
        }
    }
}

public static void stopMusic(Context context) {

    int chosenTheme = Utils.loadPreferences(context, "theme", 0);

    if(chosenTheme == 0){
        normalMP.stop();
    }
    if(chosenTheme == 1){
        loveMP.stop();
    }
    if(chosenTheme == 2){
        tomAndJerryMP.stop();
    }
    if(chosenTheme == 3){
        clashOfClansMP.stop();
    }
    if(chosenTheme == 4){
        cricketMP.stop();
    }
    if(chosenTheme == 5){
        iceAgeMP.stop();
    }

}

And here is my ThemesActivity -- music playing part

//Layout Global Variables
    RelativeLayout normalLayout;
    RelativeLayout loveLayout;
    RelativeLayout tomAndJerryLayout;
    RelativeLayout clashOfClansLayout;
    RelativeLayout cricketLayout;
    RelativeLayout iceAgeLayout;

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_themes);

 //Referencing Views with IDs
        normalLayout = (RelativeLayout) findViewById(R.id.normalLayout);
        loveLayout = (RelativeLayout) findViewById(R.id.loveLayout);
        tomAndJerryLayout = (RelativeLayout) findViewById(R.id.tom_and_jerry_layout);
        clashOfClansLayout = (RelativeLayout) findViewById(R.id.clashLayout);
        cricketLayout = (RelativeLayout) findViewById(R.id.cricketLayout);
        iceAgeLayout = (RelativeLayout) findViewById(R.id.ice_age_layout);


  normalLayout.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       MusicManager.playMusic(getBaseContext());
  }

  loveLayout.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       MusicManager.playMusic(getBaseContext());
  }

  tomAndJerryLayout.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       MusicManager.playMusic(getBaseContext());
  }

  clashOfClansLayout.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       MusicManager.playMusic(getBaseContext());
  }

  cricketLayout.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       MusicManager.playMusic(getBaseContext());
  }

  iceAgeLayout.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {
       MusicManager.playMusic(getBaseContext());
  }
}

For six cases just call this method before playing the sound:

public static void playMusic(Context context, RelativeLayout normalLayout) {

int chosenTheme = Utils.loadPreferences(context, "theme", 0);

switch(chosenTheme){
    case 0 : normalMP = MediaPlayer.create(context, R.raw.darude_sandstorm);
             normalMP.setLooping(true);
             normalMP.start();
             normalMP.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { //Handling the resource release part

            @Override
            public void onCompletion(MediaPlayer mp)
            {
                mp.release();
                normalLayout.setEnabled(true); //Enable the button after sound has played
            }
        });
             break;
   //write case statements for other five cases

    }
}  

Double-click issue: To handle the multi-click issue, simply disable the button at the beginning of onclick and enable it after the sound has completed playing.

 normalLayout.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
   normalLayout.setEnabled(false);
   MusicManager.playMusic(getBaseContext(),normalLayout);
}

UPDATED

public static void playMusic(Context context) {

int chosenTheme = Utils.loadPreferences(context, "theme", 0);

if(chosenTheme == 0)
{
// releasing the playing music then restarting
if(normalMP!=null)
{
   normalMP.release();

}
    normalMP = MediaPlayer.create(context, R.raw.darude_sandstorm);
    normalMP.setLooping(true);
    normalMP.start();

    if(loveMP != null) {
        loveMP.release();
    }
    if(tomAndJerryMP != null) {
        tomAndJerryMP.release();
    }
    if(clashOfClansMP != null) {
        clashOfClansMP.release();
    }
    if(cricketMP != null) {
        cricketMP.release();
    }
    if(iceAgeMP != null) {
        iceAgeMP.release();
    }
}

I am showing you the first theme start/stop, implement this to all the other themes

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