2

I´m trying to write a screen recorder for android and i use this as base: https://github.com/commonsguy/cw-omnibus/tree/master/MediaProjection/andcorder

My problem is: If i select a resolution bigger than 720p with mp4/h264 settings, it crashes on stop.

Examples:

(Example 1) Does NOT work

Code in RecordingSession.java:

  void start() {
    recorder=new MediaRecorder();
    recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setVideoFrameRate(config.frameRate);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    recorder.setVideoSize(1080, 1920);
    recorder.setVideoEncodingBitRate(config.bitRate);
    recorder.setOutputFile(output.getAbsolutePath());

    try {
      recorder.prepare();
      vdisplay=projection.createVirtualDisplay("andcorder",
              1080, 1920, config.density,
        VIRT_DISPLAY_FLAGS, recorder.getSurface(), null, null);
      beeper.startTone(ToneGenerator.TONE_PROP_ACK);
      recorder.start();
    }
    catch (IOException e) {
      throw new RuntimeException("Exception preparing recorder", e);
    }
  }

Output on stop:

07-29 13:52:32.880 7845-7845/com.commonsware.android.andcorder E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.commonsware.android.andcorder, PID: 7845
                                                                                 java.lang.RuntimeException: Unable to start service com.commonsware.android.andcorder.RecorderService@61a260a with Intent { act=com.commonsware.android.andcorder.STOP flg=0x10000000 cmp=com.commonsware.android.andcorder/.RecorderService bnds=[257,1321][832,1513] }: java.lang.RuntimeException: stop failed.
                                                                                     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3045)
                                                                                     at android.app.ActivityThread.access$2200(ActivityThread.java:157)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5525)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                                                                                  Caused by: java.lang.RuntimeException: stop failed.
                                                                                     at android.media.MediaRecorder.native_stop(Native Method)
                                                                                     at android.media.MediaRecorder.stop(MediaRecorder.java:851)
                                                                                     at com.commonsware.android.andcorder.RecordingSession.stop(RecordingSession.java:84)
                                                                                     at com.commonsware.android.andcorder.RecorderService.stopRecorder(RecorderService.java:152)
                                                                                     at com.commonsware.android.andcorder.RecorderService.onStartCommand(RecorderService.java:72)
                                                                                     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3028)
                                                                                     at android.app.ActivityThread.access$2200(ActivityThread.java:157) 
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1454) 
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                     at android.os.Looper.loop(Looper.java:148) 
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:5525) 
                                                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

(Example 2) - works fine

Code:

  void start() {
    recorder=new MediaRecorder();
    recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setVideoFrameRate(config.frameRate);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
    recorder.setVideoSize(720, 1280);
    recorder.setVideoEncodingBitRate(config.bitRate);
    recorder.setOutputFile(output.getAbsolutePath());

    try {
      recorder.prepare();
      vdisplay=projection.createVirtualDisplay("andcorder",
              720, 1280, config.density,
        VIRT_DISPLAY_FLAGS, recorder.getSurface(), null, null);
      beeper.startTone(ToneGenerator.TONE_PROP_ACK);
      recorder.start();
    }
    catch (IOException e) {
      throw new RuntimeException("Exception preparing recorder", e);
    }
  }

(Example 3) - also works fine

  void start() {
    recorder=new MediaRecorder();
    recorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.WEBM);
    recorder.setVideoFrameRate(config.frameRate);
    recorder.setVideoEncoder(MediaRecorder.VideoEncoder.VP8);
    recorder.setVideoSize(1080, 1920);
    recorder.setVideoEncodingBitRate(config.bitRate);
    recorder.setOutputFile(output.getAbsolutePath());

    try {
      recorder.prepare();
      vdisplay=projection.createVirtualDisplay("andcorder",
              1080, 1920, config.density,
        VIRT_DISPLAY_FLAGS, recorder.getSurface(), null, null);
      beeper.startTone(ToneGenerator.TONE_PROP_ACK);
      recorder.start();
    }
    catch (IOException e) {
      throw new RuntimeException("Exception preparing recorder", e);
    }
  }

As you can see, i can record in 1080p (and any other resolution over 720p) using webm/vp8, but not using mp4/h264. Than you may ask: Why are you not using webm, it works! Because of this:

https://i.stack.imgur.com/6hYMG.png

Look at the red marked fields. Webm doesn´t save video´s length, frame- and bitrate. Also i got problems recording audio within webm. I would like to use mp4/h264, but it fails on resolutions higher than 720p. Any solution?

EDIT: My test-device is a LG G4 / Android 6.0

Bleuzen
  • 131
  • 1
  • 7
  • The capabilities are presumably dictated by hardware. And I can't rule out a bug in the relevant Android subsystems. – CommonsWare Jul 29 '16 at 12:24
  • @CommonsWare I tryed other apps like this from the play store. The 2 apps i tryed, worked, also in 1080 and 1440p AND audio worked. If i implement audio recording in your code, it crashes. Result: this is not a problem / limit of my device, but a problem with your code, but i don't have an idea, what i have to change, to get it working like the other apps. – Bleuzen Jul 30 '16 at 19:04
  • And ... Yes, the apps i tryed only works in android 5.0 or newer, so they should use the same API. – Bleuzen Jul 30 '16 at 19:11
  • did you find a solution? seems my issue is similar you yours https://stackoverflow.com/questions/51332386/mediarecorder-and-videosource-surface-stop-failed-1007-a-serious-android-bug – user25 Jul 13 '18 at 20:36
  • and yes it works WEBM/VP8 ok, but yes it output details are quite weird (for example it shows fps 100 in MX Player, when I set 24 for MediaRecorder..) – user25 Jul 13 '18 at 21:19
  • @user25 No. I guess it was an Android bug. It worked after changing the rom or phone. – Bleuzen Jul 14 '18 at 22:13
  • @Bleuzen I tested 5-6 devices, seems it's very popular bug, it you did app only for yourself and didn't really bother then yeah it's ok, but when you want to make app for everyone it's really a headache if it doesn't work 99% for all devices.. – user25 Jul 14 '18 at 22:50
  • @user25 Yep, this one is realy bad. I don't know how, but there is for example the app "AzScreenRecorder" which bypasses this bug somehow. But sadly I don't know what they did. Sorry, but I can't help you with this :/ – Bleuzen Jul 16 '18 at 14:18

0 Answers0