gpt4 book ai didi

android - 我可以在 Android Studio 的模拟器中测试屏幕录制吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:11:21 24 4
gpt4 key购买 nike

我尝试从下面的链接测试有关屏幕录制的示例代码,我修改了一些代码以禁用录制音频。

http://www.truiton.com/2015/05/capture-record-android-screen-using-mediaprojection-apis/

我在Android Studio V1.3中测试代码,出现如下错误,capture.mp4文件为空。

我不确定是否必须在真实手机中测试代码?你可以帮帮我吗 ?谢谢!

错误信息

09-22 06:41:50.250    2167-2167/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: screencapture.truiton.com.myapplication, PID: 2167
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
at screencapture.truiton.com.myapplication.MainActivity.onToggleScreenShare(MainActivity.java:93)
at screencapture.truiton.com.myapplication.MainActivity$1.onClick(MainActivity.java:55)
at android.view.View.performClick(View.java:4780)
at android.widget.CompoundButton.performClick(CompoundButton.java:120)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

源代码

package screencapture.truiton.com.myapplication;

public class MainActivity extends Activity {

private static final String TAG = "MainActivity";
private static final int PERMISSION_CODE = 1;
private int mScreenDensity;
private MediaProjectionManager mProjectionManager;
private static final int DISPLAY_WIDTH = 480;
private static final int DISPLAY_HEIGHT = 640;
private MediaProjection mMediaProjection;
private VirtualDisplay mVirtualDisplay;
private MediaProjectionCallback mMediaProjectionCallback;
private ToggleButton mToggleButton;
private MediaRecorder mMediaRecorder;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenDensity = metrics.densityDpi;

mMediaRecorder = new MediaRecorder();
initRecorder();
prepareRecorder();

mProjectionManager = (MediaProjectionManager) getSystemService
(Context.MEDIA_PROJECTION_SERVICE);

mToggleButton = (ToggleButton) findViewById(R.id.toggle);
mToggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onToggleScreenShare(v);
}
});

mMediaProjectionCallback = new MediaProjectionCallback();
}

@Override
public void onDestroy() {
super.onDestroy();
if (mMediaProjection != null) {
mMediaProjection.stop();
mMediaProjection = null;
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode != PERMISSION_CODE) {
Log.e(TAG, "Unknown request code: " + requestCode);
return;
}
if (resultCode != RESULT_OK) {
Toast.makeText(this,
"Screen Cast Permission Denied", Toast.LENGTH_SHORT).show();
mToggleButton.setChecked(false);
return;
}
mMediaProjection = mProjectionManager.getMediaProjection(resultCode, data);
mMediaProjection.registerCallback(mMediaProjectionCallback, null);
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}

public void onToggleScreenShare(View view) {
if (((ToggleButton) view).isChecked()) {
shareScreen();
} else {
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
stopScreenSharing();
initRecorder();
prepareRecorder();
}
}

private void shareScreen() {
if (mMediaProjection == null) {
startActivityForResult(mProjectionManager.createScreenCaptureIntent(), PERMISSION_CODE);
return;
}
mVirtualDisplay = createVirtualDisplay();
mMediaRecorder.start();
}

private void stopScreenSharing() {
if (mVirtualDisplay == null) {
return;
}
mVirtualDisplay.release();
//mMediaRecorder.release();
}

private VirtualDisplay createVirtualDisplay() {
return mMediaProjection.createVirtualDisplay("MainActivity",
DISPLAY_WIDTH, DISPLAY_HEIGHT, mScreenDensity,
DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
mMediaRecorder.getSurface(), null /*Callbacks*/, null /*Handler*/);
}

private class MediaProjectionCallback extends MediaProjection.Callback {
@Override
public void onStop() {
if (mToggleButton.isChecked()) {
mToggleButton.setChecked(false);
mMediaRecorder.stop();
mMediaRecorder.reset();
Log.v(TAG, "Recording Stopped");
initRecorder();
prepareRecorder();
}
mMediaProjection = null;
stopScreenSharing();
Log.i(TAG, "MediaProjection Stopped");
}
}

private void prepareRecorder() {
try {
mMediaRecorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}

private void initRecorder() {

//mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);

mMediaRecorder.setVideoEncodingBitRate(512 * 1000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(DISPLAY_WIDTH, DISPLAY_HEIGHT);
mMediaRecorder.setOutputFile(Environment.getExternalStorageDirectory() + "/capture.mp4");
}
}

最佳答案

根据我的经验,Android 模拟器确实允许从表面读取(这是录制唯一真正需要的东西)但由于框架 Java 代码中的错误/限制而无法编码视频(有时截屏)。主要原因有两个:

  • 模拟器可能不会声明支持 EGL_ANDROID_recordable扩展,这是 MediaRecorder/MediaCodec 所要求的。您可以通过执行以下命令来检查扩展支持:

    adb shell dumpsys SurfaceFlinger | grep EGL_ANDROID_recordable
  • Android 6 内置软件编码器支持的唯一颜色格式是 YUV。来自模拟器 GPU 的视频缓冲区可能具有不同的格式。

请注意,上述限制并非模拟器独有,也可能导致实际设备出现故障。您可以使用 this answer 中的方法概述在软件中(在 CPU 上或通过 GLES)进行格式转换来解决这些问题。 ,但无论哪种方式,软件编码器的性能都会很糟糕。

关于android - 我可以在 Android Studio 的模拟器中测试屏幕录制吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32710510/

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