关于自动录制视频的功能
我想实现打开app就马上录制视频,但是运行的时候总会出现
E/MediaRecorderJNI﹕ Application lost the surface
12-16 15:49:43.659 28394-28394/com.jinglingtec.ijiazu E/MyRcoder﹕ invalid preview surface
12-16 15:49:43.749 28394-28394/com.jinglingtec.ijiazu E/MediaRecorderJNI﹕ Application lost the surface
12-16 15:49:43.749 28394-28394/com.jinglingtec.ijiazu E/MyRcoder﹕ invalid preview surface
的错误;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 去掉标题栏 ,必须放在setContentView之前
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.video_record);
// 设置横屏显示
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// 设置全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 选择支持半透明模式,在有surfaceview的activity中使用。
getWindow().setFormat(PixelFormat.TRANSLUCENT);
// 获取程序界面中的SurfaceView
sView = (SurfaceView) this.findViewById(R.id.sView);
// 设置分辨率
sView.getHolder().setFixedSize(1280, 720);
// 设置该组件让屏幕不会自动关闭
sView.getHolder().setKeepScreenOn(true);
try
{
// 创建保存录制视频的视频文件
videoFile = new File(Environment
.getExternalStorageDirectory()
.getCanonicalFile() + "/testvideo.3gp");
String str = videoFile.getPath();
Log.e("MyRcoder", str);
// 创建MediaPlayer对象
mRecorder = new MediaRecorder();
mRecorder.reset();
// 设置从麦克风采集声音(或来自录像机的声音AudioSource.CAMCORDER)
mRecorder.setAudioSource(MediaRecorder
.AudioSource.MIC);
// 设置从摄像头采集图像
mRecorder.setVideoSource(MediaRecorder
.VideoSource.CAMERA);
// 设置视频文件的输出格式
// 必须在设置声音编码格式、图像编码格式之前设置
mRecorder.setOutputFormat(MediaRecorder
.OutputFormat.THREE_GPP);
// 设置声音编码的格式
mRecorder.setAudioEncoder(MediaRecorder
.AudioEncoder.AMR_NB);
// 设置图像编码的格式
mRecorder.setVideoEncoder(MediaRecorder
.VideoEncoder.H264);
mRecorder.setVideoSize(1280, 720);
// 每秒 4帧
mRecorder.setVideoFrameRate(20);
mRecorder.setOutputFile(videoFile.getAbsolutePath());
// 指定使用SurfaceView来预览视频
mRecorder.setPreviewDisplay(sView
.getHolder().getSurface()); //①
mRecorder.prepare();
// 开始录制
mRecorder.start();
}
catch (Exception e)
{
Log.e("MyRcoder", e.getMessage());
}
}