Wednesday, June 12, 2013

Audio Recording Example in Android

ScreenShots:

strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Audio Capture</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>
    <string name="startBtn">Start Recording</string>
    <string name="stopBtn">Stop Recording</string>
    <string name="playBtn">Play </string>

</resources>

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

    <Button
        android:id="@+id/button_startRecordBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="24dp"
        android:text="@string/startBtn" />

    <Button
        android:id="@+id/button_stopRecordBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_startRecordBtn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="70dp"
        android:text="@string/stopBtn" />

    <Button
        android:id="@+id/button_playBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_stopRecordBtn"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:text="@string/playBtn" />

</RelativeLayout>


MainActivity.java:
package com.ram.audiocapture;

import java.io.IOException;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener {
private Button startRecBtn, stopRecBtn, playBtn;
private MediaRecorder recorder = null;
String FILE_PATH_NAME = null;
private MediaPlayer m = null;

public MainActivity() {
FILE_PATH_NAME = Environment.getExternalStorageDirectory()
.getAbsolutePath();
FILE_PATH_NAME += "/recordsample.3gp";

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

startRecBtn = (Button) findViewById(R.id.button_startRecordBtn);

stopRecBtn = (Button) findViewById(R.id.button_stopRecordBtn);

playBtn = (Button) findViewById(R.id.button_playBtn);

startRecBtn.setOnClickListener(this);
stopRecBtn.setOnClickListener(this);
playBtn.setOnClickListener(this);
}

@Override
public void onClick(View v) {

switch (v.getId()) {
case R.id.button_startRecordBtn:

recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(FILE_PATH_NAME);
try {
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.start();
break;

case R.id.button_stopRecordBtn:
recorder.stop();
recorder.reset();  
recorder.release();
recorder = null;
break;
case R.id.button_playBtn:

m = new MediaPlayer();
try {
m.setDataSource(FILE_PATH_NAME);
m.prepare();

} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

m.start();

break;
}
}

@Override
protected void onPause() {
if (recorder != null) {
recorder.release();
recorder = null;
}
if (m != null) {
m.release();
m = null;
}

super.onPause();
}

}

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
 <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.ram.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>




Audio Recording Example in Android Rating: 4.5 Diposkan Oleh: Unknown

0 comments:

Post a Comment