我正在使用Media Recorder录制设备屏幕。如果我提供本地路径作为输出源,那么媒体记录器工作正常,但如果我提供从parcelFileDescriptor.fromSocket(套接字)获得的文件描述符,那么我在mediaRecorder.prepare()上得到非法状态异常。
这是我的媒体记录器初始化代码 -
try {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
mMediaRecorder = new MediaRecorder();
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory().getPath() + "/video.mp4"); // this is working
ParcelFileDescriptor parcelFileDescriptor = ParcelFileDescriptor.fromSocket(socket);
FileDescriptor fd = parcelFileDescriptor.getFileDescriptor();
mMediaRecorder.setOutputFile(parcelFileDescriptor.getFileDescriptor());
mMediaRecorder.setVideoSize(displayMetrics.widthPixels, displayMetrics.heightPixels);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(1);
mMediaRecorder.prepare();
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalStateException e){
e.printStackTrace();
}
这是我的logcat -
04-22 15:26:30.954 11370-11370/com.portalD/AndroidRuntime: Shutting down VM
04-22 15:26:30.954 11370-11370/com.portalE/AndroidRuntime: FATAL EXCEPTION: main
Process: com.portal., PID: 11370
java.lang.IllegalStateException
at android.media.MediaRecorder._prepare(Native Method)
at android.media.MediaRecorder.prepare(MediaRecorder.java:885)
at com.porta.media.BCTCRecorder.initRecorder(BCTCRecorder.java:108)
at com.portal.media.BCTCRecorder.<init>(BCTCRecorder.java:40)
at com.portal.activity.BCTCSocketTestingActivity.initRecorder(BCTCSocketTestingActivity.java:110)
at com.portal.activity.BCTCSocketTestingActivity.access$200(BCTCSocketTestingActivity.java:23)
at com.portal.activity.BCTCSocketTestingActivity$4.run(BCTCSocketTestingActivity.java:100)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6862)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)