Mute the global sound in Android

前端 未结 6 1108
心在旅途
心在旅途 2020-12-02 20:49

Is there a method that can be used to mute the global sound from an application button?

6条回答
  •  一向
    一向 (楼主)
    2020-12-02 21:13

    They make it more complicated than it has to be. You can just use AudioManager.setStreamMute(). Feel free to use the code below.

    //mute audio
    AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
                 amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
                 amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
                 amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
                 amanager.setStreamMute(AudioManager.STREAM_RING, true);
                 amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
    
    
    //unmute audio
    AudioManager amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);
                 amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
                 amanager.setStreamMute(AudioManager.STREAM_ALARM, false);
                 amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
                 amanager.setStreamMute(AudioManager.STREAM_RING, false);
                 amanager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
    

提交回复
热议问题