How to make playback control from the volume button? When the track is playing, the button was not active. The track ended again became active. Here is the code:

@Override public boolean onKeyDown(int keyCode, KeyEvent event) { // TODO Auto-generated method stub switch (keyCode) { case KeyEvent.KEYCODE_VOLUME_UP: sounds = new int[] { R.raw.muz1, R.raw.muz2, R.raw.muz3, R.raw.muz4, R.raw.muz5, R.raw.muz6, R.raw.muz6, R.raw.muz7 }; mp = MediaPlayer.create(getApplicationContext(), sounds[index]); mp.setLooping(false); mp.start(); mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { public void onCompletion(MediaPlayer mp) { mp.release(); if (index < sounds.length - 1) { index++; return; } } }); } return false; } 

    1 answer 1

    Just check at the very beginning of the click handler that something is being played and in that case do not do anything.

     if(mp.isPlaying()) { return; } 
    • Do you want to disable the hardware sound control button for the duration of the song? - ZigZag
    • one
      In this case, it is contrary to the rules of good practice of writing applications for Android. It is not recommended to change the effect of hardware buttons to those for which they are not intended, or disable them. The user may be confused by the fact that he can not do the usual action, in this case, reduce the volume during the playback of the track. Such functionality in itself carries an error in the application logic. Do not mock the user. - ZigZag