Example usage for android.media CamcorderProfile QUALITY_TIME_LAPSE_CIF

List of usage examples for android.media CamcorderProfile QUALITY_TIME_LAPSE_CIF

Introduction

In this page you can find the example usage for android.media CamcorderProfile QUALITY_TIME_LAPSE_CIF.

Prototype

int QUALITY_TIME_LAPSE_CIF

To view the source code for android.media CamcorderProfile QUALITY_TIME_LAPSE_CIF.

Click Source Link

Document

Time lapse quality level corresponding to the cif (352 x 288) resolution.

Usage

From source file:com.almalence.plugins.capture.video.VideoCapturePlugin.java

private void prepareMediaRecorder() {
    mMediaRecorder = CameraController.getMediaRecorder();
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(ApplicationScreen.getMainContext());

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH && videoStabilization)
        CameraController.setVideoStabilization(true);

    CameraController.unlockCamera();/*from  w  ww. j a  va2s. c  o  m*/

    // Step 2: Set sources
    if (!CameraController.isUseCamera2()) {
        CameraController.configureMediaRecorder(mMediaRecorder);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    } else {
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    }

    if (ApplicationScreen.isMicrophonePermissionGranted())
        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);

    int quality = Integer.parseInt(
            prefs.getString(CameraController.getCameraIndex() == 0 ? ApplicationScreen.sImageSizeVideoBackPref
                    : ApplicationScreen.sImageSizeVideoFrontPref, DEFAULT_VIDEO_QUALITY));

    if (maxQuality()) {
        quality = CamcorderProfile.QUALITY_HIGH;
    }

    boolean useProfile = true;
    if (!CamcorderProfile.hasProfile(CameraController.getCameraIndex(), quality))
        useProfile = false;

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    try {
        try {
            if (swChecked) {
                int qualityTimeLapse = quality;
                // if time lapse activated
                switch (quality) {
                case CamcorderProfile.QUALITY_QCIF:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_QCIF;
                    break;
                case CamcorderProfile.QUALITY_CIF:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_CIF;
                    break;
                case CamcorderProfile.QUALITY_2160P:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_2160P;
                    break;
                case CamcorderProfile.QUALITY_1080P:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_1080P;
                    break;
                case CamcorderProfile.QUALITY_720P:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_720P;
                    break;
                case CamcorderProfile.QUALITY_480P:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_480P;
                    break;
                case QUALITY_4K:
                    quality = QUALITY_4K;
                    break;
                case CamcorderProfile.QUALITY_HIGH:
                    quality = CamcorderProfile.QUALITY_TIME_LAPSE_HIGH;
                    break;
                default:
                    break;
                }
                if (!CamcorderProfile.hasProfile(CameraController.getCameraIndex(), quality)) {
                    Toast.makeText(ApplicationScreen.instance, "Time lapse not supported", Toast.LENGTH_LONG)
                            .show();
                } else
                    quality = qualityTimeLapse;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Video", "Time lapse error catched" + e.getMessage());
            swChecked = false;
        }

        lastUseProfile = useProfile;
        if (useProfile) {
            CamcorderProfile pr = CamcorderProfile.get(CameraController.getCameraIndex(), quality);
            if (ApplicationScreen.isMicrophonePermissionGranted()) {
                mMediaRecorder.setProfile(pr);
            } else {
                // If we don't have access to microphone, then configure only video settings of MediaREcorder.
                mMediaRecorder.setOutputFormat(pr.fileFormat);
                mMediaRecorder.setVideoEncoder(pr.videoCodec);
                mMediaRecorder.setVideoSize(pr.videoFrameWidth, pr.videoFrameHeight);
                mMediaRecorder.setVideoFrameRate(pr.videoFrameRate);
                mMediaRecorder.setVideoEncodingBitRate(pr.videoBitRate);
            }
            lastCamcorderProfile = pr;
        } else {
            boolean useProf = false;
            lastUseProf = useProf;
            CameraController.Size sz = null;
            switch (quality) {
            case CamcorderProfile.QUALITY_QCIF:
                sz = new CameraController.Size(176, 144);
                break;
            case CamcorderProfile.QUALITY_CIF:
                sz = new CameraController.Size(352, 288);
                break;
            case CamcorderProfile.QUALITY_480P:
                sz = new CameraController.Size(640, 480);
                break;
            case CamcorderProfile.QUALITY_720P:
                sz = new CameraController.Size(1280, 720);
                break;
            case CamcorderProfile.QUALITY_1080P:
                sz = new CameraController.Size(1920, 1080);
                break;
            case CamcorderProfile.QUALITY_2160P: {
                if (CamcorderProfile.hasProfile(CameraController.getCameraIndex(),
                        CamcorderProfile.QUALITY_2160P))
                    sz = new CameraController.Size(3840, 2160);
                else {
                    CamcorderProfile prof = CamcorderProfile.get(CameraController.getCameraIndex(),
                            CamcorderProfile.QUALITY_HIGH);
                    prof.videoFrameWidth = 3840;
                    prof.videoFrameHeight = 2160;
                    prof.videoBitRate = (int) (prof.videoBitRate * 2.8); // need a higher bitrate for the better quality - this is roughly based on the bitrate used by an S5's native camera app at 4K (47.6 Mbps, compared to 16.9 Mbps which is what's returned by the QUALITY_HIGH profile)
                    if (ApplicationScreen.isMicrophonePermissionGranted()) {
                        mMediaRecorder.setProfile(prof);
                    } else {
                        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                        mMediaRecorder.setVideoSize(prof.videoFrameWidth, prof.videoFrameHeight);
                        mMediaRecorder.setVideoFrameRate(30);
                        mMediaRecorder.setVideoEncodingBitRate((int) (prof.videoBitRate * 2.8)); // need a higher bitrate for the better quality
                    }
                    lastCamcorderProfile = prof;
                    useProf = true;
                    lastUseProf = useProf;
                }

            }
                break;
            case QUALITY_4K: {
                if (CamcorderProfile.hasProfile(CameraController.getCameraIndex(),
                        CamcorderProfile.QUALITY_1080P)) {
                    CamcorderProfile prof = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P);
                    prof.videoFrameHeight = 2160;
                    prof.videoFrameWidth = 4096;
                    if (ApplicationScreen.isMicrophonePermissionGranted()) {
                        mMediaRecorder.setProfile(prof);
                    } else {
                        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                        mMediaRecorder.setVideoSize(prof.videoFrameWidth, prof.videoFrameHeight);
                        mMediaRecorder.setVideoFrameRate(30);
                        mMediaRecorder.setVideoEncodingBitRate(prof.videoBitRate * 4); // 2160p has 4x more pixels then 1080p.
                    }
                    lastCamcorderProfile = prof;
                    useProf = true;
                    lastUseProf = useProf;
                } else
                    sz = new CameraController.Size(4096, 2160);
            }
                break;
            default:
                break;
            }

            if (!useProf) {
                mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
                mMediaRecorder.setVideoSize(sz.getWidth(), sz.getHeight());
                mMediaRecorder.setVideoFrameRate(30);

                // Other parameters just copy from CamcorderProfile.QUALITY_1080P
                CamcorderProfile prof = CamcorderProfile.get(CamcorderProfile.QUALITY_1080P);
                mMediaRecorder.setVideoEncodingBitRate(prof.videoBitRate * 4); // 2160p has 4x more pixels then 1080p.

                if (ApplicationScreen.isMicrophonePermissionGranted()) {
                    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
                    mMediaRecorder.setAudioChannels(prof.audioChannels);
                    mMediaRecorder.setAudioEncodingBitRate(prof.audioBitRate);
                    mMediaRecorder.setAudioSamplingRate(prof.audioSampleRate);
                }

                lastSz = sz;
            } else
                lastUseProfile = true;
        }

        if (swChecked) {
            double val1 = Double.valueOf(stringInterval[interval]);
            int val2 = measurementVal;
            switch (val2) {
            case 0:
                val2 = 1;
                break;
            case 1:
                val2 = 60;
                break;
            case 2:
                val2 = 3600;
                break;
            default:
                break;
            }
            captureRate = 1 / (val1 * val2);
            mMediaRecorder.setCaptureRate(captureRate);
        }
    } catch (Exception e) {
        e.printStackTrace();

        releaseMediaRecorder(); // release the MediaRecorder object
        return;
    }

    // Step 4: Set output file
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        DocumentFile file = getOutputMediaDocumentFile();
        try {
            documentFileSavedFd = ApplicationScreen.instance.getContentResolver()
                    .openFileDescriptor(file.getUri(), "w");
            FileDescriptor fileDescriptor = documentFileSavedFd.getFileDescriptor();
            mMediaRecorder.setOutputFile(fileDescriptor);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
        }
    } else {
        mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
    }

    // Step 5: Set the preview output
    if (!CameraController.isUseCamera2()) {
        mMediaRecorder.setPreviewDisplay(ApplicationScreen.getPreviewSurfaceHolder().getSurface());
    }

    mMediaRecorder.setOrientationHint(CameraController.isFrontCamera()
            ? (ApplicationScreen.getWantLandscapePhoto()
                    ? ApplicationScreen.getGUIManager().getImageDataOrientation()
                    : (ApplicationScreen.getGUIManager().getImageDataOrientation() + 180) % 360)
            : ApplicationScreen.getGUIManager().getImageDataOrientation());

    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (Exception e) {
        Log.d("Video", "Exception preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();

        CameraController.lockCamera(); // take camera access back from MediaRecorder
        return;
    }
}