媒体录制类:android.media.MediaRecoder类,可以实现音频和视频文件的录制。生命周期:
a.Initial状态:当用户通过MediaRecoder类的构造方法实例化MediaRecoder类对象时出于初始化状态,即便此时没有任何操作,MediaRecoder也会占用系统资源,而所有的状态都可以通过reset()方法返回到此状态。
b.Initialized状态:当用户使用setAudioSource()或setVideoSource()方法后将进入音频录制或视频录制状态,并可以指定一些音频或视频的文件属性,设置完成后将进入DataSourceConfigured状态。
c.Prepared状态:就绪状态。当用户使用MediaRecoder类中的prepare()方法时将进入就绪状态,表示录制前的状态已经准备就绪。
d.Recording状态:录制状态。当用户使用MediaRecoder类中的start()方法时将进入录制状态,并且一直持续到录音或录像操作完毕。
代码:
(1)定义列表显示布局管理器----recordfiles.xml
<?xmlversion="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<ImageView
android:id="@+id/icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/file_icon"/>
<TextView
android:id="@+id/filename"
android:textSize="16px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
(2)main.xml
<?xmlversion="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
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageButton
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/record"/>
<ImageButton
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/stop"/>
</LinearLayout>
<TextView
android:id="@+id/info"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="文字提示信息..." />
<ListView
android:id="@+id/reclist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
(3)主程序:
publicclass MyMediaRecorderDemo extends Activity {
private ImageButton record = null;
private ImageButton stop = null;
private TextView info = null;
private ListView reclist = null;//定义列表视图
private SimpleAdapter recordSimpleAdapter =null;//适配器
private MediaRecorder mediaRecorder = null;
private boolean sdcardExists = false; // 判断sd卡是否存在
private File recordAudioSaveFileDir = null;// 保存所有音频文件的文件夹
private File recordAudioSaveFile = null; // 每次保存音频文件的名称
private String recordAudioSaveFileName =null; // 每次保存音频文件的名称
private String recDir = "mldnrec";// 保存的目录名称
private boolean isRecord = false ; // 录音的标志
private List<Map<String,Object>>recordFiles = null ;
@Override
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.main);
this.record = (ImageButton)super.findViewById(R.id.record);
this.stop = (ImageButton)super.findViewById(R.id.stop);
this.info = (TextView) super.findViewById(R.id.info);
this.reclist = (ListView)super.findViewById(R.id.reclist);
// 如果存在则将状态给了sdcardExists属性
if ((this.sdcardExists =Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED))) {// 判断sd卡是否存在
this.recordAudioSaveFileDir = newFile(Environment
.getExternalStorageDirectory().toString()
+File.separator
+MyMediaRecorderDemo.this.recDir + File.separator);//保存录音文件
if(!this.recordAudioSaveFileDir.exists()) { // 文件夹不存在
this.recordAudioSaveFileDir.mkdirs();// 创建文件夹
}
}
this.stop.setEnabled(false) ; // 暂停按钮现在不可用
this.record.setOnClickListener(newRecordOnClickListenerImpl());
this.stop.setOnClickListener(newStopOnClickListenerImpl());
this.reclist.setOnItemClickListener(newOnItemClickListenerImpl()) ;
this.getRecordFiles() ;
}
private class RecordOnClickListenerImplimplements OnClickListener {
@Override
public void onClick(View v) {
if(MyMediaRecorderDemo.this.sdcardExists){ // 如果sd卡存在
MyMediaRecorderDemo.this.recordAudioSaveFileName= MyMediaRecorderDemo.this.recordAudioSaveFileDir
.toString()
+ File.separator
+"MLDNRecord_"
+System.currentTimeMillis() + ".3gp"; //每次的录音文件名称都不一样
MyMediaRecorderDemo.this.recordAudioSaveFile= new File(
MyMediaRecorderDemo.this.recordAudioSaveFileName);//取得保存路径
MyMediaRecorderDemo.this.mediaRecorder= new MediaRecorder(); // 实例化对象
// 在进行录制之前必须配置若干个参数
MyMediaRecorderDemo.this.mediaRecorder
.setAudioSource(MediaRecorder.AudioSource.MIC);// 音频来源是MIC
MyMediaRecorderDemo.this.mediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//定义输出格式
MyMediaRecorderDemo.this.mediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);//定义音频编码
MyMediaRecorderDemo.this.mediaRecorder .setOutputFile(MyMediaRecorderDemo.this.recordAudioSaveFileName);//定义输出文件
try { // 进入到就绪状态
MyMediaRecorderDemo.this.mediaRecorder.prepare();
} catch (Exception e) {
//Log.i("MyMediaRecorderDemo", e.toString()) ;
}
MyMediaRecorderDemo.this.mediaRecorder.start(); // 开始录音
MyMediaRecorderDemo.this.info.setText("正在录音中...") ;
MyMediaRecorderDemo.this.stop.setEnabled(true);// 停止录音按钮可以使用了
MyMediaRecorderDemo.this.record.setEnabled(false);//录音按钮禁用
MyMediaRecorderDemo.this.isRecord= true ; // 正在录音
}
}
}
private class StopOnClickListenerImplimplements OnClickListener {
@Override
public void onClick(View v) {
if(MyMediaRecorderDemo.this.isRecord){ // 正在录音
MyMediaRecorderDemo.this.mediaRecorder.stop(); // 停止
MyMediaRecorderDemo.this.mediaRecorder.release(); // 释放资源
MyMediaRecorderDemo.this.record.setEnabled(true);//录音按钮启用
MyMediaRecorderDemo.this.stop.setEnabled(false);//
MyMediaRecorderDemo.this.info.setText("录音结束,文件路径为:"
+MyMediaRecorderDemo.this.recordAudioSaveFileName);
MyMediaRecorderDemo.this.getRecordFiles();//重新加载列表
}
}
}
private void getRecordFiles(){ // 将一个文件夹之中的全部文件列出
this.recordFiles = newArrayList<Map<String, Object>>();
if(this.sdcardExists) { // 有sd卡存在
File files [] =this.recordAudioSaveFileDir.listFiles() ; //列出目录中的文件
for (int x = 0; x < files.length;x++) {
Map<String, Object>fileInfo = new HashMap<String, Object>();
fileInfo.put("filename",files[x].getName()) ;
this.recordFiles.add(fileInfo) ;//保存数据
}
this.recordSimpleAdapter = newSimpleAdapter(this,
this.recordFiles,R.layout.recordfiles,
new String[] {"filename" }, new int[] { R.id.filename });
this.reclist.setAdapter(this.recordSimpleAdapter);//定义列表视图
}
}
private class OnItemClickListenerImplimplements OnItemClickListener {
@Override
public voidonItemClick(AdapterView<?> parent, View view, int position,
long id) {
if(MyMediaRecorderDemo.this.recordSimpleAdapter.getItem(position) instanceof Map){
Map<?, ?> map = (Map<?,?>) MyMediaRecorderDemo.this.recordSimpleAdapter
.getItem(position);//取出指定位置的内容
Uri uri = Uri
.fromFile(newFile(MyMediaRecorderDemo.this.recordAudioSaveFileDir
.toString()
+ File.separator
+map.get("filename")));//定义操作的Uri
Intent intent = newIntent(Intent.ACTION_VIEW) ; //指定Action
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//增加标记
intent.setDataAndType(uri,"audio/mp3") ;//设置数据播放的MIME
MyMediaRecorderDemo.this.startActivity(intent);//穷Activity
}
}
}
}