Alternative for deprecated AudioManger.setStreamMute?

dor00012 picture dor00012 · Oct 10, 2015 · Viewed 8.2k times · Source

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?

Answer

phxhawke picture phxhawke · Oct 10, 2015

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