#StackBounty: #java #android #mediarecorder #localsocket How to fix 'java.lang.IllegalStateException' on 'MediaRecorder.sta…

Bounty: 50

I’m trying to develop a circular video recorder and I need to save the recorded video in a buffer to recovery the last X minutes in a certain moment.
The problem is that when I run ‘start()’ method of MediaRecorder object:

mMediaRecorder.start();

The following error shows up:

java.lang.IllegalStateException
android.media.MediaRecorder.start(Native Method)
at picture.wikinova.com.odc.CameraActivity$MediaPrepareTask.doInBackground(CameraActivity.java:298)
at picture.wikinova.com.odc.CameraActivity$MediaPrepareTask.doInBackground(CameraActivity.java:289)
at android.os.AsyncTask$2.call(AsyncTask.java:333)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:764)

The error does not happen when I’m using ‘File’ as outputFile of MediaRecorder, but it happens when I’m trying to use LocalSocket to recovery the video buffer in memory.

This is my code to create the LocalServerSocket and capture the video

         mLocalServerSocket = new LocalServerSocket("sockt_ip");
            } catch (Exception e) {
                e.printStackTrace();
            }
            LocalSocket mLocalClientSocket = null;
            mLocalClientSocket = mLocalServerSocket.accept();
            InputStream in = mLocalClientSocket.getInputStream();
            byte[] buffer = new byte[1024];
            String retorno = "";
            int len = 0;
            while ((len = in.read(buffer)) >= 0) {
                for (int i = 0; i < len; i++) {
                  retorno += buffer[i];
                }

             }

This is my code to create the MediaRecorder and set the output file

CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
        profile.videoFrameWidth = optimalSize.width;
        profile.videoFrameHeight = optimalSize.height;

        parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
        mCamera.setParameters(parameters);
        try {
            mCamera.setPreviewTexture(mPreview.getSurfaceTexture());
        } catch (IOException e) {
            Log.e(TAG, "Surface texture is unavailable or unsuitable" + e.getMessage());
            return false;
        }


        mMediaRecorder = new MediaRecorder();

        mCamera.unlock();
        mMediaRecorder.setCamera(mCamera);


        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);



        mMediaRecorder.setProfile(profile);

        LocalSocket outSocket = new LocalSocket();
        try {
            outSocket.connect(new LocalSocketAddress("sockt_ip"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        mMediaRecorder.setOutputFile(outSocket.getFileDescriptor());


        mMediaRecorder.prepare();
        mMediaRecorder.start();

I already tested the socket connection. I wrote some text in

LocalSocket outSocket 

and received it in

InputStream in = mLocalClientSocket.getInputStream();
byte[] buffer = new byte[1024];

The problem is the part that I set The socket FileDescriptor as outputFile to the mediaRecorder, but I cannot figure t out what can be.


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.