Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change device volume from seek bar in android

Tags:

android

I know how to control volume of media player from seekbar.But how can i do to change system volume from seekbar in android.

like image 815
koti Avatar asked Apr 19 '12 14:04

koti


People also ask

What is SeekBar?

In Android, SeekBar is an extension of ProgressBar that adds a draggable thumb, a user can touch the thumb and drag left or right to set the value for current progress. SeekBar is one of the very useful user interface element in Android that allows the selection of integer values using a natural user interface.

How do I change the color of SeekBar?

If you are using default SeekBar provided by android Sdk then their is a simple way to change the color of that . just go to color. xml inside /res/values/colors. xml and change the colorAccent.


2 Answers

Use AudioManager and methods like adjustStreamVolume(). Here is a sample application that uses SeekBar widgets to adjust the volumes of various streams.

like image 193
CommonsWare Avatar answered Sep 22 '22 18:09

CommonsWare


The following code can be used :

import android.app.Activity;

import android.content.Context;

import android.media.AudioManager;

import android.os.Bundle;

import android.widget.SeekBar;

import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

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();
    }
}
like image 29
Aashutosh Shrivastava Avatar answered Sep 25 '22 18:09

Aashutosh Shrivastava