如何在当前 activity 具有 SurfaceView 时 return 到上一个 activity?
How to return to previous activity when current activity having SurfaceView?
我知道如何 return 到以前的 activity 因为 usual.The 当前 activity(VideoActivity.java
) 有 SurfaceView
MediaRecorder
录制视频。每当我将 finish()
实现到 return 到之前的 activity 时,它都会导致如下 logcat 的错误:
E/BufferQueueProducer: [SurfaceView] dequeueBuffer: BufferQueue has been abandoned
E/EmulatedCamera_Preview: onNextFrameAvailable: Unable to dequeue preview window buffer: 19 -> No such device
logcat只是重复了上面两行,变成了很长的列表,所以我只显示了这两行。
除了显示上面的 logcat 之外,每当我打算 VideoActivity.java
时,应用程序就会停止。
这就是我实现 return 到之前的 activity
的方式
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()
.
return 到之前的 activity 之前,这段代码工作正常
所以我的问题是,如果一个 activity 有 SurfaceView
,它怎么能 return 到以前的 activity?请告诉我正确的方法。
我意识到我没有发布 Camera
和 MediaRecorder
。我可以通过在 finish()
之前发布 Camera
和 mediaRecorder
来解决这个问题, 所以代码看起来像这样
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();
}
}
我知道如何 return 到以前的 activity 因为 usual.The 当前 activity(VideoActivity.java
) 有 SurfaceView
MediaRecorder
录制视频。每当我将 finish()
实现到 return 到之前的 activity 时,它都会导致如下 logcat 的错误:
E/BufferQueueProducer: [SurfaceView] dequeueBuffer: BufferQueue has been abandoned
E/EmulatedCamera_Preview: onNextFrameAvailable: Unable to dequeue preview window buffer: 19 -> No such device
logcat只是重复了上面两行,变成了很长的列表,所以我只显示了这两行。
除了显示上面的 logcat 之外,每当我打算 VideoActivity.java
时,应用程序就会停止。
这就是我实现 return 到之前的 activity
的方式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()
.
所以我的问题是,如果一个 activity 有 SurfaceView
,它怎么能 return 到以前的 activity?请告诉我正确的方法。
我意识到我没有发布 Camera
和 MediaRecorder
。我可以通过在 finish()
之前发布 Camera
和 mediaRecorder
来解决这个问题, 所以代码看起来像这样
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();
}
}