android / media-samples

Multiple samples showing the best practices in media APIs on Android (audio, video, etc.).
Apache License 2.0
1.27k stars 740 forks source link

how to Start to preview in view Function #36

Open helinfeng opened 8 years ago

helinfeng commented 8 years ago

How to enter the page on the opening of the camera to start the preview, press after the beginning of recording?i think everyone in view start to preview ,help me ,thanks!

hoanghiep commented 7 years ago
    class MediaPreviewTask extends AsyncTask<Void, Void, Boolean>{
        @Override
        protected Boolean doInBackground(Void... voids) {
            return showPreview();
        }

        @Override
        protected void onPostExecute(Boolean result) {
            if (!result) {
                RecorderActivity.this.finish();
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private boolean showPreview(){
        // BEGIN_INCLUDE (configure_preview)
        mCamera = CameraHelper.getDefaultCameraInstance();
//        mCamera = CameraHelper.getDefaultFrontFacingCameraInstance();

        // We need to make sure that our preview and recording video size are supported by the
        // camera. Query camera to find all the sizes and choose the optimal size given the
        // dimensions of our preview surface.
        Camera.Parameters parameters = mCamera.getParameters();
        List<Camera.Size> mSupportedPreviewSizes = parameters.getSupportedPreviewSizes();
        List<Camera.Size> mSupportedVideoSizes = parameters.getSupportedVideoSizes();
        Camera.Size optimalSize = CameraHelper.getOptimalVideoSize(mSupportedVideoSizes,
                mSupportedPreviewSizes, mPreview.getWidth(), mPreview.getHeight());

        // Use the same size for recording profile.
        CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
        profile.videoFrameWidth = optimalSize.width;
        profile.videoFrameHeight = optimalSize.height;

        // likewise for the camera object itself.
        parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
        mCamera.setParameters(parameters);
        try {
            // Requires API level 11+, For backward compatibility use {@link setPreviewDisplay}
            // with {@link SurfaceView}
            mCamera.setPreviewTexture(mPreview.getSurfaceTexture());
            mCamera.startPreview();
            return true;
        } catch (IOException e) {
            Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
        }
        return false;
    }

And call it in onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recorder);
    initLayout();
}

private void initLayout() {
    mPreview = (TextureView) findViewById(R.id.surface_view);
    captureButton = (ImageView) findViewById(R.id.button_capture);
    new MediaPreviewTask().execute(null, null, null);
}