在Android中通过android.media.MeidaRecoder类实现音频和视频文件的录制操作。
MediaRecorder 的生命周期包括以下几种状态:
1、 Initial 状态 :当用户通过 MediaRecorder 类的构造方法实例化 MediaRecorder 类对象时将处于初始化状态,即便此时没有任何操作, MediaRecorder 也会占用系统资源,而所有的状态都可以通过 reset() 方法返回到此状态。
2、 Initialized 状态 :当用户使用 setAudioSource() 或 setVideoSource() 方法后将进入音频录制或视频录制状态,并可以指定一些音频或视频的文件属性,设置完成之后将进入 DataSourceConfigured 状态。
3、 Prepared 状态 :就绪状态,当用户使用 MediaRecorder 类中的 prepare() 方法时进入就绪状态,表示录制前的状态已经准备就绪。
4、 Recoding 状态 :但用户使用 MediaRecorder 类中的 start() 方法时,将进入录制状态,并且一直持续到录音或录像操作完毕。
下面使用 MediaRecorder 完成一个音频录制以及音频文件列表
列表显示布局 --recordaudios.xml
audiomain.xml
AudioRecorderActivity.java
package com.iflytek.demo; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; 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.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageButton; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; public class AudioRecorderActivity extends Activity { private ImageButton recordBtn = null; private ImageButton stopBtn = null; private TextView infoTxt = null;// 提示信息 private ListView reclist = null;// 定义列表视图 private SimpleAdapter recordSimpleAdapter = null;// 用于定义ListView组件的适配器 private MediaRecorder mediaRecorder = null;// MediaRecorder对象 private boolean sdcardExists = false; // 判断sd卡是否存在 private File recordAudioSaveFileDir = null; // 保存所有音频文件的文件夹 private File recordAudioSaveFile = null; // 每次保存音频文件的名称 private String recordAudioSaveFileName = null; // 每次保存音频文件的名称 private String recDir = "xdwangAudio"; // 文件保存的目录路径 private boolean isRecord = false; // 判断是否正在录音 private List
AndroidManifest.xml
录制视频与录制音频文件的功能类似,唯一的区别就是在视频录制中需要定义一个额外的SurfaceView组件,以捕获摄像头采集到的数据。
范例:视频录制、列表显示、视频播放
recordvideo.xml
VideoRecorderActivity.java
package com.iflytek.demo; import java.io.File; import java.util.List; import android.app.Activity; import android.content.Intent; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.view.KeyEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.ImageButton; import android.widget.TextView; public class VideoRecorderActivity extends Activity { private ImageButton recordBtn = null; private ImageButton stopBtn = null; private ImageButton browserBtn = null; private TextView infoTxt = null; private MediaRecorder mediaRecorder = null; private File recordVideoSaveFile = null;// 文件保存目录路径 private File recordVideoSaveFileDir = null;// 文件保存目录 private String recordVideoSaveFileName = null;// 音频文件保存名称 private String recDir = "xdwangvideo";// 保存目录 private boolean sdcardExists = false; // SD卡存在的标记 private boolean isRecord = false;// 判断是否正在录音 private SurfaceView surfaceView = null; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题 super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏 super.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 高亮的风格显示 super.setContentView(R.layout.recordvideo); this.recordBtn = (ImageButton) super.findViewById(R.id.record); this.stopBtn = (ImageButton) super.findViewById(R.id.stop); this.browserBtn = (ImageButton) super.findViewById(R.id.browser); this.infoTxt = (TextView) super.findViewById(R.id.info); this.surfaceView = (SurfaceView) super.findViewById(R.id.surface); this.surfaceView.getHolder().setType( SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);//设置缓冲类型 this.surfaceView.getHolder().setFixedSize(480, 800);//设置分辨率 if ((this.sdcardExists = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED))) {//判断sd卡是否存在 this.recordVideoSaveFileDir = new File(Environment .getExternalStorageDirectory().toString() + File.separator + VideoRecorderActivity.this.recDir + File.separator); // 保存录音目录 if (!this.recordVideoSaveFileDir.exists()) { this.recordVideoSaveFileDir.mkdirs(); // 创建新文件夹 } } this.recordBtn.setOnClickListener(new RecordOnClickListenerImpl()); this.stopBtn.setOnClickListener(new StopOnClickListenerImpl()); this.browserBtn .setOnClickListener(new BrowserOnClickListenerImpl()); this.stopBtn.setEnabled(false); // 停止录象的按钮暂时不可用 } /** * * @author xdwang * * @create 2012-11-15 下午09:25:32 * * @email:[email protected] * * @description 视频录制 * */ private class RecordOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { if (VideoRecorderActivity.this.sdcardExists) { // sd卡存在 VideoRecorderActivity.this.recordVideoSaveFileName = VideoRecorderActivity.this.recordVideoSaveFileDir .toString() + File.separator + "XdwangVideo_" + System.currentTimeMillis() + ".3gp";// 文件的路径名称 VideoRecorderActivity.this.recordVideoSaveFile = new File( VideoRecorderActivity.this.recordVideoSaveFileName);// 文件路径 VideoRecorderActivity.this.mediaRecorder = new MediaRecorder(); VideoRecorderActivity.this.mediaRecorder.reset(); // 重置 VideoRecorderActivity.this.mediaRecorder .setVideoSource(MediaRecorder.VideoSource.CAMERA);// 设置视频源,从摄像头来 VideoRecorderActivity.this.mediaRecorder .setAudioSource(MediaRecorder.AudioSource.MIC);// 设置音频源,表示从麦克风进来 VideoRecorderActivity.this.mediaRecorder .setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);// 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4 VideoRecorderActivity.this.mediaRecorder .setVideoEncoder(MediaRecorder.VideoEncoder.H264);// 设置录制的视频编码h263 // h264,注意这里如果写H263会出现错误 VideoRecorderActivity.this.mediaRecorder .setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);// 设置音频的编码 VideoRecorderActivity.this.mediaRecorder .setOutputFile(VideoRecorderActivity.this.recordVideoSaveFileName);// 设置视频保存路径 Camera camera = Camera.open(0); // 取得第一个摄像头,可能存在多个,0表示后面的,1表示前置摄像头 Parameters parameters = camera.getParameters(); ListpreviewSizes = parameters .getSupportedPreviewSizes(); Camera.Size pictureSize = previewSizes.get(0); VideoRecorderActivity.this.mediaRecorder.setVideoSize( pictureSize.width, pictureSize.height);// 视频分辨率 // WindowManager wm = (WindowManager) // getSystemService(Context.WINDOW_SERVICE); // // 获取窗口服务 // Display display = wm.getDefaultDisplay();// 获取屏幕信息 // VideoRecorderActivity.this.mediaRecorder.setVideoSize( // display.getWidth(), display.getHeight());// 视频分辨率 VideoRecorderActivity.this.mediaRecorder.setVideoFrameRate(10);// 设置每秒为10帧 // 必须放在设置编码和格式的后面,否则报错 VideoRecorderActivity.this.mediaRecorder .setPreviewDisplay(VideoRecorderActivity.this.surfaceView .getHolder().getSurface());// 定义视频显示 try { VideoRecorderActivity.this.mediaRecorder.prepare();// 准备录像 } catch (Exception e) { Log.i("VideoRecorderActivity", e.toString()); } VideoRecorderActivity.this.mediaRecorder.start();// 开始录像 VideoRecorderActivity.this.infoTxt.setText("正在录象中..."); VideoRecorderActivity.this.stopBtn.setEnabled(true); VideoRecorderActivity.this.recordBtn.setEnabled(false); VideoRecorderActivity.this.isRecord = true;// 正在录像 } } } /** * * @author xdwang * * @create 2012-11-15 下午09:30:23 * * @email:[email protected] * * @description 停止录制视频 * */ private class StopOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { if (VideoRecorderActivity.this.isRecord) { VideoRecorderActivity.this.mediaRecorder.stop(); VideoRecorderActivity.this.mediaRecorder.release(); VideoRecorderActivity.this.stopBtn.setEnabled(false); VideoRecorderActivity.this.recordBtn.setEnabled(true); VideoRecorderActivity.this.infoTxt.setText("录象结束,文件路径为:" + VideoRecorderActivity.this.recordVideoSaveFile); } } } /** * * @author xdwang * * @create 2012-11-15 下午09:36:36 * * @email:[email protected] * * @description Activity跳转 * */ private class BrowserOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { Intent intent = new Intent(VideoRecorderActivity.this, BroswerActivity.class); VideoRecorderActivity.this.startActivity(intent); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { super.finish(); } return false; } }
recordfiles.xml(BroswerActivity 程序进行ListView显示)
broswer.xml(BroswerActivity布局)
BroswerActivity.java
package com.iflytek.demo; import java.io.File; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ImageButton; import android.widget.ListView; import android.widget.SimpleAdapter; /** * @author xdwang * * @create 2012-11-15 下午10:15:22 * * @email:[email protected] * * @description 视频文件浏览的Activity操作,提供ListView显示 * */ public class BroswerActivity extends Activity { private ImageButton backBtn = null; private ListView videolist = null;// 定义列表视图 private SimpleAdapter recordSimpleAdapter = null;// 适配器 private List> recordFiles = null;// 保存所有的List数据 private String recDir = "xdwangvideo";// 保存目录 private File recordVideoSaveFileDir = null;// 文件保存目录 private boolean sdcardExists = false;// sd卡是否存在 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题 super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 高亮的风格显示 super.setContentView(R.layout.broswer); if ((this.sdcardExists = Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED))) { this.recordVideoSaveFileDir = new File(Environment .getExternalStorageDirectory().toString() + File.separator + BroswerActivity.this.recDir + File.separator); if (!this.recordVideoSaveFileDir.exists()) { this.recordVideoSaveFileDir.mkdirs(); } } this.backBtn = (ImageButton) super.findViewById(R.id.back); this.videolist = (ListView) super.findViewById(R.id.videolist); this.getRecordFiles();// 取得全部的文件列表 this.videolist.setOnItemClickListener(new OnItemClickListenerImpl()); this.backBtn.setOnClickListener(new BackOnClickListenerImpl()); } /** * @descrption 加载给定目录中的全部文件 * @author xdwang * @create 2012-11-15下午10:26:55 */ private void getRecordFiles() {// 取得全部录音文件 this.recordFiles = new ArrayList >(); if (this.sdcardExists) { File files[] = this.recordVideoSaveFileDir.listFiles(); for (int x = 0; x < files.length; x++) { Map fileInfo = new HashMap (); fileInfo.put("filename", files[x].getName());// 增加显示内容 this.recordFiles.add(fileInfo);// 保存数据 } this.recordSimpleAdapter = new SimpleAdapter(this, this.recordFiles, R.layout.recordfiles, new String[] { "filename" }, new int[] { R.id.filename });// 实例化适配器 this.videolist.setAdapter(this.recordSimpleAdapter);// 定义列表视图 } } /** * * @author xdwang * * @create 2012-11-15 下午10:37:23 * * @email:[email protected] * * @description 返回操作 * */ private class BackOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { Intent it = new Intent(BroswerActivity.this, VideoRecorderActivity.class); BroswerActivity.this.startActivity(it); } } /** * * @author xdwang * * @create 2012-11-15 下午10:43:41 * * @email:[email protected] * * @description 单击视频选项进行播放 * */ private class OnItemClickListenerImpl implements OnItemClickListener { @Override public void onItemClick(AdapterView> adapter, View view, int position, long id) {// 选项单击 if (BroswerActivity.this.recordSimpleAdapter.getItem(position) instanceof Map) {// 判断是否是Map实例 Map, ?> map = (Map, ?>) BroswerActivity.this.recordSimpleAdapter .getItem(position);// 取得指定位置的内容 Intent intent = new Intent(BroswerActivity.this, PlayVideoActivity.class);// 指定Intent intent.putExtra("filepath", BroswerActivity.this.recordVideoSaveFileDir.toString() + File.separator + map.get("filename").toString()); BroswerActivity.this.startActivity(intent);// 启动Activity } } } }
play.xml
PlayVideoActivity.java
package com.iflytek.demo; import android.app.Activity; import android.content.Intent; import android.media.AudioManager; import android.media.MediaPlayer; import android.os.Bundle; import android.view.KeyEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.ImageButton; /** * @author xdwang * * @create 2012-11-15 下午10:50:57 * * @email:[email protected] * * @description 播放视频的Activity,使用Mediaplayer完成播放 * */ public class PlayVideoActivity extends Activity { private ImageButton playBtn = null; private ImageButton stopBtn = null; private ImageButton backBtn = null; private MediaPlayer mediaPlayer = null; private SurfaceView sufaceView = null; private SurfaceHolder surfaceHolder = null; private String filepath = null;// 播放文件路径 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.requestWindowFeature(Window.FEATURE_NO_TITLE); // 不显示标题 super.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); super.getWindow().addFlags( WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // 高亮的风格显示 super.setContentView(R.layout.play); this.filepath = super.getIntent().getStringExtra("filepath"); this.playBtn = (ImageButton) super.findViewById(R.id.play); this.stopBtn = (ImageButton) super.findViewById(R.id.stop); this.backBtn = (ImageButton) super.findViewById(R.id.back); this.sufaceView = (SurfaceView) super.findViewById(R.id.surfaceView); this.surfaceHolder = this.sufaceView.getHolder(); this.surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);// 设置SurfaceView类型 this.mediaPlayer = new MediaPlayer(); this.mediaPlayer.reset();// 重置操作 try { this.mediaPlayer.setDataSource(this.filepath);// 设置播放文件的路径 } catch (Exception e) { } this.playBtn.setOnClickListener(new PlayOnClickListenerImpl()); this.stopBtn.setOnClickListener(new StopOnClickListenerImpl()); this.backBtn.setOnClickListener(new BackOnClickListenerImpl()); } /** * * @author xdwang * * @create 2012-11-14 下午10:57:56 * * @email:[email protected] * * @description 视频播放 * */ private class PlayOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { PlayVideoActivity.this.mediaPlayer .setAudioStreamType(AudioManager.STREAM_MUSIC);// 设置音频类型 PlayVideoActivity.this.mediaPlayer .setDisplay(PlayVideoActivity.this.surfaceHolder);// 设置显示区域 try { PlayVideoActivity.this.mediaPlayer.prepare();// 预备状态 PlayVideoActivity.this.mediaPlayer.start();// 播放视频 } catch (Exception e) { } } } /** * * @author xdwang * * @create 2012-11-15 下午10:59:06 * * @email:[email protected] * * @description 暂停视频 * */ private class StopOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { PlayVideoActivity.this.mediaPlayer.stop();// 停止播放 } } /** * * @author xdwang * * @create 2012-11-15下午11:03:16 * * @email:[email protected] * * @description 返回 * */ private class BackOnClickListenerImpl implements OnClickListener { @Override public void onClick(View v) { Intent it = new Intent(PlayVideoActivity.this, BroswerActivity.class); PlayVideoActivity.this.startActivity(it); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { this.mediaPlayer.stop(); this.mediaPlayer.release(); super.finish(); } return false; } }
AndroidManifest.xml