需要权限
<!-- 调用摄像头权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- 录制视频/音频权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- sd卡读写权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<!-- 挂载sd卡使用现有应用程序的Android摄像头应用程序 -->
自定义代码
package com.example.record_video_text;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SurfaceView;
import android.view.View;
public class MainActivity extends Activity {
SurfaceView SurfaceView_text;
private MediaRecorder mediaRecorder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SurfaceView_text = (SurfaceView) findViewById(R.id.SurfaceView_text);
}
public void Record(View v) {
// 1获取MediaRecorder对象
mediaRecorder = new MediaRecorder();
Camera open = Camera.open();
open.setDisplayOrientation(90);
open.unlock();//解锁
mediaRecorder.setCamera(open);
// 2重复
mediaRecorder.reset();
// 3设置音视频资源的来源
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//音频
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//视频
// 4设置录制视频的格式
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 5配置
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置音频的编码
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);//设置视频的编码格式
// 6设置储存到手机文件
File file = new File(Environment.getExternalStorageDirectory().getPath()+"/shiping");
//创建目录
if(!file.exists()){
file.mkdirs();
}
File file2 = new File(file.getPath()+"/"+System.currentTimeMillis()+".3gp");
try {
file2.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
mediaRecorder.setOutputFile(file2.getPath());
mediaRecorder.setVideoFrameRate(20);//设置每秒播放几帧
// 7设置预览
mediaRecorder.setPreviewDisplay(SurfaceView_text.getHolder().getSurface());
// 8准备播放
try {
mediaRecorder.prepare();
mediaRecorder.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void stop(View v) {
if(mediaRecorder!=null){
mediaRecorder.stop();
mediaRecorder.release();
}
}
}
2系统录像
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
file = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".3gp");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
startActivityForResult(intent, 110);
返回
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 110) {
SurfaceView_text.setVideoPath(file.getPath());
SurfaceView_text.setMediaController(new MediaController(this));
SurfaceView_text.start();
}
}
设置视频清晰度
mediaRecorder.setVideoSize(640, 480);// 设置分辨率:
mediaRecorder.setVideoEncodingBitRate(510241024);// 设置帧频率,然后就清晰了