Pages

Friday, May 6, 2016

Android – Audio Capture Tutorial


MainActivity.java
package kantapp.android_audiocapturetutorial;

import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends AppCompatActivity
{
    Button play,record,stop;
    MediaRecorder audioRecord;
    String outputFile=null;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        play=(Button)findViewById(R.id.play);
        record=(Button)findViewById(R.id.record);
        stop=(Button)findViewById(R.id.stop);

        play.setEnabled(false);
        stop.setEnabled(false);

        outputFile= Environment.getExternalStorageDirectory().getAbsolutePath()+"/recording.3gp";

        audioRecord=new MediaRecorder();
        audioRecord.setAudioSource(MediaRecorder.AudioSource.MIC);
        audioRecord.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        audioRecord.setAudioEncoder(MediaRecorder.OutputFormat.AMR_NB);
        audioRecord.setOutputFile(outputFile);

        record.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {

                try
                {
                    audioRecord.prepare();
                    audioRecord.start();
                }
                catch (IllegalStateException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

                record.setEnabled(false);
                stop.setEnabled(true);

                Toast.makeText(MainActivity.this, "Recording Starting...", Toast.LENGTH_SHORT).show();
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                audioRecord.stop();
                audioRecord.release();
                audioRecord=null;

                stop.setEnabled(false);
                play.setEnabled(true);

                Toast.makeText(MainActivity.this, "Audio Record Succesfully", Toast.LENGTH_SHORT).show();

            }
        });

        play.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v) throws IllegalArgumentException,SecurityException,IllegalStateException
            {
                MediaPlayer  mediaPlayer=new MediaPlayer();
                try
                {
                    mediaPlayer.setDataSource(outputFile);
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }

                try
                {
                    mediaPlayer.prepare();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                mediaPlayer.start();

            }
        });

        Button exit=(Button)findViewById(R.id.exit);
        exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context="kantapp.android_audiocapturetutorial.MainActivity">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@mipmap/ic_launcher"
        android:layout_gravity="center_horizontal" />

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Play"
            android:id="@+id/play"
            android:layout_gravity="left|top" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Record"
            android:id="@+id/record"
            android:layout_gravity="center_horizontal|top" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Stop"
            android:id="@+id/stop"
            android:layout_gravity="right|top" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Exit"
            android:id="@+id/exit"
            android:layout_gravity="center" />
    </FrameLayout>
</LinearLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="kantapp.android_audiocapturetutorial">
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        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>

No comments:

Post a Comment