项目作者: HBiSoft

项目描述 :
Lightweight screen recording Android library
高级语言: Java
项目地址: git://github.com/HBiSoft/HBRecorder.git
创建时间: 2019-08-14T07:11:00Z
项目社区:https://github.com/HBiSoft/HBRecorder

开源协议:MIT License

下载


Creating and maintaining a library like this requires a significant amount of time and effort.

If you’d like to show your appreciation, you can do so below:

Buy Me A Coffee



HBRecorder


Android Arsenal

Lightweight screen and audio recording Android library

Requires API level 21>


Demo:

Download the demo app here



Adding the library to your project:

Add the following in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. ...
  4. maven { url 'https://jitpack.io' }
  5. }
  6. }

Implement library in your app level build.gradle:

  1. dependencies {
  2. implementation 'com.github.HBiSoft:HBRecorder:3.0.9'
  3. }

Implementing the library:

  1. In your Activity, first implement HBRecorder, as shown below:
  1. public class MainActivity extends AppCompatActivity implements HBRecorderListener {
  1. Alt+Enter to implement the following methods:
  1. @Override
  2. public void HBRecorderOnStart() {
  3. //When the recording starts
  4. }
  5. @Override
  6. public void HBRecorderOnComplete() {
  7. //After file was created
  8. }
  9. @Override
  10. public void HBRecorderOnError(int errorCode) {
  11. //When an error occurs
  12. }
  13. @Override
  14. public void HBRecorderOnPause() {
  15. //When recording was paused
  16. }
  17. @Override
  18. public void HBRecorderOnResume() {
  19. //When recording was resumed
  20. }
  1. Init HBRecorder as shown below:
    ```java
    public class MainActivity extends AppCompatActivity implements HBRecorderListener {
    HBRecorder hbRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

    1. super.onCreate(savedInstanceState);
    2. setContentView(R.layout.activity_main);
    3. //Init HBRecorder
    4. hbRecorder = new HBRecorder(this, this);

}

  1. 4. Add the following permissions in your manifest:
  2. ```java
  3. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>
  4. <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" ></uses-permission>
  5. <uses-permission android:name="android.permission.RECORD_AUDIO" ></uses-permission>
  6. <uses-permission android:name="android.permission.FOREGROUND_SERVICE" ></uses-permission>
  7. <!-- For SDK 34 -->
  8. <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" ></uses-permission>

That’s it, HBRecorder is now ready to be used.


When you want to start capturing your screen, it is important you do it as shown below:

  1. private void startRecordingScreen() {
  2. MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
  3. Intent permissionIntent = mediaProjectionManager != null ? mediaProjectionManager.createScreenCaptureIntent() : null;
  4. startActivityForResult(permissionIntent, SCREEN_RECORD_REQUEST_CODE);
  5. }
  6. @Override
  7. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  8. super.onActivityResult(requestCode, resultCode, data);
  9. if (requestCode == SCREEN_RECORD_REQUEST_CODE) {
  10. if (resultCode == RESULT_OK) {
  11. //Start screen recording
  12. hbRecorder.startScreenRecording(data, resultCode);
  13. }
  14. }
  15. }

All available methods:

  1. // Set the output path as a String
  2. // Only use this on devices running Android 9 and lower or you have to add android:requestLegacyExternalStorage="true" in your manifest
  3. // Defaults to - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)
  4. hbrecorder.setOutputPath(String);
  5. // Set output Uri
  6. // Only use this on devices running Android 10>
  7. // When setting a Uri ensure you pass the same name to HBRecorder as what you set in ContentValues (DISPLAY_NAME and TITLE)
  8. hbRecorder.setOutputUri(Uri);
  9. // Set file name as String
  10. // Defaults to - quality + time stamp. For example HD-2019-08-14-10-09-58.mp4
  11. hbrecorder.setFileName(String);
  12. // Set audio bitrate as int
  13. // Defaults to - 128000
  14. hbrecorder.setAudioBitrate(int);
  15. // Set audio sample rate as int
  16. // Defaults to - 44100
  17. hbrecorder.setAudioSamplingRate(int);
  18. // Enable/Disable audio
  19. // Defaults to true
  20. hbrecorder.isAudioEnabled(boolean);
  21. // Enable/Disable HD Video
  22. // Defaults to true
  23. hbrecorder.recordHDVideo(boolean);
  24. // Get file path as String
  25. hbrecorder.getFilePath();
  26. // Get file name as String
  27. hbrecorder.getFileName();
  28. // Start recording screen by passing it as Intent inside onActivityResult
  29. hbrecorder.startScreenRecording(Intent);
  30. // Pause screen recording (only available for devices running 24>)
  31. hbrecorder.pauseScreenRecording();
  32. // Resume screen recording
  33. hbreccorder.resumeScreenRecording();
  34. // Stop screen recording
  35. hbrecorder.stopScreenRecording();
  36. // Check if recording is in progress
  37. hbrecorder.isBusyRecording();
  38. // Set notification icon by passing, for example R.drawable.myicon
  39. // Defaults to R.drawable.icon
  40. hbrecorder.setNotificationSmallIcon(int);
  41. // Set notification icon using byte array
  42. hbrecorder.setNotificationSmallIcon(byte[]);
  43. // Set notification icon using vector drawable
  44. hbRecorder.setNotificationSmallIconVector(vector);
  45. // Set notification title
  46. // Defaults to "Recording your screen"
  47. hbrecorder.setNotificationTitle(String);
  48. // Set notification description
  49. // Defaults to "Drag down to stop the recording"
  50. hbrecorder.setNotificationDescription(String);
  51. // Set notification stop button text
  52. // Defaults to "STOP RECORDING"
  53. hbrecorder.setNotificationButtonText(String);
  54. // Set output orientation (in degrees)
  55. hbrecorder.setOrientationHint(int);
  56. // Set max output file size
  57. hbrecorder.setMaxFileSize(long);
  58. // Set max time (in seconds)
  59. hbRecorder.setMaxDuration(int);

Custom setting:

When you want to enable custom settings you must first call:

  1. hbRecorder.enableCustomSettings();

Then you can set the following:

  1. //MUST BE ONE OF THE FOLLOWING - https://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html
  2. hbRecorder.setAudioSource(String);
  3. //MUST BE ONE OF THE FOLLOWING - https://developer.android.com/reference/android/media/MediaRecorder.VideoEncoder.html
  4. hbRecorder.setVideoEncoder(String);
  5. //If nothing is provided, it will select the highest value supported by your device
  6. hbRecorder.setScreenDimensions(HeightInPx, WidthInPx);
  7. //Frame rate is device dependent
  8. //You can use Camcoderprofile to determine the frame rate
  9. hbRecorder.setVideoFrameRate(int);
  10. //The bitrate is also dependent on the device and the frame rate that is set
  11. hbRecorder.setVideoBitrate(int);
  12. //MUST BE ONE OF THE FOLLOWING - https://developer.android.com/reference/android/media/MediaRecorder.OutputFormat.html
  13. hbRecorder.setOutputFormat(String);

It is important to note that limitations are device dependent. It is best to set the video encoder to “DEFAULT” and let MediaRecorder pick the best encoder.

In the demo app you will have the option to test different video encoders, bitrate, frame rate and output formats. If your device does not support any of the parameters you have selected HBRecorderOnError will be called.