![](https://img-blog.csdn.net/20131025154856921?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYmlyZGZseTEyMzQ1/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
一.应用代码
1.录音
public class mediarecorder1 extends ListActivity
{
private Button mAudioStartBtn;
private Button mAudioStopBtn;
private File mRecAudioFile; // 录制的音频文件
private File mRecAudioPath; // 录制的音频文件路徑
private MediaRecorder mMediaRecorder;// MediaRecorder对象
private List<String> mMusicList = new ArrayList<String>();// 录音文件列表
private String strTempFile = "recaudio_";// 零时文件的前缀
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mymultimedia_mediarecorder1);
mAudioStartBtn = (Button) findViewById(R.id.mediarecorder1_AudioStartBtn);
mAudioStopBtn = (Button) findViewById(R.id.mediarecorder1_AudioStopBtn);
/*按钮状态*/
mAudioStartBtn.setEnabled(true);
mAudioStopBtn.setEnabled(false);
/* 检测是否存在SD卡 */
if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
{
mRecAudioPath = Environment.getExternalStorageDirectory();// 得到SD卡得路径
musicList();// 更新所有录音文件到List中
} else
{
Toast.makeText(mediarecorder1.this, "没有SD卡", Toast.LENGTH_LONG).show();
}
/* 开始按钮事件监听 */
mAudioStartBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
try
{
/* ①Initial:实例化MediaRecorder对象 */
mMediaRecorder = new MediaRecorder();
/* ②setAudioSource/setVedioSource*/
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置麦克风
/* ②设置输出文件的格式:THREE_GPP/MPEG-4/RAW_AMR/Default
* THREE_GPP(3gp格式,H263视频/ARM音频编码)、MPEG-4、RAW_AMR(只支持音频且音频编码要求为AMR_NB)
* */
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
/* ②设置音频文件的编码:AAC/AMR_NB/AMR_MB/Default */
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
/* ②设置输出文件的路径 */
try
{
mRecAudioFile = File.createTempFile(strTempFile, ".amr", mRecAudioPath);
} catch (Exception e)
{
e.printStackTrace();
}
mMediaRecorder.setOutputFile(mRecAudioFile.getAbsolutePath());
/* ③准备 */
mMediaRecorder.prepare();
/* ④开始 */
mMediaRecorder.start();
/*按钮状态*/
mAudioStartBtn.setEnabled(false);
mAudioStopBtn.setEnabled(true);
} catch (IOException e)
{
e.printStackTrace();
}
}
});
/* 停止按钮事件监听 */
mAudioStopBtn.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
if (mRecAudioFile != null)
{
/* ⑤停止录音 */
mMediaRecorder.stop();
/* 将录音文件添加到List中 */
mMusicList.add(mRecAudioFile.getName());
ArrayAdapter<String> musicList = new ArrayAdapter<String>(mediarecorder1.this,
R.layout.list, mMusicList);
setListAdapter(musicList);
/* ⑥释放MediaRecorder */
mMediaRecorder.release();
mMediaRecorder = null;
/* 按钮状态 */
mAudioStartBtn.setEnabled(true);
mAudioStopBtn.setEnabled(false);
}
}
});
}
/* 播放录音文件 */
private void playMusic(File file)
{
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
/* 设置文件类型 */
intent.setDataAndType(Uri.fromFile(file), "audio");
startActivity(intent);
}
@Override
/* 当我们点击列表时,播放被点击的音乐 */
protected void onListItemClick(ListView l, View v, int position, long id)
{
/* 得到被点击的文件 */
File playfile = new File(mRecAudioPath.getAbsolutePath() + File.separator
+ mMusicList.get(position));
/* 播放 */
playMusic(playfile);
}
/* 播放列表 */
public void musicList()
{
// 取得指定位置的文件设置显示到播放列表
File home = mRecAudioPath;
if (home.listFiles(new MusicFilter()).length > 0)
{
for (File file : home.listFiles(new MusicFilter()))
{
mMusicList.add(file.getName());
}
ArrayAdapter<String> musicList = new ArrayAdapter<String>(mediarecorder1.this,
R.layout.list, mMusicList);
setListAdapter(musicList);
}
}
}
/* 过滤文件类型 */
class MusicFilter implements FilenameFilter
{
public boolean accept(File dir, String name)
{
return (name.endsWith(".amr"));
}
}
2.录制视频
public class TestBasicVideo extends Activity implements SurfaceHolder.Callback {
private Button start;// 开始录制按钮
private Button stop;// 停止录制按钮
private MediaRecorder mediarecorder;// 录制视频的类
private SurfaceView surfaceview;// 显示视频的控件
// 用来显示视频的一个接口,我靠不用还不行,也就是说用mediarecorder录制视频还得给个界面看
// 想偷偷录视频的同学可以考虑别的办法。。嗯需要实现这个接口的Callback接口
private SurfaceHolder surfaceHolder;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏
// 设置横屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// 选择支持半透明模式,在有surfaceview的activity中使用。
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
init();
}
private void init() {
start = (Button) this.findViewById(R.id.start);
stop = (Button) this.findViewById(R.id.stop);
start.setOnClickListener(new TestVideoListener());
stop.setOnClickListener(new TestVideoListener());
surfaceview = (SurfaceView) this.findViewById(R.id.surfaceview);
SurfaceHolder holder = surfaceview.getHolder();// 取得holder
holder.addCallback(this); // holder加入回调接口
// setType必须设置,要不出错.
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
class TestVideoListener implements OnClickListener {
@Override
public void onClick(View v) {
if (v == start) {
mediarecorder = new MediaRecorder();// 创建mediarecorder对象
// 设置录制视频源为Camera(相机)
mediarecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 设置录制完成后视频的封装格式THREE_GPP为3gp.MPEG_4为mp4
mediarecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置录制的视频编码h263 h264
mediarecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
// 设置视频录制的分辨率。必须放在设置编码和格式的后面,否则报错
mediarecorder.setVideoSize(176, 144);
// 设置录制的视频帧率。必须放在设置编码和格式的后面,否则报错
mediarecorder.setVideoFrameRate(20);
mediarecorder.setPreviewDisplay(surfaceHolder.getSurface());
// 设置视频文件输出的路径
mediarecorder.setOutputFile("/sdcard/love.3gp");
try {
// 准备录制
mediarecorder.prepare();
// 开始录制
mediarecorder.start();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (v == stop) {
if (mediarecorder != null) {
// 停止录制
mediarecorder.stop();
// 释放资源
mediarecorder.release();
mediarecorder = null;
}
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder
surfaceHolder = holder;
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder
surfaceHolder = holder;
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// surfaceDestroyed的时候同时对象设置为null
surfaceview = null;
surfaceHolder = null;
mediarecorder = null;
}
}
二:框架介绍
参考博客:Android的MediaRecorder架构介绍