如何使用LocalSocket

时间:2019-07-15 22:10:49

标签: java android mediarecorder localsocket

我正在尝试开发圆形录像机,我需要将录制的视频保存在缓冲区中,以恢复特定时刻的最后X分钟。 问题是当我运行MediaRecorder对象的'start()'方法时:

mMediaRecorder.start();

出现以下错误:

  

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

当我使用“文件”作为MediaRecorder的outputFile时不会发生该错误,但是当我尝试使用LocalSocket恢复内存中的视频缓冲区时会发生该错误。

这是我的代码,用于创建LocalServerSocket并捕获视频

         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];
                }

             }

这是我创建MediaRecorder并设置输出文件的代码

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();

我已经测试了套接字连接。我在

中写了一些文字
LocalSocket outSocket 

并在

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

问题是我将套接字FileDescriptor设置为mediaRecorder的outputFile的一部分,但是我无法弄清楚是什么。

2 个答案:

答案 0 :(得分:0)

尝试使用new LocalSocket(LocalSocket.SOCKET_STREAM)而不是new LocalSocket()创建LocalSocketImpl

如果您没有如上所述设置 socket类型,则LocalSocket(由IllegalStateException封装的实际系统套接字)会以{{1 }},该类中唯一的一个:

public void create (int sockType) throws IOException {
    // no error if socket already created
    // need this for LocalServerSocket.accept()
    if (fd == null) {
        int osType;
        switch (sockType) {
            case LocalSocket.SOCKET_DGRAM:
                osType = OsConstants.SOCK_DGRAM;
                break;
            case LocalSocket.SOCKET_STREAM:
                osType = OsConstants.SOCK_STREAM;
                break;
            case LocalSocket.SOCKET_SEQPACKET:
                osType = OsConstants.SOCK_SEQPACKET;
                break;
            default:
                throw new IllegalStateException("unknown sockType");
        }
        try {
            fd = Os.socket(OsConstants.AF_UNIX, osType, 0);
            mFdCreatedInternally = true;
        } catch (ErrnoException e) {
            e.rethrowAsIOException();
        }
    }
}

之所以应该使用SOCKET_STREAM,是因为LocalSocketImpl内部的LocalServerSocket也使用该类型。

答案 1 :(得分:0)

使用以下代码

    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(LocalSocket.SOCKET_STREAM);
            try {
                outSocket.connect(new LocalSocketAddress("sockt_ip"));
            } catch (Exception e) {
                e.printStackTrace();
            }
            mMediaRecorder.setOutputFile(outSocket.getFileDescriptor());


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