Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off all sounds on Android

Tags:

android

I'm new developing on Android. I'm have a view with a ToggleButton on it that when pushed will turn off all sound until the ToggleButton is pushed again.

Currently, I have this code. I can see the logging events happening, but the sound is not turning off. Can anyone provide some insight?

 public void onToggleClicked(View view) {
    Log.i("onToggleClicked", "ToggleClick Event Started");
    // Is the toggle on?
    boolean on = ((ToggleButton) view).isChecked();

    if (on) {
        Log.i("onToggleIsChecked", "ToggleClick Is On");
        //turn off sound, disable notifications
        amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
        Log.i("STREAM_SYSTEM", "Set to true");
        //notifications
        amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
        Log.i("STREAM_NOTIFICATION", "Set to true");
        //alarm
        amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
        Log.i("STREAM_ALARM", "Set to true");
        //ringer
        amanager.setStreamMute(AudioManager.STREAM_RING, true);
        Log.i("STREAM_RING", "Set to true");
        //media
        amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
        Log.i("STREAM_MUSIC", "Set to true");

    } else {
        Log.i("onToggleIsChecked", "ToggleClick Is Off");
        // turn on sound, enable notifications
        amanager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
        Log.i("STREAM_SYSTEM", "Set to False");
        //notifications
        amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
        Log.i("STREAM_NOTIFICATION", "Set to False");
        //alarm
        amanager.setStreamMute(AudioManager.STREAM_ALARM, false);
        Log.i("STREAM_ALARM", "Set to False");
        //ringer
        amanager.setStreamMute(AudioManager.STREAM_RING, false);
        Log.i("STREAM_RING", "Set to False");
        //media
        amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
        Log.i("STREAM_MUSIC", "Set to False");
    }
    Log.i("onToggleClicked", "ToggleClick Event Ended");
}

Thanks in advance!

like image 817
Briana Finney Avatar asked Sep 10 '12 14:09

Briana Finney


People also ask

How do I turn off mute all sounds on a Samsung?

When silent mode is turned on, all phone sounds are turned off. Slide your finger downwards starting from the top of the screen. Press the sound mode icon the necessary number of times to turn silent mode on or off. Press the Home key to return to the home screen.


Video Answer


1 Answers

I was able to make it work by changing the event to a listener

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //create the toggle button ref and instantiate
    ToggleButton tb = (ToggleButton)this.findViewById(R.id.tglSetStatus);
    tb.setTextOff(getString(R.string.available_status));
    tb.setTextOn(getString(R.string.driving_status));
    //default to being available
    tb.setChecked(false);
    // attach an OnClickListener
    tb.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            // your click actions go here
            Log.i("onToggleClicked", "ToggleClick Event Started");
            //an AudioManager object, to change the volume settings  
            AudioManager amanager; 
            amanager = (AudioManager)getSystemService(AUDIO_SERVICE);

            // Is the toggle on?
            boolean on = ((ToggleButton)v).isChecked();

            if (on) {
                Log.i("onToggleIsChecked", "ToggleClick Is On");
                //turn ringer silent
                amanager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
                Log.i("RINGER_MODE_SILENT", "Set to true");

                //turn off sound, disable notifications
                amanager.setStreamMute(AudioManager.STREAM_SYSTEM, true);
                Log.i("STREAM_SYSTEM", "Set to true");
                //notifications
                amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, true);
                Log.i("STREAM_NOTIFICATION", "Set to true");
                //alarm
                amanager.setStreamMute(AudioManager.STREAM_ALARM, true);
                Log.i("STREAM_ALARM", "Set to true");
                //ringer
                amanager.setStreamMute(AudioManager.STREAM_RING, true);
                Log.i("STREAM_RING", "Set to true");
                //media
                amanager.setStreamMute(AudioManager.STREAM_MUSIC, true);
                Log.i("STREAM_MUSIC", "Set to true");

            } else {
                Log.i("onToggleIsChecked", "ToggleClick Is Off");

                //turn ringer silent
                amanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
                Log.i(".RINGER_MODE_NORMAL", "Set to true");

                // turn on sound, enable notifications
                amanager.setStreamMute(AudioManager.STREAM_SYSTEM, false);
                Log.i("STREAM_SYSTEM", "Set to False");
                //notifications
                amanager.setStreamMute(AudioManager.STREAM_NOTIFICATION, false);
                Log.i("STREAM_NOTIFICATION", "Set to False");
                //alarm
                amanager.setStreamMute(AudioManager.STREAM_ALARM, false);
                Log.i("STREAM_ALARM", "Set to False");
                //ringer
                amanager.setStreamMute(AudioManager.STREAM_RING, false);
                Log.i("STREAM_RING", "Set to False");
                //media
                amanager.setStreamMute(AudioManager.STREAM_MUSIC, false);
                Log.i("STREAM_MUSIC", "Set to False");
            }
            Log.i("onToggleClicked", "ToggleClick Event Ended");
        }
    });

I found input by looking at these questions: How to turn off all sounds and vibration via Android API and this one: attach onClickListener to ToggleButton

like image 158
Briana Finney Avatar answered Oct 22 '22 09:10

Briana Finney