从前置摄像头Android录制

时间:2017-09-06 08:18:22

标签: android camera android-camera surfaceview record

我使用此代码从前置摄像头录制,但是当摄像头开始录制时,录制器更改为后置摄像头(表面视图显示前置摄像头预览),这是我的代码:

public class MatchActivity extends Activity implements SurfaceHolder.Callback {

    private static final String LOG_TAG = MainActivity.class.getCanonicalName();
    View myButton;
    MediaRecorder mediaRecorder;
    SurfaceHolder surfaceHolder;
    boolean recording;
    private TextView mTimer;
    private Camera mCamera;
    TextView prepareid;
    CountDownTimer countDownTimer;
    String pathOfVideo;
    TextView textmode;
    ImageView imagemode;
    boolean isRunning = false;
    String PIC;
    int ID;

    operations op = new operations();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_match);
        SurfaceView myVideoView = (SurfaceView) findViewById(R.id.surface);
        op.Localize();
        recording = false;
        mCamera = Camera.open(1);
        mCamera.setDisplayOrientation(90);
        mCamera.unlock();
        mediaRecorder = new MediaRecorder();
        mediaRecorder.setCamera(mCamera);

        pathOfVideo=initMediaRecorder();
        surfaceHolder = myVideoView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder = myVideoView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


        myButton = (View) findViewById(R.id.Prepare);
        myButton.setOnClickListener(StartVideo);

        ImageView CorecctAnswer = (ImageView) findViewById(R.id.correctanswer);
        CorecctAnswer.setOnClickListener(CorrectAnswer);


        prepareid = (TextView) findViewById(R.id.prepareid);
        mTimer = (TextView) findViewById(R.id.timer);
        if (ContextCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MatchActivity.this, new String[]{android.Manifest.permission.CAMERA}, 50);
        }

    }
    private Button.OnClickListener StartVideo
            = new Button.OnClickListener() {
        @Override
        public void onClick(final View arg0) {
            myButton.setVisibility(View.GONE);
            prepareid.setVisibility(View.GONE);

            countDownTimer = new CountDownTimer(60 * 1000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                    mTimer.setText(String.valueOf(millisUntilFinished / 1000)+ " ثانیه ");
                    isRunning = true;
                }
                @Override
                public void onFinish() {
                    isRunning= false;
                    if(camerastate)
                        stopvideo();
                    Intent intent = new Intent(MatchActivity.this, ResultPage.class);
                    Bundle b = new Bundle();
                    b.putString("PIC",PIC);
                    b.putString("PATH", pathOfVideo);
                    b.putString("Word", textmode.getText().toString());
                    b.putInt("Answer",0);
                    b.putInt("ID",ID);
                    intent.putExtras(b);
                    startActivity(intent);
                    finish();
                }
            };
            startvideo();
        }
    };
    public void stopvideo()
    {
        recording=false;
        try {

            mediaRecorder.stop();
            mediaRecorder.reset();

            finish();
        } catch (Exception e) {
            Log.e(LOG_TAG, "Failed to stop recorder", e);
        }
        mediaRecorder.release();
        mediaRecorder = null;
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
    }
    public void startvideo()
    {
        mCamera.release();
        countDownTimer.start();
        mediaRecorder.start();
        recording = true;
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        if (mCamera != null) {
            Camera.Parameters params = mCamera.getParameters();
            mCamera.setParameters(params);
            try {
                mCamera.setPreviewDisplay(holder);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mCamera.startPreview();
        }
        prepareMediaRecorder();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {

    }
    private String initMediaRecorder() {

        mediaRecorder.setCamera(mCamera);
        mediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
        CamcorderProfile camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);  //get your own profile
        Camera.Parameters parameters = null;
        if (mCamera.getParameters()!=null) {
            parameters = mCamera.getParameters();
        }
        parameters.setPreviewSize(camcorderProfile.videoFrameWidth,camcorderProfile.videoFrameHeight);
        mCamera.setParameters(parameters);
        File file = new File(getExternalFilesDir(Environment.DIRECTORY_MOVIES),"hadskalme" + System.currentTimeMillis()+ ".mp4");
        if (file.exists()) {
            file.delete();
        }
        mediaRecorder.setOutputFile(file.getAbsolutePath());

        mediaRecorder.setMaxDuration(90000); // Set max duration 90 sec.
        mediaRecorder.setMaxFileSize(15000000); // Set max file size 15M

        return String.valueOf(file.getAbsolutePath());
    }

    private void prepareMediaRecorder() {

        mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());

        try {
            mediaRecorder.prepare();
        } catch (IllegalStateException | IOException e) {
            Log.e(LOG_TAG, "  to prepare recorder", e);
        }
    }
}

我收到了什么错误:

  

W / AudioSystem:AudioFlinger服务器死了!   W / IMediaDeathNotifier:媒体服务器死了    E / MediaPlayer:错误(100,0)   W / MediaMetadataRetriever:MediaMetadataRetriever服务器死了!    W / CameraBase:相机服务死了!

1 个答案:

答案 0 :(得分:0)

public static Camera openFrontalCamera() {
   Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
   for (int i = 0; i < Camera.getNumberOfCameras(); i++) {
       Camera.getCameraInfo(i, cameraInfo);
       if (Camera.CameraInfo.CAMERA_FACING_FRONT == cameraInfo.facing) {
           return Camera.open(i);
       }
   }
   return null;
}

代码中的用法

mCamera = openFrontalCamera();
if (mCamera != null) {
    //continue flow
} else {
    //no frontal camera available
}