7 월 보안 업데이트로 6.0.1을 실행하는 Nexus 5에서 스크린 레코딩을 위해이 코드를 사용하고 있습니다. 화면 녹화는 5.0.1, 6.0, 6.0.1을 실행하는 다른 기기에서도 정상적으로 작동하지만 휴대 전화에서는 작동하지 않습니다. 화면 녹화를 시작하려고하면 다음과 같은 오류가 발생합니다.MediaRecorder.GetSurface() null을 반환했습니다.
E/MediaRecorder: SurfaceMediaSource could not be initialized!
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1995, result=-1, data=Intent { (has extras) }} to activity {MainActivity}: java.lang.IllegalStateException: failed to get surface
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
Caused by: java.lang.IllegalStateException: failed to get surface
at android.media.MediaRecorder.getSurface(Native Method)
화면 녹화를 위해 표면을 얻지 못했습니다. 문제의 원인은 무엇이며 어떻게 해결할 수 있습니까?
소스 코드 :
public static MediaProjectionManager getmMediaProjectionManager(final MainActivity context) {
DisplayMetrics metrics = new DisplayMetrics();
context.getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;
DISPLAY_HEIGHT = metrics.heightPixels;
DISPLAY_WIDTH = metrics.widthPixels;
mMediaRecorder = new MediaRecorder();
mMediaProjectionManager = (MediaProjectionManager) context.getSystemService(Context.MEDIA_PROJECTION_SERVICE);
return mMediaProjectionManager;
}
@TargetApi(21)
public static void startScreenRecording(Intent data) {
mMediaProjectionCallback = new MediaProjectionCallback();
initRecorder(null);
mMediaProjection = mMediaProjectionManager.getMediaProjection(RESULT_OK, data);
mMediaProjection.registerCallback(mMediaProjectionCallback, null);
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}
@TargetApi(21)
private static VirtualDisplay createVirtualDisplay() {
return mMediaProjection.createVirtualDisplay("MainActivity",
DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null /*Callbacks*/, null
/*Handler*/);
}
@TargetApi(21)
private static void initRecorder(MainActivity context) {
try {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setOutputFile(Environment
.getExternalStorageDirectory() + "/video"+ System.currentTimeMillis()+".mp4");
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mMediaRecorder.setVideoEncodingBitRate(VIDEO_ENCODING_BITRATE);
mMediaRecorder.setVideoFrameRate(VIDEO_FRAME_RATE);
mMediaRecorder.prepare();
} catch (Exception e) {
Log.e("Util", e.getLocalizedMessage());
}
}
@TargetApi(21)
private static class MediaProjectionCallback extends MediaProjection.Callback {
@Override
public void onStop() {
}
}
@TargetApi(21)
public static void stopScreenSharing() {
mMediaRecorder.stop();
mMediaRecorder.reset();
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
destroyMediaProjection();
}
@TargetApi(21)
private static void destroyMediaProjection() {
if (mMediaProjection != null) {
Log.e(TAG, "destroying projection");
mMediaProjection.unregisterCallback(mMediaProjectionCallback);
mMediaProjection.stop();
mMediaProjection = null;
}
}
안부
MediaRecorder를 글로벌하게 확인하십시오. –
문제를 어떻게 해결할 지 모르겠습니다. 해결책 제안 뒤에있는 이유에 대해 설명해 주시겠습니까 –
onCreate 메소드에서 전역 유효성 검사를 초기화해야합니다. 그 후에는 매번이 veriable으로 처리하기를 원할 경우 이미 초기화되고이 오류는 발생하지 않습니다. –