我正在尝试开发一个圆形视频录制器,并且需要将录制的视频保存在缓冲区中,在特定时刻恢复最后X分钟的内容。问题是当我运行MediaRecorder对象的'start()'方法时:
mMediaRecorder.start();
以下错误出现:
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)
当我使用'MediaRecorder'的'File'作为输出文件时,不会发生错误,但是当我尝试使用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];
问题在于我将Socket文件描述符设置为媒体记录器的输出文件时,但我无法弄清楚问题所在。