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:
Lightweight screen and audio recording Android library
Requires API level 21>
Download the demo app here
Add the following in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url'https://jitpack.io' } }
}Implement library in your app level build.gradle:
dependencies {
implementation'com.github.HBiSoft:HBRecorder:3.0.9'
}- In your
Activity, first implementHBRecorder, as shown below:
publicclassMainActivityextendsAppCompatActivityimplementsHBRecorderListener {Alt+Enterto implement the following methods:
@OverridepublicvoidHBRecorderOnStart() {
//When the recording starts
}
@OverridepublicvoidHBRecorderOnComplete() {
//After file was created
}
@OverridepublicvoidHBRecorderOnError(interrorCode) {
//When an error occurs
}
@OverridepublicvoidHBRecorderOnPause() {
//When recording was paused
}
@OverridepublicvoidHBRecorderOnResume() {
//When recording was resumed
}- Init
HBRecorderas shown below:
publicclassMainActivityextendsAppCompatActivityimplementsHBRecorderListener {
HBRecorderhbRecorder;
@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //Init HBRecorderhbRecorder = newHBRecorder(this, this); }- Add the following permissions in your manifest:
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permissionandroid:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permissionandroid:name="android.permission.RECORD_AUDIO" />
<uses-permissionandroid:name="android.permission.FOREGROUND_SERVICE" />
<!-- ForSDK34 -->
<uses-permissionandroid:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" />That's it, HBRecorder is now ready to be used.
privatevoidstartRecordingScreen() {
MediaProjectionManagermediaProjectionManager = (MediaProjectionManager) getSystemService(Context.MEDIA_PROJECTION_SERVICE);
IntentpermissionIntent = mediaProjectionManager != null ? mediaProjectionManager.createScreenCaptureIntent() : null;
startActivityForResult(permissionIntent, SCREEN_RECORD_REQUEST_CODE);
}
@OverrideprotectedvoidonActivityResult(intrequestCode, intresultCode, Intentdata) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SCREEN_RECORD_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//Start screen recordinghbRecorder.startScreenRecording(data, resultCode);
}
}
}// Set the output path as a String// Only use this on devices running Android 9 and lower or you have to add android:requestLegacyExternalStorage="true" in your manifest// Defaults to - Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES)hbrecorder.setOutputPath(String);
// Set output Uri// Only use this on devices running 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.setOutputUri(Uri);
// Set file name as String// Defaults to - quality + time stamp. For example HD-2019-08-14-10-09-58.mp4hbrecorder.setFileName(String);
// Set audio bitrate as int// Defaults to - 128000hbrecorder.setAudioBitrate(int);
// Set audio sample rate as int // Defaults to - 44100hbrecorder.setAudioSamplingRate(int);
// Enable/Disable audio// Defaults to truehbrecorder.isAudioEnabled(boolean);
// Enable/Disable HD Video// Defaults to truehbrecorder.recordHDVideo(boolean);
// Get file path as Stringhbrecorder.getFilePath();
// Get file name as Stringhbrecorder.getFileName();
// Start recording screen by passing it as Intent inside onActivityResulthbrecorder.startScreenRecording(Intent);
// Pause screen recording (only available for devices running 24>)hbrecorder.pauseScreenRecording();
// Resume screen recordinghbreccorder.resumeScreenRecording();
// Stop screen recordinghbrecorder.stopScreenRecording();
// Check if recording is in progresshbrecorder.isBusyRecording();
// Set notification icon by passing, for example R.drawable.myicon// Defaults to R.drawable.iconhbrecorder.setNotificationSmallIcon(int);
// Set notification icon using byte arrayhbrecorder.setNotificationSmallIcon(byte[]);
// Set notification icon using vector drawablehbRecorder.setNotificationSmallIconVector(vector);
// Set notification title// Defaults to "Recording your screen"hbrecorder.setNotificationTitle(String);
// Set notification description// Defaults to "Drag down to stop the recording"hbrecorder.setNotificationDescription(String);
// Set notification stop button text// Defaults to "STOP RECORDING"hbrecorder.setNotificationButtonText(String);
// Set output orientation (in degrees)hbrecorder.setOrientationHint(int);
// Set max output file sizehbrecorder.setMaxFileSize(long);
// Set max time (in seconds)hbRecorder.setMaxDuration(int);When you want to enable custom settings you must first call:
hbRecorder.enableCustomSettings();Then you can set the following:
//MUST BE ONE OF THE FOLLOWING - https://developer.android.com/reference/android/media/MediaRecorder.AudioSource.htmlhbRecorder.setAudioSource(String);
//MUST BE ONE OF THE FOLLOWING - https://developer.android.com/reference/android/media/MediaRecorder.VideoEncoder.htmlhbRecorder.setVideoEncoder(String);
//If nothing is provided, it will select the highest value supported by your devicehbRecorder.setScreenDimensions(HeightInPx, WidthInPx);
//Frame rate is device dependent//You can use Camcoderprofile to determine the frame ratehbRecorder.setVideoFrameRate(int);
//The bitrate is also dependent on the device and the frame rate that is sethbRecorder.setVideoBitrate(int);
//MUST BE ONE OF THE FOLLOWING - https://developer.android.com/reference/android/media/MediaRecorder.OutputFormat.htmlhbRecorder.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.


