我只需要从前置摄像头录制视频,我搜索了很多,但无法找到解决方案(简单)
如果我将相机类型设置为 1,则应用程序崩溃..
这是我的代码
import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
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.Button;
import android.widget.Toast;
public class VideoCapture extends Activity implements OnClickListener, SurfaceHolder.Callback {
MediaRecorder recorder;
SurfaceHolder holder;
boolean recording = false;
String pathVideo;
private int cameraType = 1;
@Override
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); */
pathVideo = "/reg_" + System.currentTimeMillis() + ".mp4";
recorder = new MediaRecorder();
initRecorder();
setContentView(R.layout.camera);
SurfaceView cameraView = (SurfaceView) findViewById(R.id.surface_camera);
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
Button rec =(Button) findViewById(R.id.buttonstart);
rec.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
if (recording) {
recorder.stop();
recording = false;
recorder.release();
// Let's initRecorder so we can record again
initRecorder();
prepareRecorder();
} else {
recording = true;
recorder.start();
}
}
});
}
private void initRecorder() {
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
File Directory = new File("/sdcard/CantaTu/");
// have the object build the directory structure, if needed.
Directory.mkdirs();
File mediaFile = new File(Directory,pathVideo);
if(mediaFile.exists()){
mediaFile.delete();
}
recorder.setOutputFile(mediaFile.getAbsolutePath());
recorder.setMaxDuration(400000); // 50 seconds
recorder.setMaxFileSize(50000000); // Approximately 5 megabytes
}
private void prepareRecorder() {
recorder.setPreviewDisplay(holder.getSurface());
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
public void surfaceCreated(SurfaceHolder holder) {
//camera = Camera.open(cameraType);
prepareRecorder();
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
if (recording) {
recorder.stop();
recording = false;
recorder.release();
}
recorder.release();
//finish();
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
有没有办法简单地记录到前置摄像头?谢谢!
编辑
这是我的实际代码(如果我尝试打开前置摄像头,它就不起作用)
public void inizializzazione(){
cameraView.setVisibility(0);
boolean found = false;
int i;
for(i=0; i< Camera.getNumberOfCameras(); i++){
System.out.println("camera n " +i);
Camera.CameraInfo newInfo = new Camera.CameraInfo();
Camera. getCameraInfo(i, newInfo);
if (newInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
try {
found = true;
cam = Camera.open(i);
System.out.println("trovata la fotocamera frontale");
} catch (RuntimeException e) {
Log.e("Your_TAG", "Camera failed to open: " + e.getLocalizedMessage());
}
}
pathVideo = "/reg_" + System.currentTimeMillis() + ".mp4";
File Directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/CantaTu/");
// have the object build the directory structure, if needed.
Directory.mkdirs();
File mediaFile = new File(Directory,pathVideo);
if(mediaFile.exists()){
mediaFile.delete();
}
recorder = new MediaRecorder();
holder = cameraView.getHolder();
holder.addCallback(this);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(1);
CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile(mediaFile.getAbsolutePath());
recorder.setMaxDuration(400000); // 50 seconds
recorder.setPreviewDisplay(holder.getSurface());
try {
if(found == true){
recorder.setCamera(cam);
}
recorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
recorder.start();
scritta.setVisibility(0);
caricamento.setVisibility(4);
}
}
我不知道为什么它不打开前置摄像头...错误是“在无效状态下开始调用”(因为它找到了前置摄像头,并尝试设置)