main xml
===========================================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:text="Play" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_below="@+id/button1"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<MediaController
android:id="@+id/mediaController1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</MediaController>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="44dp"
android:layout_toRightOf="@+id/button1"
android:text="Stop" />
</RelativeLayout>
=============================================
activity class :AudioRecoderActivity .java
===================================
code :
====================
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.SurfaceView;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AudioRecoderActivity extends Activity {
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
private Button startRecording ;
private Button stopRecording ;
//private Button stopRecording = null;
File video;
AudioRecorder audioRecorder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startRecording = (Button)findViewById(R.id.button1);
stopRecording= (Button)findViewById(R.id.button2);
/*recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
44100, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT, 1000);
*/
audioRecorder=new AudioRecorder();
startRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
audioRecorder.start();
} catch (IOException e) {
}
}
});
stopRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
audioRecorder.stop();
} catch (IOException e) {
}
}
});
}
}
===============================================
2 nd java class :AudioRecorder
=========================
import java.io.IOException;
import java.util.Random;
import android.media.MediaRecorder;
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
public void start() throws IOException {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
int min = 1;
int max = 999;
Random r = new Random();
int ramNum = r.nextInt(max - min + 1) + min;
recorder.setOutputFile("/mnt/sdcard/recode_"+ramNum+".3gp");
recorder.prepare();
recorder.start();
}
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
===========================================
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/button1"
android:layout_width="125dp"
android:layout_height="wrap_content"
android:text="Play" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_below="@+id/button1"
android:layout_marginTop="20dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<MediaController
android:id="@+id/mediaController1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</MediaController>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="44dp"
android:layout_toRightOf="@+id/button1"
android:text="Stop" />
</RelativeLayout>
=============================================
activity class :AudioRecoderActivity .java
===================================
code :
====================
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.View;
import android.view.SurfaceView;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AudioRecoderActivity extends Activity {
private SurfaceHolder surfaceHolder;
private SurfaceView surfaceView;
public MediaRecorder mrec = new MediaRecorder();
private Button startRecording ;
private Button stopRecording ;
//private Button stopRecording = null;
File video;
AudioRecorder audioRecorder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startRecording = (Button)findViewById(R.id.button1);
stopRecording= (Button)findViewById(R.id.button2);
/*recorder = new AudioRecord(MediaRecorder.AudioSource.MIC,
44100, AudioFormat.CHANNEL_IN_STEREO,
AudioFormat.ENCODING_PCM_16BIT, 1000);
*/
audioRecorder=new AudioRecorder();
startRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
audioRecorder.start();
} catch (IOException e) {
}
}
});
stopRecording.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
audioRecorder.stop();
} catch (IOException e) {
}
}
});
}
}
===============================================
2 nd java class :AudioRecorder
=========================
import java.io.IOException;
import java.util.Random;
import android.media.MediaRecorder;
public class AudioRecorder {
final MediaRecorder recorder = new MediaRecorder();
public void start() throws IOException {
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
int min = 1;
int max = 999;
Random r = new Random();
int ramNum = r.nextInt(max - min + 1) + min;
recorder.setOutputFile("/mnt/sdcard/recode_"+ramNum+".3gp");
recorder.prepare();
recorder.start();
}
public void stop() throws IOException {
recorder.stop();
recorder.release();
}
}
===================================================================
take two permission in menifest :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
No comments:
Post a Comment