android control volume programmatically
Hello Guys. Welcome to our new tutorial of android control volume programmatically using android studio. this tutorial will help you to change android volume with seekbar programmatically.
you can control media volume, system volume, alarm volume, notification volume, etc.step by step tutorial for Android AudioManager volume control example tutorial using seekbar.
please follow the code given below to android volume control example tutorial using seekbar.
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
import android.content.Context;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.SeekBar;
public class mediavolumecontrol extends AppCompatActivity {
AudioManager mAudio;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mediavolumecontrol);
mAudio = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
SeekBar alarm = (SeekBar)findViewById(R.id.alarm);
SeekBar music = (SeekBar)findViewById(R.id.music);
SeekBar ring = (SeekBar)findViewById(R.id.ring);
SeekBar system = (SeekBar)findViewById(R.id.system);
SeekBar voice = (SeekBar)findViewById(R.id.voice);
initControls(alarm, AudioManager.STREAM_ALARM);
initControls(music, AudioManager.STREAM_MUSIC);
initControls(ring, AudioManager.STREAM_RING);
initControls(system, AudioManager.STREAM_SYSTEM);
initControls(voice, AudioManager.STREAM_VOICE_CALL);
}
private void initControls (SeekBar seek, final int stream)
{
seek.setMax(mAudio.getStreamMaxVolume(stream));
seek.setProgress(mAudio.getStreamVolume(stream));
seek.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar bar, int progress, boolean fromUser)
{
mAudio.setStreamVolume(stream, progress, AudioManager.FLAG_PLAY_SOUND);
}
public void onStartTrackingTouch(SeekBar bar) {
}
public void onStopTrackingTouch(SeekBar bar) {
}
});
}
}
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" >
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/textViewAlarm"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewAlarm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:layout_marginTop="32dp"
android:text="Adjust Alarm Volume"
app:layout_constraintBottom_toTopOf="@+id/alarm"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<SeekBar
android:id="@+id/alarm"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewAlarm"
app:layout_constraintBottom_toTopOf="@+id/textViewMusic"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewAlarm" />
<TextView
android:id="@+id/textViewMusic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/alarm"
android:text="Adjust Music Volume"
app:layout_constraintBottom_toTopOf="@+id/music"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/alarm" />
<SeekBar
android:id="@+id/music"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewMusic"
app:layout_constraintBottom_toTopOf="@+id/textViewRing"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewMusic" />
<TextView
android:id="@+id/textViewRing"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/music"
android:text="Adjust Ringtone Volume"
app:layout_constraintBottom_toTopOf="@+id/ring"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/music" />
<SeekBar
android:id="@+id/ring"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewRing"
app:layout_constraintBottom_toTopOf="@+id/textViewSystem"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewRing" />
<TextView
android:id="@+id/textViewSystem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/ring"
android:text="Adjust System Volume"
app:layout_constraintBottom_toTopOf="@+id/system"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ring" />
<SeekBar
android:id="@+id/system"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewSystem"
app:layout_constraintBottom_toTopOf="@+id/textViewVoice"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewSystem" />
<TextView
android:id="@+id/textViewVoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/system"
android:text="Adjust Voice Volume"
app:layout_constraintBottom_toTopOf="@+id/voice"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/system" />
<SeekBar
android:id="@+id/voice"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewVoice"
android:layout_marginBottom="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textViewVoice" />
</android.support.constraint.ConstraintLayout>
Output:
Finally, run this project.
This is how android control volume programmatically in android looks like.


