我正在尝试开发圆形录像机,我需要将录制的视频保存在缓冲区中,以恢复特定时间的最后X分钟。
问题是当我运行MediaRecorder对象的'start()'方法时:
mMediaRecorder.start();
出现以下错误:
当我使用"file"作为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的一部分,但是我不知道可以做什么。
最佳答案
尝试使用LocalSocket
而不是new LocalSocket(LocalSocket.SOCKET_STREAM)
创建new LocalSocket()
。
如果您没有如上所示设置套接字类型,LocalSocketImpl
(它是由LocalSocket
封装的实际系统套接字)会精确地提示IllegalStateException
,即该类中唯一的套接字:
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
也使用了该类型。关于java - 如何使用LocalSocket在 'java.lang.IllegalStateException'上修复 'MediaRecorder.start()',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57047764/