我已经在做Android摄像头视频流传输一段时间了。起初,我尝试了libstreaming包,但遇到了这个问题。所以我按照Google相机示例代码写了自己的类。但是,当我将ParcelFileDescriptor
传递给mMediaRecorder.setOutputFile
时,我遇到了这个错误:
E/MediaRecorder: start failed: -2147483648
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: sharedcameraandroid, PID: 14868
java.lang.RuntimeException: start failed.
at android.media.MediaRecorder.start(Native Method)
at MainActivity.startRecordingVideo(MainActivity.java:268)
at MainActivity.setUpMediaRecorder(MainActivity.java:257)
所以我在网上查找这个(将输出格式更改为3GPP),这个来添加
Thread.sleep
,还有这个。但是它们都没有起作用,所以我来问问是否有人知道如何解决这个问题。这是我的代码:
private void setUpMediaRecorder() throws IOException {
final Activity activity = mActivity;
if (null == activity) {
return;
}
ParcelFileDescriptor[] parcelFileDescriptors = ParcelFileDescriptor.createPipe();
mParcelRead = new ParcelFileDescriptor(parcelFileDescriptors[0]);
mParcelWrite = new ParcelFileDescriptor(parcelFileDescriptors[1]);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// mMediaRecorder.setOutputFile((new File(mActivity.getExternalFilesDir(null), "video.mp4")).getAbsolutePath());
// Log.d(TAG, (new File(mActivity.getExternalFilesDir(null), "video.mp4")).getAbsolutePath());
mMediaRecorder.setOutputFile(mParcelWrite.getFileDescriptor());
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
// mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.prepare();
startRecordingVideo();
// https://dev59.com/MGUq5IYBdhLWcg3wT-2D
InputStream is = new ParcelFileDescriptor.AutoCloseInputStream(mParcelRead);
RtspClient client = new RtspClient(is, releaseSampleFile());
client.startStream();
}
如果我使用被注释的代码行替换,一切都正常。谢谢您的任何帮助!