当前位置: 首页 > 知识库问答 >
问题:

屏幕记录:停止失败,传递结果ResultInfo失败

洪英豪
2023-03-14

我不能成功的屏幕录制,它开始录制ok,但当它停止应用程序崩溃

错误:

public class ScreenRecord extends AppCompatActivity {

    MediaRecorder mr;
    MediaProjection mp;
    int dw = 720;
    int dh = 1280;
    MediaProjectionManager mpm;
    DisplayMetrics metrics;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mr = new MediaRecorder();
        initRecorder();
        mpm = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        startActivityForResult(mpm.createScreenCaptureIntent(), 1000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        mp = mpm.getMediaProjection(resultCode, data);
        mp.createVirtualDisplay(getClass().getName(), dw, dh, metrics.densityDpi,
                DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, mr.getSurface(), 
                null, null);
        mr.start();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        mr.stop();
    }

    private void initRecorder() {
        try {
            mr.setVideoSource(MediaRecorder.VideoSource.SURFACE);
            mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mr.setOutputFile(Environment
                    .getExternalStoragePublicDirectory(Environment
                            .DIRECTORY_DOWNLOADS) + "/video.mp4");
            mr.setVideoSize(dw, dh);
            mr.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
            mr.setVideoEncodingBitRate(12 * 1000 * 1000);
            mr.setVideoFrameRate(60);
            mr.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

共有1个答案

燕志学
2023-03-14

确定录制已实际开始后,将stop()移动到其他位置。我的猜测是,start()要等到您将主应用程序线程的控制权交还给Android之后才会生效。

 类似资料: