如何在具有SurfaceView的当前活动时返回上一个活动?

时间:2017-10-18 10:29:02

标签: android surfaceview

我知道如何像往常一样返回上一个活动。当前活动(VideoActivity.java)有SurfaceView MediaRecorder来录制视频。每当我实现finish()以返回上一个活动时,它都会导致错误,如下面的logcat:

E/BufferQueueProducer: [SurfaceView] dequeueBuffer: BufferQueue has been abandoned 
E/EmulatedCamera_Preview: onNextFrameAvailable: Unable to dequeue preview window buffer: 19 -> No such device

logcat只重复上面的2行并成为很长的列表,因此我只显示了这两行。

除了显示上面的logcat之外,每当我打算VideoActivity.java时,应用程序都会停止。

这是我实现返回上一个活动的方式

backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(isRecording){
                    stopRecording();
                    finish();
                }else{
                    finish();
                }

        }
    });

基本上我像这样实现SurfaceView:

public class VideoActivity extends AppCompatActivity implements SurfaceHolder.Callback{

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

    surfaceHolder = videoSurface.getHolder();
    surfaceHolder.addCallback(this);
    surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

    backButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if(isRecording){
                stopRecording();
                finish();
            }else{
                finish();
            }

        }
    });
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    try{
        mCamera = Camera.open();
    }catch (RuntimeException ex){
        ex.printStackTrace();
    }

    try{
        mCamera.setPreviewDisplay(surfaceHolder);
        mCamera.startPreview();
    }catch (Exception e){
        e.printStackTrace();
    }
    prepareRecorder();
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    mRecorder.reset();
    mRecorder.release();
    mCamera.release();

    // once the objects have been released they can't be reused
    mRecorder = null;
    mCamera = null;    }

}

在使用finish()返回上一个活动之前,此代码正常运行。

所以我的问题是,如果某个活动有SurfaceView,它怎么能回到以前的活动?请告诉我正确的方法。

1 个答案:

答案 0 :(得分:0)

我意识到我没有发布CameraMediaRecorder。我可以通过在Camera之前发布mediaRecorderfinish()来解决此问题,因此代码看起来像这样

back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isRecording){
                    Log.d("Video","stop recording");
            try{
                   mRecorder.stop();
            }catch (RuntimeException e) {

            }
            releaseMediaRecorder();
            mCamera.lock();
            isRecording = false;
            releaseCamera();
                    finish();
                }else{
                    releaseCamera();
                    finish();
                }

            }
        });
}

private void releaseCamera(){
        if (mCamera != null){
            // release the camera for other applications
            mCamera.setPreviewCallback(null);
            surfaceHolder.removeCallback(VideoActivity.this);
            mCamera.release();
            mCamera = null;
        }
}

private void releaseMediaRecorder(){
        if (mRecorder != null) {
            // clear recorder configuration
            mRecorder.reset();

            mRecorder.release();
            mRecorder = null;

            mCamera.lock();
        }
}