Search code examples
androidandroid-6.0-marshmallowandroid-audiomanager

Alternative for deprecated AudioManger.setStreamMute?


AudioManger.setStreamMute is now deprecated with api 23 and it is preferred to use AudioManager.adjustStreamVolume with AudioManager.ADJUST_MUTE.

My problem is that this kind of flag is only supported with api 23 while my app is minimum api 16.

Is there an other way of muting the whole system?

If not, why would google deprecate this method?


Solution

  • The way I would do it would be to use an if/else block to use the proper calls depending on the version of Android that the app is currently running under.

    // Change the stream to your stream of choice. 
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
       am.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_MUTE, 0);
    } else {
       am.setStreamMute(AudioManager.STREAM_MUSIC, true);
    }