dor00012 dor00012 - 5 months ago 284
Android Question

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?

Answer

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