Here Sequence of Method call is as per state diagram of media Recorder .... Otherwise Invalid sate call Error will be Occur........
Steps:
1) Also her two permission add in Android manifest file
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2) This is java file coding
audiorecording.java
package com.example.recording;
import java.io.File;
import java.io.FileDescriptor;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.support.v4.app.NavUtils;
/*
* O@K Tech....
*/
public class audiorecording extends Activity implements OnClickListener {
Button start,stop,frmt;
private MediaRecorder rcorder = null;
private int opfrmt[] = {MediaRecorder.OutputFormat.MPEG_4,MediaRecorder.OutputFormat.THREE_GPP};
private int curfrmt = 0;
private String filextsn[] = {".mp4",".3gp"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recording);
start = (Button)findViewById(R.id.button1);
stop = (Button)findViewById(R.id.button2);
frmt = (Button)findViewById(R.id.button3);
start.setOnClickListener(this);
stop.setOnClickListener(this);
frmt.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_recording, menu);
return true;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
System.out.println("Recoding start");
startrecording();
break;
case R.id.button2:
System.out.println("Recoding stop");
stoprecording();
break;
case R.id.button3:
System.out.println("format check");
showDialog(1);
break;
default:
break;
}
}
private void stoprecording() {
System.out.println("stop recoding method");
if(rcorder != null)
{
rcorder.stop();
rcorder.reset();
rcorder.release();
rcorder = null;
}
}
private void startrecording() {
System.out.println("startrecoding method");
rcorder = new MediaRecorder();
rcorder.setAudioSource(MediaRecorder.AudioSource.MIC);
rcorder.setOutputFormat(opfrmt[curfrmt]);
rcorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
rcorder.setOutputFile(getfilepath());
try {
rcorder.prepare();
rcorder.start();
} catch (Exception e) {
}
}
private String getfilepath() {
System.out.println("getfile path method");
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
File file = new File(filepath, "recodingfile");
if(!file.exists())
file.mkdir();
return(file.getAbsolutePath() + "/" + System.currentTimeMillis() + filextsn[curfrmt]);
}
@Override
protected Dialog onCreateDialog(int id) {
System.out.println("oncreate dialog");
AlertDialog.Builder albuilddlg = new AlertDialog.Builder(this);
String frmt[] = {"mp4","3gp"};
albuilddlg.setTitle("Select Format.....");
albuilddlg.setSingleChoiceItems(frmt, curfrmt, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
curfrmt = which;
}
});
return albuilddlg.create();
}
}
3).Xml file
<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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Start" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button1"
android:text="Stop" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button2"
android:text="Change Format" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="138dp"
android:src="@drawable/audio" />
</RelativeLayout>
out put of this porgram
All iz well
OAK Tech.......
ya.. thanx...............next i will put another android post.. which realy use full to...
ReplyDelete