The problem is this: case R.id.b1: I chose animation + music, I choose case R.id.b2: the player and animation stopped and I need to click a second time to play case R.id.b2: how can I avoid a second click? I know that in the method I check and stop the player, but that is if I re-press case R.id.b1:, i.e. I need a check on the playing player

@Override public void onClick(View v) { stopAnimation(); switch (v.getId()) { //imageview1 case R.id.b1: { v.startAnimation(anim); playSample(soundsRawResIds[0]); break; } //imageview2 case R.id.b2: { v.startAnimation(anim); playSample(soundsRawResIds[1]); break; } //imageview3 case R.id.b3: { v.startAnimation(anim); playSample(soundsRawResIds[2]); break; } case R.id.b4: { v.startAnimation(anim); playSample(soundsRawResIds[3]); break; } case R.id.b5: { v.startAnimation(anim); playSample(soundsRawResIds[4]); break; } case R.id.b6: { v.startAnimation(anim); playSample(soundsRawResIds[5]); break; } case R.id.b7: { v.startAnimation(anim); playSample(soundsRawResIds[6]); break; } } }} //метод через,который пытаюсь воспроизводить мелодии private void playSample(int resid) { AssetFileDescriptor afd = getContext().getResources().openRawResourceFd(resid); if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); } else { try { mediaPlayer.reset(); mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength()); mediaPlayer.prepareAsync(); afd.close(); } catch (IllegalArgumentException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); } catch (IllegalStateException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); } catch (IOException e) { Log.e(TAG, "Unable to play audio queue do to exception: " + e.getMessage(), e); } } } 

    2 answers 2

    You have no error. You wrote it the way it works. First, the player stops and immediately launches a new one. You would read a thread on how conditional operators work. In your case, you need to wrap the rest of the playSample method (i.e., try and catch blocks) in the else.

    • @upward, so ... now somewhat confused ... And what did not suit the previous version of the code? .. - YuriiSPb
    • @upwardm, well ... Return it as it was ... I don’t remember why the original option didn’t suit you - YuriySPb
    • @upward, if I understood correctly, you need to activate the int variable that stores the ID of the last pressed button. If pi pressing is a coincidence and the music does not play, then start, otherwise stop YuriiSPb
    • @upward, yes ID is v.getId() checks are organized by if - YurySPb
    • one
      I repeat - create a variable to store the last clicked ID. When pressed, compare this value with the current ID and whether the music is played at the same time. You also wrote code that is always true - Yuriy SPb

    As always, thanks @ YuriySPb

    That's what I need:

      @Override public void onClick(View v) { imageButton.setImageResource(R.drawable.play_button_pause); stopAnimation(); if (saveID ==v.getId()&mediaPlayer.isPlaying()){ mediaPlayer.stop(); } else{ switch ( saveID = v.getId()) { case R.id.b1: { playSample(soundsRawResIds[0]); v.startAnimation(anim); break; } case R.id.b2: { playSample(soundsRawResIds[1]); v.startAnimation(anim); break; } case R.id.b3: { playSample(soundsRawResIds[2]); v.startAnimation(anim); break; } case R.id.b4: { playSample(soundsRawResIds[3]); v.startAnimation(anim); break; } case R.id.b5: { playSample(soundsRawResIds[4]); v.startAnimation(anim); break; } case R.id.b6: { playSample(soundsRawResIds[5]); v.startAnimation(anim); break; } case R.id.b7: { playSample(soundsRawResIds[6]); v.startAnimation(anim); } break; } } } }