MediaRecorder录制声音并放入sd中显示出来

1.代码:
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Display;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

public class Test extends Activity {
/** Called when the activity is first created. */
private ListView mListView = null;
private Button btn_start = null;
private Button btn_stop = null;
private MediaRecorder mMediaRecorder = null;
private List<String> rec = new ArrayList<String>();// ���¼���ļ�
private File home = null;
private File path = null;
private String temp = "recaudio_";// ��ʱ�ļ�ǰ׺

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) this.findViewById(R.id.ListView01);
btn_start = (Button) this.findViewById(R.id.Button01);
btn_stop = (Button) this.findViewById(R.id.Button02);
// �Ƿ����SD��
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
home = Environment.getExternalStorageDirectory();
MusicList();
} else {
Toast.makeText(this, "���Ȳ���SD��", Toast.LENGTH_LONG).show();
return;
}
btn_start.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
try {
// ����¼����ʱ�ļ�
path = File.createTempFile(temp, ".3gp", home);
setTitle("==" + path.getAbsolutePath());
mMediaRecorder = new MediaRecorder();
mMediaRecorder
.setAudioSource(MediaRecorder.AudioSource.MIC);// ���������Դ����˷�
mMediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);// ���ø�ʽ
mMediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setOutputFile(path.getAbsolutePath());// ��������ļ�·��
mMediaRecorder.prepare();
mMediaRecorder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

});
btn_stop.setOnClickListener(new Button.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mMediaRecorder.stop();
mMediaRecorder.release();
mMediaRecorder = null;
MusicList();
}

});
mListView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String path = home + File.separator + rec.get(arg2);
File f = new File(path);
PlayMusic(f);
}

});
}

/**
* ��ʾ�б�
*/
public void MusicList() {
File[] f = home.listFiles(new MusicFilter());
rec.clear();
for (int i = 0; i < f.length; i++) {
File file = f[i];
rec.add(file.getName());
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, rec);
mListView.setAdapter(adapter);
}

/**
* ����¼���ļ�
*
* @param file
*/
public void PlayMusic(File file) {
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "audio");
this.startActivity(intent);
}

class MusicFilter implements FilenameFilter {

public boolean accept(File dir, String filename) {
// TODO Auto-generated method stub
return (filename.endsWith(".3gp"));
}

}
}
2.main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"  
>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"
android:layout_weight="1"
android:id="@+id/Button01"
>
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结束"
android:layout_weight="1"
android:id="@+id/Button02" >
</Button>
</LinearLayout>
<ListView android:layout_height="wrap_content"
android:id="@+id/ListView01"
android:layout_width="fill_parent">
</ListView>
</LinearLayout>

你可能感兴趣的:(xml,android,.net,OS,F#)