Increase Decrease Media Player volume in android using Seekbar programmatically

How to Change android mobile phone media volume with seekbar movement using coding.

Media player volume refers here to all the media player volume apps also to inbuilt applications which play audio + video media in android mobile phones. In this tutorial we are simply modifying the android mobile device volume itself with seekbar. So here is the complete step by step tutorial for Increase Decrease Media Player volume in android using Seekbar programmatically.

android-project-download-code-button

How to Increase Decrease Media Player volume in android using Seekbar programmatically.

Code for MainActivity.java file.

 package com.increasedecreasemediaplayervolume_android_examples.com;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;


public class MainActivity extends Activity {

 SeekBar seekbar;
 TextView textview;
 AudioManager audioManager;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 seekbar = (SeekBar)findViewById(R.id.seekBar1);
 textview = (TextView)findViewById(R.id.textView1);
 
 audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
 
 seekbar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
 
 seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
 @Override
 public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
 
 textview.setText("Media Volume : " + i);
 
 audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, i, 0);
 }

 @Override
 public void onStartTrackingTouch(SeekBar seekBar) {

 }

 @Override
 public void onStopTrackingTouch(SeekBar seekBar) {

 }
 });
 
 
 }

}

Code for activity_main.xml layout file.

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:paddingBottom="@dimen/activity_vertical_margin"
 android:paddingLeft="@dimen/activity_horizontal_margin"
 android:paddingRight="@dimen/activity_horizontal_margin"
 android:paddingTop="@dimen/activity_vertical_margin"
 tools:context="com.increasedecreasemediaplayervolume_android_examples.com.MainActivity" >

 <SeekBar
 android:id="@+id/seekBar1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_alignParentLeft="true"
 android:layout_centerVertical="true" />

 <TextView
 android:id="@+id/textView1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_above="@+id/seekBar1"
 android:layout_centerHorizontal="true"
 android:layout_marginBottom="39dp"
 android:text="Move Seekbar to Set Media Player volume"
 android:gravity="center"
 android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Screenshot :

Increase Decrease Media Player volume in android using Seekbar programmatically

Click here to download Increase Decrease Media Player volume in android using Seekbar programmatically project with source code.