Android Media Player双重播放问题

时间:2016-10-30 04:40:32

标签: java android

我正在做一个Tic-Tac-Toe。

我有一个管理音乐的MusicManager类和一个基于6个主题启动音乐的ThemesActivity类。只要单击一个按钮,它就会按预期停止其他mediaPlayers。但是,当双击主题时,它会播放两次音乐。我试图使用isPlaying()来阻止它,但它没有成功。

以下是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();
    }

}

这是我的ThemesActivity - 音乐播放部分

//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());
  }
}

2 个答案:

答案 0 :(得分:1)

对于六种情况,只需在播放声音之前调用此方法:

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

    }
}  

双击问题:要解决多次点击问题,只需在onclick开头按下按钮,然后在声音播放完毕后启用它。

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

答案 1 :(得分:0)

<强>已更新

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();
    }
}

我向您展示第一个主题开始/停止,将其实施到所有其他主题