setOneShotPreviewCallback not hitting onPreviewFra

2019-02-16 00:48发布

问题:

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?

回答1:

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

mCamera.reconnect();

before calling:

mCamera.setOneShotPreviewCallback(mPreviewCallback);

(but after everything else).