今天在做一个简单的录音机的时候,录音机老是无法正常实例化,不能使用,后来查找问题,一个一个的试,最后确定了,原来是两个设置写反了,却造成了,无法正常工作,真是郁闷啊。找了一个教程看了一下,教程中也没有提,这两个不能写反,就当给自己一个警告吧,也希望看到的人,不知道这个问题的人能记住。下面把代码放到下面:
package com.RecordTest;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.app.ListActivity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Toast;
public class RecordTest extends ListActivity {
Button btn1, btn2;
int currertindex = 0;
private MediaRecorder mediarecorder; // 创建录音机
File recordFile = null;
File sdcardPath = null;// 录音文件目录
String Tempfile = "Record_temp_";
List<String> fileList = new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
sdcardPath = Environment.getExternalStorageDirectory();
setTitle(sdcardPath.getName());
} else {
Toast.makeText(RecordTest.this, "没有SD卡", 1000).show();
}
getRecordList();
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
recordFile = File.createTempFile(Tempfile, ".amr",
sdcardPath);
Log.i("aaaaa", "1");
mediarecorder = new MediaRecorder();
Log.i("aaaaa", "2");
mediarecorder.setAudioSource(MediaRecorder.AudioSource.MIC); // 设置麦克风
Log.i("aaaaa", "3");
mediarecorder
.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT); //设置输出文件格式
Log.i("aaaaa", "4");
mediarecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT); // 设置编码格式
Log.i("aaaaa", "5");
mediarecorder.setOutputFile(recordFile.getAbsolutePath()); // 使用绝对路径进行保存文件
Log.i("aaaaa", "6");
mediarecorder.prepare();
mediarecorder.start();
Toast.makeText(RecordTest.this, "开始", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if (recordFile != null) {
mediarecorder.stop();
fileList.add(recordFile.getName());
ArrayAdapter<String> list = new
ArrayAdapter<String>(RecordTest.this,
R.layout.itemlist, R.id.listitem, fileList);
setListAdapter(list);
mediarecorder = null;
Toast.makeText(RecordTest.this, "停止", Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Log.i("eeeee", e.getMessage());
}
}
});
}
void getRecordList() {
// 首先检测是否存在SDCard
try {
File home = sdcardPath;
fileList.clear();
if (home.list(new Fileter()).length > 0) {
for (File file : home.listFiles(new Fileter())) {
fileList.add(file.getName());
}
ArrayAdapter<String> list = new ArrayAdapter<String>(this,
R.layout.itemlist, R.id.listitem, fileList);
setListAdapter(list);
}
} catch (Exception e) {
Toast.makeText(RecordTest.this, e.getMessage(), 1000).show();
}
}
class Fileter implements FilenameFilter {
public boolean accept(File dir, String filename) {
// TODO Auto-generated method stub
return filename.equals(".amr");
}
}
}
在使用过程中,需要加入操作SD卡和使用录音设备的权限,即在AndroidManifest.xml中加入: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
然后就可以使用了,如果有问题,欢迎大家指正!