In this android example i show you, how to set a ringing volume in android with seekbar by using audio manager.
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<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=".MainActivity" >
<TextView
android:id="@+id/tVVolume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:text="Volume: " />
<SeekBar
android:id="@+id/sbVolume"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp" />
</RelativeLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.volumecontrollapp;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//define TextViews to show details of volume and brightness
private TextView tVVolume;
// define SeekBars to set volume and brightness
private SeekBar sbVolume;
private AudioManager audioManager;
int maxVolume = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Suggests an audio stream whose volume should be changed with
// hardware volume controls.
setVolumeControlStream(AudioManager.STREAM_RING);
initializeControls();
}
private void initializeControls() {
// get reference of the UI Controls
sbVolume = (SeekBar) findViewById(R.id.sbVolume);
tVVolume = (TextView) findViewById(R.id.tVVolume);
try {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// set max progress according to volume
sbVolume.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_RING));
// get current volume
sbVolume.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_RING));
// Set the seek bar progress to 1
sbVolume.setKeyProgressIncrement(1);
// get max volume
maxVolume = sbVolume.getMax();
sbVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_RING,
progress, 0);
// Calculate the volume percentage
float perc = (progress / (float) maxVolume) * 100;
// Set the volume percentage
tVVolume.setText("Volume: " + (int) perc + " %");
}
});
} catch (Exception e) {
}
}
}
NOTE: Give permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Step 3: Run Project:
CREATE NEW ANDROID PROJECT
Step 1: Write code into activity_main.xml
<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=".MainActivity" >
<TextView
android:id="@+id/tVVolume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:text="Volume: " />
<SeekBar
android:id="@+id/sbVolume"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="150dp" />
</RelativeLayout>
Step 2: Write code into MainActivity.java
package dev.androidapplink.volumecontrollapp;
import android.media.AudioManager;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
//define TextViews to show details of volume and brightness
private TextView tVVolume;
// define SeekBars to set volume and brightness
private SeekBar sbVolume;
private AudioManager audioManager;
int maxVolume = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Suggests an audio stream whose volume should be changed with
// hardware volume controls.
setVolumeControlStream(AudioManager.STREAM_RING);
initializeControls();
}
private void initializeControls() {
// get reference of the UI Controls
sbVolume = (SeekBar) findViewById(R.id.sbVolume);
tVVolume = (TextView) findViewById(R.id.tVVolume);
try {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
// set max progress according to volume
sbVolume.setMax(audioManager
.getStreamMaxVolume(AudioManager.STREAM_RING));
// get current volume
sbVolume.setProgress(audioManager
.getStreamVolume(AudioManager.STREAM_RING));
// Set the seek bar progress to 1
sbVolume.setKeyProgressIncrement(1);
// get max volume
maxVolume = sbVolume.getMax();
sbVolume.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
audioManager.setStreamVolume(AudioManager.STREAM_RING,
progress, 0);
// Calculate the volume percentage
float perc = (progress / (float) maxVolume) * 100;
// Set the volume percentage
tVVolume.setText("Volume: " + (int) perc + " %");
}
});
} catch (Exception e) {
}
}
}
NOTE: Give permission in AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Step 3: Run Project:
No comments :
Post a Comment