What method to make you download all the media volume on Android?

Asked

Viewed 106 times

4

My application needs the user to be able to choose at its beginning whether they want to hear the sounds of the app or not, because it is an app to be used in the classroom and in this environment the volume of the app should be reset.

So I created a AlertDialog for as soon as the app runs the user decides whether or not to lower the volume, so:

AlertDialog.Builder dlg = new AlertDialog.Builder(ActDinamica.this);
    dlg.setTitle("Bem Vindo!");
    dlg.setMessage("Você está em aula?");
    dlg.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Mute();
        }
    });
    dlg.setNegativeButton("Não", null);
    dlg.show();

Hence I need to configure this method Mute(); so that the volume of audio from mobile media goes to zero.

Method:

private void Mute() {
    // codigo para o volume zerar?
}

Note: I found only methods to stop specific audios that were being executed...

2 answers

2

In your file Manifest.xml, you must add permission to vibrate.

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

To silence the phone, you can trigger the AudioManager as follows:

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

And to get the volume back, you can do it this way:

AudioManager audioManager = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);

audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
  • Friend, first there was a mistake here (bold): [Audiomanager audioManager = (Audiomanager) context. getSystemService(Context.AUDIO_SERVICE);], after correcting this error based on the other answer to that question, it again did not work... But thank you!

2


You must use the Audiomanager, which is a public class, which provides volume access and control of bell mode. See an example below of how it can be used by defining, with the method setStreamVolume(), a specific volume:

AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);

In your case you can define how 0 as soon as the AlertDialog appear. See:

private void Mute() {
    AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
}

Check out the documentation for more details.

  • 1

    That wouldn’t fit a comment?

  • Not only for me, but it’s better to avoid clicks, add more in the community.

  • 1

    @Felipepaetzold looks again... I made another edition taking from the documentation. What you found?

  • 1

    It’s perfect! Thanks again! @Ack Lay

  • @Tonyálaffe are you man?! hehe.. Beauty! Need, just put the doubt here as detailed and objective as possible that the staff will find a way to help. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.