gpt4 book ai didi

java - MediaRecorder.GetSurface() 返回 null

转载 作者:行者123 更新时间:2023-12-05 07:48:27 24 4
gpt4 key购买 nike

我在我的 Nexus 5 上使用此代码进行屏幕录制,该 Nexus 5 运行 6.0.1,带有 7 月安全更新。屏幕录制在运行 5.0.1、6.0、6.0.1 的其他设备上工作正常,但它在我的手机上不起作用。当我尝试开始屏幕录制时出现以下错误。

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)

获取 Surface 进行屏幕录制失败。是什么原因造成的,我该如何解决?

源代码:

 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;
}
}

最好的问候

最佳答案

这基本上与 this one 相同的问题.我也面临这个问题。奇怪的是,它只发生在 Marshmallow 上,在 Lollipop 上它确实有效。

文档说:

Surface getSurface ()

May only be called after prepare. Frames rendered to the Surface before start will be discarded.Throws:
IllegalStateException - if it is called before prepare, after stop, or is called when VideoSource is not set to SURFACE.

但在 Mediarecorder.java 中,它是:

@throws IllegalStateException if it is called after {@link #prepare} and before {@link #stop}.

但是无论我将它放在 之前 还是 之后 prepare() 都没有区别,两者都不起作用。它抛出一个 IllegalStateException 真的很奇怪,尽管上面的事情都不适用。

然而,this solution来自 Matt Snider 确实在 Marshmallow 上工作。但由于 IMO 比较困难(尤其是在尝试录制音频时),最好让它也与 MediaRecorder 一起运行。

如果有人想重现问题,只需使用 thisthis编码并在 Marshmallow 机器上运行。

关于java - MediaRecorder.GetSurface() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38630664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com