Android Audio Manager

Android Audio Manager

Android Audio Manager with the help of this You can certainly control your audio volume level and audio account i-e:(vibrate,silent, e.t.c) in google android. Android Audio Manager Android provides that category that provides usage of these controls.

To be able to use AndroidManager course, you have to first create an thing of Android Audio Manager category by dialling the getSystemService() method. Its syntax is listed below.

private AudioManager myAudioManager;
myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE); 

 

When you instantiate the object of Android Audio Manager, you can use setRingerMode method to set the audio tracks or ringer profile of your device. Its syntax is listed below.

myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);

 

An integer is used by the technique setRingerMode amount as a parameter. For every mode , an integer number is assigned that will differentiate between different modes. The possible methods are.

Mode & Descriptions

RINGER_Mode_VIBRATE

The mode is defined by this Function at vibrate setting.

RINGER_Mode_NORMAL

This Mode models these devices at normal mode.

RINGER_Mode_SILENT

The mode is defined by this Method at silent function.

Once you’ve arranged the function , you can call the getRingerMode() solution to get the placed state of the machine. Its syntax is listed below.

int mod = myAudioManager.getRingerMode();

 

In the getRingerMode method aside, there are other methods available in the Android Audio Manager category to control the quantity and other settings. They below are listed.

Methods & Descriptions

adjustVolume(int route, int flags)

This method changes the volume of the very most relevant stream

getMode()

This method results the current audio mode

getStreamMaxVolume(int streamType)

This method comes back the maximum volume level index for a specific stream

getStreamVolume(int streamType)

This method results the current amount index for a specific stream

isMusicActive()

This method bank checks whether any music is dynamic.

startBluetoothSco()

This technique Start Bluetooth SCO audio tracks connection

stopBluetoothSco()

This technique stop Bluetooth SCO music connection.

Android Audio Manager Example

The below android audio manager example shows the utilization of AudioManager class or category. It crates a program which allows you to create different ringer settings for your device.

To test out this example , you will need to perform this on a genuine device.

Steps     Description

  1. You use Android studio IDE to generate an Android request under a program com.example.sappal.myapplication; While creating this job, ensure you Aim for SDK and Compile With at the latest version of Android os SDK to utilize higher degrees of APIs.
  2. Modify src/MainActivity.java to include all android audio manger syntax
  3. Modify design layout res/design/activity_main.xml add any GUI part if required.
  4. Modify res/beliefs/string.xml and add necessary string components.
  5. Modify AndroidManifest.xml to include necessary permissions.
  6. Run the application form and choose a operating google android device and set up the application onto it and validate the results.

This is actually the content of src/MainActivity.java

package com.example.sappal.myapplication;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;

import android.os.Bundle;
import android.os.Environment;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;

import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.IOException;


public class MainActivity extends Activity {
   Button mode,ring,vibrate,silent;
   private AudioManager myAudioManager;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      vibrate=(Button)findViewById(R.id.button3);
      ring=(Button)findViewById(R.id.button2);
      
      mode=(Button)findViewById(R.id.button);
      silent=(Button)findViewById(R.id.button4);
      myAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
      
      vibrate.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
            Toast.makeText(MainActivity.this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
         }
      });
      
      ring.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
            Toast.makeText(MainActivity.this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
         }
      });
      
      silent.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            Toast.makeText(MainActivity.this,"Now in silent Mode",Toast.LENGTH_LONG).show();
         }
      });
      
      mode.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            int mod=myAudioManager.getRingerMode();
            
            if(mod==AudioManager.RINGER_MODE_VIBRATE){
               Toast.makeText(MainActivity.this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
            }
            
            else if(mod==AudioManager.RINGER_MODE_NORMAL){
               Toast.makeText(MainActivity.this,"Now in Ringing Mode",Toast.LENGTH_LONG).show();
            }
            
            else
            {
               Toast.makeText(MainActivity.this,"Now in Vibrate Mode",Toast.LENGTH_LONG).show();
            }
         }
      });
   }
   
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
   }
   
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      
      int id = item.getItemId();
      
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
         return true;
      }
      return super.onOptionsItemSelected(item);
   }
}

Below is the content of 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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Android Audio Recording"
      android:id="@+id/textView"
      android:textSize="30dp"
      android:layout_alignParentTop="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" />
      
   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Andro Development"
      android:id="@+id/textView2"
      android:textColor="#ff3eff0f"
      android:textSize="35dp"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true" />
      
   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/logo"
      android:layout_below="@+id/textView2"
      android:layout_alignLeft="@+id/textView2"
      android:layout_alignStart="@+id/textView2"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Mode"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_marginTop="59dp" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Ring"
      android:id="@+id/button2"
      android:layout_alignTop="@+id/button"
      android:layout_centerHorizontal="true" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="vibrate"
      android:id="@+id/button3"
      android:layout_alignTop="@+id/button2"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Silent"
      android:id="@+id/button4"
      android:layout_below="@+id/button2"
      android:layout_alignLeft="@+id/button2"
      android:layout_alignStart="@+id/button2" />
      
</RelativeLayout>

It is the content of Strings.xml

<resources>
   <string name="app_name">My Application</string>
   <string name="hello_world">Hello world!</string>
   <string name="action_settings">Settings</string>
</resources>

It is the content of AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.audiomanager"
   android:versionCode="1"
   android:versionName="1.0" >

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.sappal.myapplication"
         android:label="@string/app_name" >
      
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      
      </activity>
      
   </application>
</manifest>

 

Let’s make an effort to run the android audio manger application. I suppose you have linked your actual Google android Mobile device with your personal computer. To perform the app from Android Studio, open up one of your project’s activity data files and click Run Eclipse Run icon Android Audio Manager from the tool. Android studio room shall screen Images

Select silent button now, you’ll get silent icon at Notification bar

Android Audio Manager

Now just choose the ring button and then click the existing setting button to observe that if its position has been placed.

Android Audio Manager

Now click the Vibrate button and then press the existing method button to note that if it’s arranged or not.It shall screen the next display screen.android-audio-manger