mHvNG
mHvNG

Reputation: 671

Android application device volume

I have a few questions about an application I want to develop.

  1. I'm trying to find out if it's possible to make a feature that sets the message / ringtone volume from the device (not the application alone) to lets say 20%.

  2. Is it possible to check the volume from the device? So that I can check at all times if the volume is lower than 20%.

  3. Is it possible to make an app run 24/7, so I can always check the volume?

Android Studio and Flutter is both fine.

Upvotes: 1

Views: 167

Answers (2)

mHvNG
mHvNG

Reputation: 671

For this project I used the volume library. This library lets you change all the different kind of volumes inside Android.

My code:

@override
  void initState() {
      super.initState();
      audioManager = AudioManager.STREAM_NOTIFICATION;
      initPlatformState();
      const fiveSec = const Duration(seconds: 15);
      timer = new Timer.periodic(fiveSec, (Timer timer) => setState(
        () {
          updateVolumes();
        }
      ));
  }

  Future<void> initPlatformState() async {
    await Volume.controlVolume(AudioManager.STREAM_NOTIFICATION);
  }

  updateVolumes() async {
    setState(() {

    });
    currentVol = await Volume.getVol;
    if (currentVol < minimumVol) {
      print('Volume needs some adjustments...');
      setVol(minimumVol);
    } else {
      print('Volume is fine!');
    }
  }

  setVol(int i) async {
    await Volume.setVol(i);
  }

Upvotes: 1

Ediz Kaplan
Ediz Kaplan

Reputation: 21

 public class TestExample extends Activity 
   {

    private SeekBar volumeSeekbar = null;
    private AudioManager audioManager = null; 

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setVolumeControlStream(AudioManager.STREAM_MUSIC);
        setContentView(R.layout.main);
        initControls();
    }

    private void initControls()
    {
        try
        {
            volumeSeekbar = (SeekBar)findViewById(R.id.seekBar1);
            audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            volumeSeekbar.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volumeSeekbar.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC));   


            volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
            {
                @Override
                public void onStopTrackingTouch(SeekBar arg0) 
                {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) 
                {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) 
                {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            progress, 0);
                }
            });
        }
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

Upvotes: 2

Related Questions