Android Audio Capture

Android Audio Capture

Android Audio Capture this facility provides by Android means android has built-in microphone by which you can capture audio and can save it. There are several ways to achieve that but the most frequent way is through MediaRecorder class.

Android facilitate MediaRecorder class to record audio/music or video. To be able to use MediaRecorder class ,you will generate an example of MediaRecorder class first. Its syntax is listed below

MediaRecorder myAudioRecorder = new MediaRecorder();

 

Nou now will placed the source , outcome and encoding format and result output doc. Their syntax is listed below

myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
myAudioRecorder.setOutputFile(outputFile);

 

 

After specifying the audio tracks source and format and its own output file, we can then call the two basic methods prepare and learn to start recording the audio play it in your cellphone

myAudioRecorder.prepare();
myAudioRecorder.start();

 

From these methods apart , there are other methods listed in the MediaRecorder class that allows you more control over audio tracks and video recording.

Methods & Description

setAudioSource()

This technique specifies the foundation/source of music to be recorded

setVideoSource()

This technique specifies the foundation of training video to be recorded

setOutputFormat()

This technique specifies the audio tracks format where audio tracks to be stored

setAudioEncoder()

This technique specifies the audio tracks encoder to be utilized

setOutputFile()

This technique configures the path to the record into that your recorded sound is usually to be stored

stop()

This method halts the taking process.

release()

This technique should be called when the recorder occasion is needed.

Android Audio Capture Example

This android audio capture example provides demo of MediaRecorder category to capture audio tracks and then MediaPlayer category to experience that recorded audio tracks.

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

Steps     Description

  1. You use Android studio IDE to make an Android software and name it as AudioCapture under a program com.example.sappal.myapplication;. While creating this task, ensure you Aim for SDK and Compile With at the latest version of Google android SDK to work with higher degrees of APIs.
  2. Modify src/MainActivity.java to include AudioCapture code
  3. Modify design layout res/design/activity_main.xml add any GUI part if required.
  4. Modify AndroidManifest.xml to include necessary permissions.
  5. Run the application form and choose a operating google android device and mount the application onto it and check the results.

This is actually the content of src/MainActivity.java

package com.example.sappal.myapplication;

import android.app.Activity;
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 play,stop,record;
   private MediaRecorder myAudioRecorder;
   private String outputFile = null;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      play=(Button)findViewById(R.id.button3);
      stop=(Button)findViewById(R.id.button2);
      record=(Button)findViewById(R.id.button);
      
      stop.setEnabled(false);
      play.setEnabled(false);
      outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";;
      
      myAudioRecorder=new MediaRecorder();
      myAudioRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      myAudioRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      myAudioRecorder.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
      myAudioRecorder.setOutputFile(outputFile);
      
      record.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            try {
               myAudioRecorder.prepare();
               myAudioRecorder.start();
            }
            
            catch (IllegalStateException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
            
            catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
            }
            
            record.setEnabled(false);
            stop.setEnabled(true);
            
            Toast.makeText(getApplicationContext(), "Recording started", Toast.LENGTH_LONG).show();
         }
      });
      
      stop.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            myAudioRecorder.stop();
            myAudioRecorder.release();
            myAudioRecorder  = null;
            
            stop.setEnabled(false);
            play.setEnabled(true);
            
            Toast.makeText(getApplicationContext(), "Audio recorded successfully",Toast.LENGTH_LONG).show();
         }
      });
      
      play.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException {
            MediaPlayer m = new MediaPlayer();
            
            try {
               m.setDataSource(outputFile);
            }
            
            catch (IOException e) {
               e.printStackTrace();
            }
            
            try {
               m.prepare();
            }
            
            catch (IOException e) {
               e.printStackTrace();
            }
            
            m.start();
            Toast.makeText(getApplicationContext(), "Playing audio", 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);
   }
}

 

It is the content of activity_main.xml


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed


<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="Record"
      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="Stop"
      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="play"
      android:id="@+id/button3"
      android:layout_alignTop="@+id/button2"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView" />

</RelativeLayout>

 

Here 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>

 

Here 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.audiocapture"
   android:versionCode="1"
   android:versionName="1.0" >
   
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.RECORD_AUDIO" /> 

   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.audiocapture.MainActivity"
         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 our application android audio capture app. I suppose you have linked your actual Android os Mobile device with your personal computer. To perform the app from studio, available one of your project’s activity data and click Run Eclipse Run Icon android audio capturefrom the toolbar. Prior to starting your application, Google android studio room will screen pursuing images

Now by default you will notice stop and play button disable. Just press the Record button as well as your request shall start recording the audio. It’ll display the next screen in android audio capture app.

android-audio-capture

Now click stop button and it will save the recorded music to external sd card. When you select stop button , the next screen seems

android-audio-capture

Now click the play button and and recorded music will just start playing on the device. The next message appears when you select play button

android-audio-capture


Do You Have Any Question About This Tutorial? Ask Question Now On Andro Development & Get Answer Instantly. No Signup/Login Needed