Android Mediarecorder使用前置摄像头

时间:2018-05-02 23:00:23

标签: android android-camera mediarecorder

我正在开发一款应该使用前置摄像头录制视频的应用。现在正在使用后置摄像头。当我尝试更改cameraID以选择前置摄像头的配置文件时,如

中所述

this question

实际上没有任何反应,仍然使用了后置摄像头。

cmd2

清单文件:

public class MainActivity extends Activity  implements View.OnClickListener, SurfaceHolder.Callback {
    public static MediaRecorder recorder;
    public static SurfaceHolder holder;
    public static SurfaceView cameraView;
    boolean recording = false;
    public static final int RequestPermissionCode = 2;

    @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);

        if(checkPermission()) {
            initRecorder();
            setContentView(R.layout.activity_main);

            cameraView = (SurfaceView) findViewById(R.id.CameraView);
            holder = cameraView.getHolder();
            holder.addCallback(this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            cameraView.setClickable(true);
            cameraView.setOnClickListener(this);
        } else {
            requestPermission();
            initRecorder();
            setContentView(R.layout.activity_main);

            cameraView = (SurfaceView) findViewById(R.id.CameraView);
            holder = cameraView.getHolder();
            holder.addCallback(this);
            holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

            cameraView.setClickable(true);
            cameraView.setOnClickListener(this);
        }
    }

    private  int getCamID() {
        int cameraCount = 0;
        Camera cam = null;
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        cameraCount = Camera.getNumberOfCameras();
        for (int camIdx = 0; camIdx<cameraCount; camIdx++) {
            Camera.getCameraInfo(camIdx, cameraInfo);
            if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
                return camIdx;
            }
        }
        return 0;
    }

    private void initRecorder() {
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        int camID = getCamID();
        CamcorderProfile cpHigh = CamcorderProfile
                .get(camID,CamcorderProfile.QUALITY_LOW);
        recorder.setProfile(cpHigh);

        //get the time stamp
        String timestamp = new Timestamp(System.currentTimeMillis()).toString();
        //remove the seconds part
        timestamp = timestamp.substring(0, timestamp.length()-6).replaceAll(":","");
        //append file name to the time stamp
        String filestamp =  timestamp;
        recorder.setOutputFile(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
                filestamp +".mp4");
        recorder.setMaxDuration(50000); // 50 seconds
        recorder.setMaxFileSize(5000000); // 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 onClick(View v) {
        if (recording) {
            recorder.stop();
            recording = false;
            recorder.release();
            // Let's initRecorder so we can record again

        } else {
            initRecorder();
            prepareRecorder();
            recording = true;
            recorder.start();
        }
    }

    public void surfaceCreated(SurfaceHolder holder) {
        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();
        finish();
    }



    private void requestPermission() {
        ActivityCompat.requestPermissions(MainActivity.this, new
                String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO,CAMERA}, RequestPermissionCode);
    }


    public boolean checkPermission() {
        int result = ContextCompat.checkSelfPermission(getApplicationContext(),
                WRITE_EXTERNAL_STORAGE);
        int result1 = ContextCompat.checkSelfPermission(getApplicationContext(),
                RECORD_AUDIO);
        int result2 = ContextCompat.checkSelfPermission(getApplicationContext(),
                CAMERA);
        return result == PackageManager.PERMISSION_GRANTED &&
                result1 == PackageManager.PERMISSION_GRANTED &&
                result2 == PackageManager.PERMISSION_GRANTED;
    }
}

1 个答案:

答案 0 :(得分:0)

MediaRecorder定义setCamera()方法easy to use,但在API 21中已弃用(因为它引用旧的Camera API)。对于API 21+,最好使用camera2 API打开相机并使用setVideoSource(SURFACE),如官方camera2video sample所示。