0

Hi I'm trying to add a mute button to my android project app just learning the basics here's what I did. I created a Button (this is just for now in the future I will try to use Switch) id set as android:id="@+id/muteBtn" and declared the varaibles in java Button muteBtn = (Button) findViewById(R.id.muteBtn) ; and when its click it must put the mode to silent mode I added a user-permission because its giving me an error Not allowed to change Do Not Disturb state

 <uses-permission android:name="android.permission.ACCESS_NOTIFICATION_POLICY" />

and here's the button click listener

 muteBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M
                    && !notificationManager.isNotificationPolicyAccessGranted()) {

                Intent intent = new Intent(
                        android.provider.Settings
                                .ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS);

                startActivity(intent);
            }
            else {
                AudioManager audioManager = (AudioManager) Settings.this.getSystemService(Context.AUDIO_SERVICE);
                audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            }
        }
    });

this method is not working any suggestions?

UPDATE 1

i just noticed this code works but the ringer volume volume goes down instead of media

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Possible duplicate https://stackoverflow.com/questions/31862753/android-how-to-turn-on-do-not-disturb-dnd-programmatically – Deni Erdyneev Feb 14 '18 at 11:35

2 Answers2

0

public void onClick(View v) {

  switch (v.getId()) {
  case R.id.wmute:
    AudioManager.setMode(AudioManager.MODE_IN_CALL);
    AudioManager.setStreamSolo(AudioManager.STREAM_VOICE_CALL, true);
     break;
  default:
     break;
  }

} }

   }
pawas kr. singh
  • 416
  • 1
  • 4
  • 12
0

Try this one: I am assuming you want to put down the volume of music and not put the phone on silent mode... Please correct me if i am wrong..

    if (!isMute) {
        AudioManager mAudioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);
        currentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        isMute = true;
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
        imageButtonMute.setImageDrawable(getResources().getDrawable(R.mipmap.mute_off));
     } else {

 imageButtonMute.setImageDrawable(getResources().getDrawable(R.mipmap.mute));
      AudioManager mAudioManager = (AudioManager) 
      activity.getSystemService(Context.AUDIO_SERVICE);
     isMute = false;                       
     mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, currentVolume, 0);
    }
Mohit
  • 134
  • 7
  • why is it giving me the same error `Non-static method setMode(int) cannot be referenced from a static content` im inside `onCreate` method – Christian Aure Feb 14 '18 at 11:38
  • i havnt used the setMode Method, can you post the exact line on which it is giving that error ? – Mohit Feb 14 '18 at 11:41
  • ` AudioManager mAudioManager = (AudioManager) activity.getSystemService(Context.AUDIO_SERVICE);` I change the word activity here and replace my `Settings` java class and then the `getSystemService` becomes underlined with red color and says the error i mentioned – Christian Aure Feb 14 '18 at 11:44
  • change it to Settings.this – Mohit Feb 14 '18 at 11:44
  • AudioManager mAudioManager = (AudioManager) Settings.this.getSystemService(Context.AUDIO_SERVICE); – Mohit Feb 14 '18 at 11:45
  • or try it without that. AudioManager mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); – – Mohit Feb 14 '18 at 11:46
  • thanks for the help! maybe I'm a bit confused and dig up for more and this just solved my problem. `setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);` – Christian Aure Feb 14 '18 at 11:52