setOneShotpReviewCallback未在回调中击中onpreviewframe()[英] setOneShotPreviewCallback not hitting onPreviewFrame() in callback

本文是小编为大家收集整理的关于setOneShotpReviewCallback未在回调中击中onpreviewframe()的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在为正在制作的应用程序创建视频捕获活动,并且我想捕获预览框架,以用作播放按钮.我已经在onCreate(...)中设置了SurfaceView,并在SurfaceHolder.Callback中挂上了相机的预览显示:

private Callback mSurfaceViewCallback = new Callback() {

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mSurfaceCreated = false;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mSurfaceCreated = true;
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        try {
            mSurfaceCreated = true;
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
};

适用于静态相机应用程序.相机预览在onPause()和onResume()中打开和关闭:

@Override
protected void onResume() {
    super.onResume();
    if(mCamera!=null)
        mCamera.startPreview();
}

@Override
protected void onPause() {
    if(mCamera!=null)
        mCamera.stopPreview();
    super.onPause();
}

我已经设置了这样的MediaRecorder:

protected void startRecording() {
    mMediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));

    mMediaRecorder.setMaxDuration(10000);
    mMediaRecorder.setOutputFile(new File(getFilesDir(), mChallengeUUID + ".mp4").getAbsolutePath());
    mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mCamera.setOneShotPreviewCallback(mPreviewCallback);
    mMediaRecorder.start();
}

然而,倒数第二条无法正常工作.我的Camera.PreviewCallback's onPreviewFrame(...)被错过了.

正如我所说,

拥有它的目的是从录制视频以显示给用户的视频时获得帧.为什么回调不被击中?

推荐答案

mCamera.unlock()将相机从过程中分离出来.有必要致电:

mCamera.reconnect();

致电之前:

mCamera.setOneShotPreviewCallback(mPreviewCallback);

(但是在其他一切之后).

本文地址:https://www.itbaoku.cn/post/102580.html

问题描述

I'm creating a video capture activity for an app I'm making, and I want to capture a preview frame as well to use as a playback button. I've set the SurfaceView up in onCreate(...) and hooked up the camera's preview display in the SurfaceHolder.Callback:

private Callback mSurfaceViewCallback = new Callback() {

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        mSurfaceCreated = false;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mSurfaceCreated = true;
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        try {
            mSurfaceCreated = true;
            mCamera.setPreviewDisplay(mHolder);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
};

which worked fine for the static camera application. Camera preview is switched on and off in onPause() and onResume():

@Override
protected void onResume() {
    super.onResume();
    if(mCamera!=null)
        mCamera.startPreview();
}

@Override
protected void onPause() {
    if(mCamera!=null)
        mCamera.stopPreview();
    super.onPause();
}

and I've set the MediaRecorder up like this:

protected void startRecording() {
    mMediaRecorder = new MediaRecorder();
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_LOW));

    mMediaRecorder.setMaxDuration(10000);
    mMediaRecorder.setOutputFile(new File(getFilesDir(), mChallengeUUID + ".mp4").getAbsolutePath());
    mMediaRecorder.setPreviewDisplay(mHolder.getSurface());
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    mCamera.setOneShotPreviewCallback(mPreviewCallback);
    mMediaRecorder.start();
}

That penultimate line, however, isn't working. My Camera.PreviewCallback's onPreviewFrame(...) method is being missed.

The purpose of having it is, as I say, to get a frame from the beginning of when the video is being recorded to display to the user. Why isn't the callback being hit?

推荐答案

mCamera.unlock() detaches the camera from the process. It's necessary to call:

mCamera.reconnect();

before calling:

mCamera.setOneShotPreviewCallback(mPreviewCallback);

(but after everything else).