HBRecorder Documentation

repository·master·Indexed 19 days ago

https://github.com/hbisoft/hbrecorder

A lightweight Android library for capturing screen and audio recordings. HBRecorder simplifies MediaProjection and MediaRecorder APIs, requiring API level 21 or higher. It provides a manageable interface for recording control, custom settings for encoders and bitrates, and a listener interface for lifecycle and error callbacks.

Tokens
2.8K
Snippets
7
Records
7
Agent score
18%

What's inside HBRecorder

  1. Configure Custom Recording Settings

    master

    To use advanced settings like custom encoders, bitrates, or dimensions, you must first call hbRecorder.enableCustomSettings().

    Warning: If a selected parameter is not supported by the device, HBRecorderOnError will be triggered. It is recommended to set the video encoder to "DEFAULT" to allow MediaRecorder to select the best available option.

    hbRecorder.enableCustomSettings();
    
    // Audio Source (Must be a valid MediaRecorder.AudioSource constant)
    hbRecorder.setAudioSource(String);
    
    // Video Encoder (Must be a valid MediaRecorder.VideoEncoder constant)
    hbRecorder.setVideoEncoder(String);
    
    // Screen Dimensions (Height, Width)
    hbRecorder.setScreenDimensions(HeightInPx, WidthInPx);
    
    // Video Parameters
    hbRecorder.setVideoFrameRate(int); // Frame rate is device dependent
    hbRecorder.setVideoBitrate(int); // Bitrate depends on device and frame rate
    
    // Output Format (Must be a valid MediaRecorder.OutputFormat constant)
    hbRecorder.setOutputFormat(String);
  2. Start Screen Recording

    master

    To start recording, you must first request screen capture permission from the user using MediaProjectionManager. Once the user grants permission in onActivityResult, pass the resulting Intent and resultCode to hbRecorder.startScreenRecording().

    private void startRecordingScreen() {
        MediaProjectionManager mediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
        Intent permissionIntent = mediaProjectionManager != null ? mediaProjectionManager.createScreenCaptureIntent() : null;
        startActivityForResult(permissionIntent, SCREEN_RECORD_REQUEST_CODE);
    }
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == SCREEN_RECORD_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // Start screen recording
                hbRecorder.startScreenRecording(data, resultCode);
            }
        }
    }
  3. Install HBRecorder via JitPack

    master

    To use HBRecorder in your Android project, add the JitPack repository to your root build.gradle and then implement the dependency in your app-level build.gradle.

    Note: HBRecorder requires API level 21 or higher.

    // Root build.gradle
    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
    
    // App level build.gradle
    dependencies {
        implementation 'com.github.HBiSoft:HBRecorder:3.0.9'
    }
  4. Configure Permissions for HBRecorder

    master

    Add the following permissions to your AndroidManifest.xml. For devices running SDK 34, you must also include the FOREGROUND_SERVICE_MEDIA_PROJECTION permission.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <!-- For SDK 34 -->
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />
  5. Implement HBRecorderListener

    master

    Your Activity must implement the HBRecorderListener interface to receive lifecycle and error callbacks from the recorder.

    public class MainActivity extends AppCompatActivity implements HBRecorderListener {
    
        @Override
        public void HBRecorderOnStart() {
            // When the recording starts
        }
    
        @Override
        public void HBRecorderOnComplete() {
            // After file was created
        }
    
        @Override
        public void HBRecorderOnError(int errorCode) {
            // When an error occurs
        }
    
        @Override
        public void HBRecorderOnPause() {
            // When recording was paused
        }
    
        @Override
        public void HBRecorderOnResume() {
            // When recording was resumed
        }
    }
  6. Initialize HBRecorder

    master

    Initialize the HBRecorder instance within your Activity's onCreate method, passing this as the context and the listener.

    public class MainActivity extends AppCompatActivity implements HBRecorderListener {
        HBRecorder hbRecorder;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState); 
            setContentView(R.layout.activity_main);     
    
            // Init HBRecorder
            hbRecorder = new HBRecorder(this, this);        
        }
    }
  7. Reference: HBRecorder Public Methods

    master

    The following methods are available on the HBRecorder instance to control recording parameters and lifecycle.

    // Output Configuration
    hbrecorder.setOutputPath(String); // For Android 9 and lower. For Android 10+, use setOutputUri.
    // Note: For Android 10+, add android:requestLegacyExternalStorage="true" in manifest if using setOutputPath
    hbrecorder.setOutputUri(Uri); // For Android 10+.
    // When setting a Uri, ensure you pass the same name to HBRecorder as what you set in ContentValues (DISPLAY_NAME and TITLE)
    hbrecorder.setFileName(String); // Defaults to quality + timestamp (e.g., HD-2019-08-14-10-09-58.mp4)
    
    // Audio/Video Settings
    hbrecorder.setAudioBitrate(int); // Defaults to 128000
    hbrecorder.setAudioSamplingRate(int); // Defaults to 44100
    hbrecorder.isAudioEnabled(boolean); // Defaults to true
    hbrecorder.recordHDVideo(boolean); // Defaults to true
    
    // Recording Control
    hbrecorder.startScreenRecording(Intent); // Start recording
    hbrecorder.pauseScreenRecording(); // Only for devices running Android 24+
    // hbrecorder.resumeScreenRecording(); // Resume recording
    hbrecorder.stopScreenRecording(); // Stop recording
    hbrecorder.isBusyRecording(); // Check if recording is in progress
    
    // Metadata & Notifications
    hbrecorder.getFilePath(); // Get file path as String
    hbrecorder.getFileName(); // Get file name as String
    hbrecorder.setNotificationSmallIcon(int); // Set icon via resource ID
    hbrecorder.setNotificationSmallIcon(byte[]); // Set icon via byte array
    hbrecorder.setNotificationSmallIconVector(vector); // Set icon via vector drawable
    hbrecorder.setNotificationTitle(String); // Defaults to "Recording your screen"
    hbrecorder.setNotificationDescription(String); // Defaults to "Drag down to stop the recording"
    hbrecorder.setNotificationButtonText(String); // Defaults to "STOP RECORDING"
    
    // Constraints
    hbrecorder.setOrientationHint(int); // Set output orientation in degrees
    hbrecorder.setMaxFileSize(long); // Set max file size
    hbrecorder.setMaxDuration(int); // Set max duration in seconds