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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
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)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
<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.