How to mute the sound of pressing volume keys only in my app on Android?
Should I just mute the volum开发者_如何学运维e onResume and set the volume level back onPause with AudioManager?
Or is there another proper way to do this?
Thanks!
I think your answer lies in the documentation for setStreamMute()
.
The mute command is protected against client process death: if a process with an active mute request on a stream dies, this stream will be unmuted automatically.
The mute requests for a given stream are cumulative: the AudioManager can receive several mute requests from one or more clients and the stream will be unmuted only when the same number of unmute requests are received.
For a better user experience, applications MUST unmute a muted stream in onPause() and mute is again in onResume() if appropriate.
If anyone is interested, there's a workaround to avoid your app muting everything. Doing the following, will mute the volume keys and notifications incoming calls will remain as configured in the system (rather than being forcely muted).
setVolumeControlStream(AudioManager.STREAM_DTMF);
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamMute(AudioManager.STREAM_DTMF, true);
精彩评论