I can't succeed in screen recording, it starts recording ok but when it stops app crashes
the error:
E/MediaRecorder: stop failed: -1007 D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.confusedbox.screenrecorder, PID: 6314 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1000, result=-1, data=Intent { (has extras) }} to activity {com.confusedbox.screenrecorder/com.confusedbox.screenrecorder.ScreenRecord}: java.lang.RuntimeException: stop failed. at android.app.ActivityThread.deliverResults(ActivityThread.java:3706) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3749) at android.app.ActivityThread.access$1400(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5441) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) Caused by: java.lang.RuntimeException: stop failed. at android.media.MediaRecorder.stop(Native Method) at com.confusedbox.screenrecorder.ScreenRecord.onActivityResult(ScreenRecord.java:48) at android.app.Activity.dispatchActivityResult(Activity.java:6508) at android.app.ActivityThread.deliverResults(ActivityThread.java:3702) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3749) at android.app.ActivityThread.access$1400(ActivityThread.java:153) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5441) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738) at 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)