Search code examples
javaandroidandroid-audiomanager

How to increase and decrease the volume programmatically in Android


I created a music player app and I want to set the volume up/down programmatically. I want to implement two Buttons to increase/decrease the volume and set to the media player.

Activity:

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:

<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>

Solution

  • Create an object for audio manager

    AudioManager audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    
    
    Button upButton = (Button) findViewById(R.id.upButton);
            upButton.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
    
                    //To increase media player volume               
                    audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
                }
            });
            
            Button downButton = (Button) findViewById(R.id.downButton);
            downButton.setOnClickListener(new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    
                    //To decrease media player volume
                    audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);             
                }
            });
    

    The above example used Button label

    for volume up and down code

    @Override
        public boolean dispatchKeyEvent(KeyEvent event) {
            int action = event.getAction();
            int keyCode = event.getKeyCode();
            switch (keyCode) {
                case KeyEvent.KEYCODE_VOLUME_UP:
                    if (action == KeyEvent.ACTION_DOWN) {
                        audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
                    }
                    return true;
                case KeyEvent.KEYCODE_VOLUME_DOWN:
                    if (action == KeyEvent.ACTION_DOWN) {
                        audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
                    }
                    return true;
                default:
                    return super.dispatchKeyEvent(event);
            }
        }