This video shows the steps to create a volume controller App for your Android Phone. This video first creates a simple layout with volume up and down button and uses a seek-bar to show the current level of the volume.
For any query, suggestions or appreciations, we will be glad to hear from you at: programmerworld1990@gmail.com
Source Code:
package com.example.myvolumecontroller;
import android.content.Context;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.View;
import android.widget.SeekBar;
import android.widget.Toast;import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private SeekBar seekBar;
private AudioManager audioManager;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);seekBar = findViewById(R.id.seekBar);
audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
seekBar.setMax(audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC));
seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
}public void UpButton(View view){
audioManager.adjustVolume(AudioManager.ADJUST_RAISE, AudioManager.FLAG_PLAY_SOUND);
seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
Toast.makeText(this,”Volume Up”, Toast.LENGTH_SHORT).show();
}public void DownButton(View view){
audioManager.adjustVolume(AudioManager.ADJUST_LOWER, AudioManager.FLAG_PLAY_SOUND);
seekBar.setProgress(audioManager.getStreamVolume(AudioManager.STREAM_MUSIC));
Toast.makeText(this,”Volume Down”, Toast.LENGTH_SHORT).show();
}
}
<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.myvolumecontroller”><application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” /><category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application></manifest>
<?xml version=”1.0″ encoding=”utf-8″?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”><Button
android:id=”@+id/button2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”160dp”
android:layout_marginTop=”250dp”
android:layout_marginEnd=”163dp”
android:onClick=”DownButton”
android:text=”@string/down”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintHorizontal_bias=”1.0″
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /><Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”160dp”
android:layout_marginTop=”136dp”
android:onClick=”UpButton”
android:text=”@string/up”
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /><SeekBar
android:id=”@+id/seekBar”
android:layout_width=”300dp”
android:layout_height=”0dp”
android:layout_marginStart=”41dp”
android:layout_marginTop=”150dp”
android:layout_marginEnd=”70dp”
app:layout_constraintEnd_toEndOf=”parent”
app:layout_constraintHorizontal_bias=”0.0″
app:layout_constraintStart_toStartOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/button2″ /></androidx.constraintlayout.widget.ConstraintLayout>
Didn’t understand. Could you please help by elaborating a bit here?
Cheers
Programmer World
–