I created the music player app and i want to set the volume
up/down programmatically .
I want to implement two another Button
for increase/decrease of volume and set to the media player .
Please help me in accessing volume up/down in android.
Activity Codes :
control = (ImageView) findViewById(R.id.control);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
control.setOnClickListener(pausePlay);
control.setBackgroundResource(R.drawable.pause);
control id is my play and pause button :
{
// TODO Auto-generated method stub
// TODO Auto-generated method stub
if (playPause) {
control.setBackgroundResource(R.drawable.play);
if (mediaPlayer.isPlaying())
mediaPlayer.pause();
media.stop();
intialStage = false;
playPause = false;
} else {
control.setBackgroundResource(R.drawable.pause);
if (intialStage) {
new Player()
.execute("http://streaming.shoutcast.com/MUKILFMRADIO");
} else {
if (!mediaPlayer.isPlaying())
mediaPlayer.start();
}
playPause = true;
}
}
Layout codes:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/control1"
android:layout_margin="10dp"
android:layout_gravity="center"
android:background="@drawable/decrement"
android:layout_above="@+id/latestAddedSongs"
android:layout_alignEnd="@+id/musicArtistName" />
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:id="@+id/control"
android:layout_margin="10dp"
android:layout_gravity="center"
android:background="@drawable/play"
android:layout_above="@+id/latestAddedSongs"
android:layout_alignEnd="@+id/musicArtistName" />
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="@+id/control2"
android:layout_margin="10dp"
android:layout_gravity="center"
android:background="@drawable/increment"
android:layout_above="@+id/latestAddedSongs"
android:layout_alignEnd="@+id/musicArtistName" />
</LinearLayout>
Give a link or tutorial about Mute/UnMute and Volume Up/Down in android
Programmatically.
if your type is AudioManager.STREAM_MUSIC: use this code ,follow phone volume button
Try below code
Create an object for audio manager
The above example used Button label
for volume up and down code
Below code worked great
There are several streams available in Android using which audio can be managed
Depending on your need you can change the stream here is STREAM_MUSIC example :
You can get constant value using refelction from AudioSytem class:
Following up on solution of jesu, if you want to change volume within Service instead of Activity, replace:
with: