Reputation: 1183
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:
I can provide more code if wanted.
Thanks in advance!
Upvotes: 2
Views: 1950
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
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
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