mnordber
mnordber

Reputation: 1183

Using a service for video recording results in IOException "Invalid Preview Surface"

I've seen similar problems but my code is structured quite different compared to those threads I've read about this issue.

My problem is that when I try to set the previewDisplay of the MediaRecorder

mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());

I get the IOException "invalid preview surface".
A Log.v(TAG, mSurfaceHolder.getSurface().toString() gives "Surface(name=null)/@0x42333f90" so I think the surface is null somehow?

I'm trying to make this camera recording as a service which makes me uncertain about what my surface and surfaceholder should be.

Currently, in onCreate() of RecorderService.class is:

SurfaceView view = new SurfaceView(getActivity());
mSurfaceHolder = view.getHolder();

where getActivity() returns the activity where the service is started from.

The camera is started from:

Log.v(TAG, "Starting camera");
Intent intent = new Intent(getActivity(), CameraRecorder.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(intent);

So when I try to stop the recording, the app crashes on mMediaRecorder.stop() due to a IllegalStateException.

To sum it up:

  1. I want a RecorderService.class which can be started from any activity and it will record the camera until stopService()
  2. I'm not sure what to use for SurfaceView. I'm currently using the activity from where the recording is started but this results in a null SurfaceView (I think).

I can provide more code if wanted.

Thanks in advance!

Upvotes: 2

Views: 1950

Answers (3)

George Thomas
George Thomas

Reputation: 13

i have initialized like this in oncreate():

mSurfaceView = CameraRecorder.mSurfaceView;
        mSurfaceHolder = CameraRecorder.mSurfaceHolder;

and made the camera to prepare in oncreate() of the service itself and the error occurs.

I hope it is the same issue. Am not able to implement the above code.

Upvotes: 0

18446744073709551615
18446744073709551615

Reputation: 16832

I have fixed java.io.IOException: invalid preview surface by using a SurfaceHolder.Callback. You cannot use the preview surface before the callback's method surfaceCreated() is called. One has to use something like

mSurfaceView.getHolder().addCallback(mSurfaceHolderCallback);

Upvotes: 1

mnordber
mnordber

Reputation: 1183

I managed to solve this by creating a new activity CameraRecorder.java which sole purpose is to start the service. in the layout xml-file of CameraRecorder I added a SurfaceView which I used for preview. Since I run finish() in CameraRecorder.java right when I started the service, the preview isn't visible on the screen!

Upvotes: 1

Related Questions