-->

Sunday, November 19, 2017

How To Make Audio Control For Beginner Android Studio

Baca Juga

Making a simple app to control audio phone with android studio-stormpedia.blogspot.co.id-Hey guys , this time i'll teach you how to make a simple app to control audio sound with Android Studio.First of all you must have Android Studio any version , (better with Android Studio 3.0).If you don't have android studio , you can download manually in this link , Download Android Studio.

If you already have it , please see and read this tutorial until finish.So let's make it.

First Start a new Android Studio Project -> Next -> *select API Level (recomended higher API Level)* -> Next -> Choose Your Activity -> Your App Name -> Finish (actually i don't care about this step to create a new project).

Then , write this xml code to configure your layout in activity_main.xml

<RelativeLayout 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="com.example.asus.myapplication.MainActivity">
    <SeekBar        android:id="@+id/ringtone"        android:layout_width="300dp"        android:layout_height="wrap_content"        android:layout_alignParentStart="true"        android:layout_alignParentTop="true"        android:layout_marginStart="48dp"        android:layout_marginTop="56dp" />
    <TextView        android:id="@+id/textView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignStart="@+id/ringtone"        android:layout_below="@+id/ringtone"        android:layout_marginTop="16dp"        android:text="Ringtone Sound" />
    <SeekBar        android:id="@+id/music"        android:layout_width="300dp"        android:layout_height="wrap_content"        android:layout_alignStart="@+id/textView"        android:layout_below="@+id/textView"        android:layout_marginTop="14dp"        android:max="10" />
    <TextView        android:id="@+id/textView2"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignStart="@+id/music"        android:layout_below="@+id/music"        android:layout_marginTop="19dp"        android:text="Music Sound" />
    <SeekBar        android:id="@+id/notification"        android:layout_width="300dp"        android:layout_height="wrap_content"        android:layout_alignStart="@+id/textView2"        android:layout_below="@+id/textView2"        android:layout_marginTop="33dp" />
    <TextView        android:id="@+id/textView3"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignStart="@+id/notification"        android:layout_centerVertical="true"        android:text="Notification Sound" />
    <SeekBar        android:id="@+id/alarm"        android:layout_width="300dp"        android:layout_height="wrap_content"        android:layout_alignStart="@+id/textView3"        android:layout_below="@+id/textView3"        android:layout_marginTop="22dp" />
    <TextView        android:id="@+id/textView4"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignStart="@+id/alarm"        android:layout_below="@+id/alarm"        android:layout_marginTop="24dp"        android:text="Alarm Sound" /></RelativeLayout>

and the result for the xml code is below :

Add this code above onCreate function :
SeekBar ringtone,music,notification,alarm;
AudioManager manager;

If success , and then write this code in MainActivity.java after onCreate function :

    /*This Code For Controlling Volume By SeekBar When The User Slide SeekBar*/
    private void initControls(SeekBar bar,final int stream){
        bar.setMax(manager.getStreamMaxVolume(stream));
        bar.setProgress(manager.getStreamVolume(stream));
        bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                manager.setStreamVolume(stream,i,AudioManager.FLAG_PLAY_SOUND);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
 If you don't know why you have to write above code , it's function void to control stream volume from what kind of volume by SeekBar number.After you write that function , write this code after 'setContentView(R.layout.activity_main)'

ringtone = (SeekBar)findViewById(R.id.ringtone);
music = (SeekBar)findViewById(R.id.music);
notification = (SeekBar)findViewById(R.id.music);
alarm = (SeekBar)findViewById(R.id.alarm);
initControls(ringtone,AudioManager.STREAM_RING);
initControls(music,AudioManager.STREAM_MUSIC);
initControls(notification,AudioManager.STREAM_NOTIFICATION);
initControls(alarm,AudioManager.STREAM_ALARM);
 And Then click 'Run' Or Symbol Like Play Button , and then select your device to Run Your App.And Finally your App can control your sound phone.


EmoticonEmoticon