屏幕录制:停止失败和失败传递结果ResultInfo

时间:2017-07-23 14:11:14

标签: android

我无法成功进行屏幕录制,它开始录制正常但是当它停止应用程序崩溃时

错误:

  

E / MediaRecorder:停止失败:-1007 D / AndroidRuntime:关闭VM   E / AndroidRuntime:FATAL EXCEPTION:主要流程:   com.confusedbox.screenrecorder,PID:6314 java.lang.RuntimeException:   发送失败结果ResultInfo {who = null,request = 1000,   result = -1,data = Intent {(has extras)}} to activity   {com.confusedbox.screenrecorder / com.confusedbox.screenrecorder.ScreenRecord}:   java.lang.RuntimeException:停止失败。       在android.app.ActivityThread.deliverResults(ActivityThread.java:3706)       在android.app.ActivityThread.handleSendResult(ActivityThread.java:3749)       在android.app.ActivityThread.access $ 1400(ActivityThread.java:153)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1400)       在android.os.Handler.dispatchMessage(Handler.java:102)       在android.os.Looper.loop(Looper.java:148)       在android.app.ActivityThread.main(ActivityThread.java:5441)       at java.lang.reflect.Method.invoke(Native Method)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:738)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)引起:java.lang.RuntimeException:停止失败。       在android.media.MediaRecorder.stop(Native方法)       在com.confusedbox.screenrecorder.ScreenRecord.onActivityResult(ScreenRecord.java:48)       在android.app.Activity.dispatchActivityResult(Activity.java:6508)       在android.app.ActivityThread.deliverResults(ActivityThread.java:3702)       在android.app.ActivityThread.handleSendResult(ActivityThread.java:3749)       在android.app.ActivityThread.access $ 1400(ActivityThread.java:153)       在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1400)       在android.os.Handler.dispatchMessage(Handler.java:102)       在android.os.Looper.loop(Looper.java:148)       在android.app.ActivityThread.main(ActivityThread.java:5441)       at java.lang.reflect.Method.invoke(Native Method)       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:738)       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)

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

Android 6(Marshmallow)

1 个答案:

答案 0 :(得分:0)

在确定录制内容实际已开始后,将sed -i /etc/ssh/sshd_config -e '/# The default requires explicit activation of protocol 1/a Protocol 2' 移至其他位置。我的猜测是stop()在您将主应用程序线程的控制权返回给Android之后的某个时间才会生效。