johnnyshrewd
johnnyshrewd

Reputation: 1180

Android SeekBar update on volume button click

I am trying to sync a Seekbar with the volume of the device, and update it when the user is using the button to set the volume. But it does not seem to keep in sync. Sometimes only updates once and stops.

try
    {
        final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mVolume.setMax(audioManager
                .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
        mVolume.setProgress(audioManager
                .getStreamVolume(AudioManager.STREAM_MUSIC));


        mVolume.setOnSeekBarChangeListener(new SeekBar.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();
    }

And for the volume button click:

    @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    final AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
        try{
            mVolume.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC) - 1);
        } catch (Error e) {
            // min value
        }
    } else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
        try{
            mVolume.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC) + 1);
        } catch (Error e) {
            // max value
        }
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 2

Views: 1008

Answers (1)

Pabel
Pabel

Reputation: 672

I use the this method in my seekBar

    private boolean setVolumeMultimedia(int volume) {
    AudioManager audioManager = (AudioManager) mActivity.getSystemService(Context.AUDIO_SERVICE);
    if (audioManager == null) {
        NMPLog.e(TAG, "Unexpected null AudioManager. Unable to get/set the volume/mute.");
        return false;
    }
    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND);
    NMPLog.i(TAG, "Volume of session: " + audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
    return true;
}

I pass audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, volume, AudioManager.FLAG_PLAY_SOUND); and not 0. AudioManager.FLAG_PLAY_SOUND is the integer 4.

You can set this flags.

public static final int FLAG_ALLOW_RINGER_MODES = 2;
public static final int FLAG_PLAY_SOUND = 4;
public static final int FLAG_REMOVE_SOUND_AND_VIBRATE = 8;
public static final int FLAG_SHOW_UI = 1;
public static final int FLAG_VIBRATE = 16;

Upvotes: 1

Related Questions