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?
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.
The accepted answer works fine in muting the system, but if you need to restore the state (e.g. when users pause / quit your app), please note that the semantics of the
adjustStreamVolume
andsetStreamMute
methods are different:For
setStreamMute
, from the documentation:This does not seem to be the case for
adjustStreamVolume
withAudioManager.ADJUST_MUTE
. In other words, if the status of the stream is already muted before you mute it withsetStreamMute (stream, true)
, an immediatesetStreamMute (stream, false)
will leave it in muted state, whileadjustStreamVolume
withAudioManager.ADJUST_UNMUTE
may unmute the stream.Depending on the use case, to emulate the old semantics, one way is to check the mute state before muting, something like the following -
To mute:
To unmute:
This assumes users are unlikely to invoke another app to mute the stream in-between and expect the stream to stay muted after your app unmutes (anyway there seems no way to check this).
Incidentally, the
isStreamMute
method was previously hidden, and was only unhidden in API 23, enabling its use for this purpose.