Hi.
This is Bogdan with another interesting , I hope, tutorial for http://androidgenuine.com .
Today I will show you how to adjust the sound of the mediaplayer using a seekbar. I guess you have at least one song on your phone to check if the change of volume works but in any case I also put in this project 3 buttons : Start , Pause and Stop ; to control the song I added.
I hope you do not dislike the song so much. Anyway, I like this song so that’s why it is Maroon 5 – Moves Like Jagger.
Ok. The structure is simple :
-I have a class called: ControlVolume.class
-An xml file : mylayout.xml (like usual)
-This is important .Be focused. I created a new folder in type resources (res) called raw where I added the song.
Here is ControlVolume.class:
public class ControlVolume extends Activity implements OnSeekBarChangeListener{ //We neww onseekbarchangelistener so I implemented it
//Here I declare some stuff
private static TextView reading = null;
AudioManager audioManager;
MediaPlayer oSong;
int stopped =1 ;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout); //The layout that we are using
//Here is a song that I have in a folder that I create in resources.
//The name of the folder is raw and there is my song called "melodie"
//Actually the song is Maroon 5 - Moves Like Jaggar
oSong = MediaPlayer.create(ControlVolume.this, R.raw.melodie);
//Here is the textview that we are using to show to progress : ?%
reading = (TextView) findViewById(R.id.reading);
SeekBar seekBar = (SeekBar) findViewById(R.id.sbSound);
//Here we declare the audioManager . We change the volume with it.
audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
//We need the stream max volume to set the seekbar's maximum with this value
int maxV = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
//We want that the default progress of the seekbar to be the value that the sound is in that moment.
//For example if the sound is 50% we want the progress of the seekbar to be at the half of it
int curV = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
//Here we set the max and progress like I said above.
seekBar.setMax(maxV);
seekBar.setProgress(curV);
//Here we do some maths to find out the percentage of the volume and the seekbar's progress is.
int x = (int)(curV*(((float)100/15)));
//We set the default text for our textview
reading.setText(""+x+"% ");
//Here we have the 3 buttons : Start,Pause,Stop; for our song.
Button bStart = (Button) findViewById(R.id.bStart);
bStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
//I am using the int stopped to know if the song is stopped or paused
if(stopped==1){
//If it is stopped we need to assign it the song we want again.
oSong = MediaPlayer.create(ControlVolume.this, R.raw.melodie);
}
oSong.start();
}
});
Button bPause = (Button) findViewById(R.id.bPause);
bPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
oSong.pause();
//Playing with stopped variable
stopped=0;
}
});
Button bStop = (Button) findViewById(R.id.bStop);
bStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
oSong.stop();
stopped=1;
}
});
//We set the onseekbarchangelistener
seekBar.setOnSeekBarChangeListener(this);
}
public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) {
// TODO Auto-generated method stub
//We update the textview with actual progress
int x = (int)(progress*(((float)100/15)));
reading.setText(""+x+"% ");
//We update the sound with actual progress of the seekbar
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
}
public void onStartTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar arg0) {
// TODO Auto-generated method stub
}
}
Here is the XML file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
>
<Button
android:id="@+id/bStart"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text="Start" />
<Button
android:id="@+id/bPause"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text="Pause" />
<Button
android:id="@+id/bStop"
android:layout_width="80dip"
android:layout_height="wrap_content"
android:text="Stop" />
</LinearLayout>
<SeekBar
android:id="@+id/sbSound"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/reading"
android:text="0%"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40dp"
android:layout_gravity="center"
/>
</LinearLayout>
Here is a picture where you can see how the application looks like:

You can download the entire project, like always, from here.
This was all for this tutorial. I hope you understood and liked it. See you with more new and interesting stuff . Bye!