* Mute or unmute an audio stream.
* <p>
* 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.
* <p>
* 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.
* <p>
* For a better user experience, applications MUST unmute a muted stream
* in onPause() and mute is again in onResume() if appropriate.
* <p>
* This method should only be used by applications that replace the platform-wide
* management of audio settings or the main telephony application.
*
* @param streamType The stream to be muted/unmuted.
* @param state The required mute state: true for mute ON, false for mute OFF
*/
红色部分的意思是:静音请求是可以叠加的,AudioManager 可以接收多个静音请求;
但是只有接收到同等数量的非静音请求后才会实现非静音功能;
例:
AndroidAudioManager androidAudioManager = (android.media.AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
androidAudioManager.setStreamMute(android.media.AudioManager.STREAM_MUSIC, true);
androidAudioManager.setStreamMute(android.media.AudioManager.STREAM_MUSIC, true);
androidAudioManager.getStreamVolume(android.media.AudioManager.STREAM_MUSIC);
此时获取到的音量值是0 ;
androidAudioManager.setStreamMute(android.media.AudioManager.STREAM_MUSIC, false);
此时获取到的音量值是0 ;
androidAudioManager.setStreamMute(android.media.AudioManager.STREAM_MUSIC, false);
此时获取到的音量值是设置静音前的音量值 ;
正确的使用方法:设置静音状态之前要判断当前的系统静音状态,如果是要的target 状态就不要重复设置了。