Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,10 @@ Then you can set the following:
hbRecorder.setAudioSource(String);
//MUST BE ONE OF THE FOLLOWING - https://developer.android.com/reference/android/media/MediaRecorder.VideoEncoder.html
hbRecorder.setVideoEncoder(String);
//Use this If you want to set resolution of the video like 720 or 1080 etc. for example if you want your video to
//be of 1920x1080 then pass 1920 to this function it will calculate the width according to device resolution,
//while maintaining the aspect ratio.
hbRecorder.setResolution(HeightInPx);
//If nothing is provided, it will select the highest value supported by your device
hbRecorder.setScreenDimensions(HeightInPx, WidthInPx);
//Frame rate is device dependent
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ android {
compileSdk 34
defaultConfig {
applicationId "com.hbisoft.hbrecorderexample"
minSdk 17
minSdk 19
targetSdk 34
versionCode 1
versionName "1.0"
Expand Down
121 changes: 77 additions & 44 deletions app/src/main/java/com/hbisoft/hbrecorderexample/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.hbisoft.hbrecorderexample;

import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static com.hbisoft.hbrecorder.Constants.MAX_FILE_SIZE_REACHED_ERROR;
import static com.hbisoft.hbrecorder.Constants.SETTINGS_ERROR;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentValues;
Expand All @@ -20,19 +24,18 @@
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import androidx.appcompat.widget.SwitchCompat;
import android.widget.Toast;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

Expand All @@ -49,10 +52,6 @@
import java.util.Locale;
import java.util.Map;

import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
import static com.hbisoft.hbrecorder.Constants.MAX_FILE_SIZE_REACHED_ERROR;
import static com.hbisoft.hbrecorder.Constants.SETTINGS_ERROR;


/**
* Created by HBiSoft on 13 Aug 2019
Expand Down Expand Up @@ -151,8 +150,8 @@ protected void onCreate(Bundle savedInstanceState) {
Log.e("HBRecorderCodecInfo", "defaultVideoEncoder for (" + mMimeType + ") -> " + defaultVideoEncoder);
Log.e("HBRecorderCodecInfo", "MaxSupportedFrameRate -> " + hbRecorderCodecInfo.getMaxSupportedFrameRate(mWidth, mHeight, mMimeType));
Log.e("HBRecorderCodecInfo", "MaxSupportedBitrate -> " + hbRecorderCodecInfo.getMaxSupportedBitrate(mMimeType));
Log.e("HBRecorderCodecInfo", "isSizeAndFramerateSupported @ Width = "+mWidth+" Height = "+mHeight+" FPS = "+mFPS+" -> " + isSizeAndFramerateSupported);
Log.e("HBRecorderCodecInfo", "isSizeSupported @ Width = "+mWidth+" Height = "+mHeight+" -> " + hbRecorderCodecInfo.isSizeSupported(mWidth, mHeight, mMimeType));
Log.e("HBRecorderCodecInfo", "isSizeAndFramerateSupported @ Width = " + mWidth + " Height = " + mHeight + " FPS = " + mFPS + " -> " + isSizeAndFramerateSupported);
Log.e("HBRecorderCodecInfo", "isSizeSupported @ Width = " + mWidth + " Height = " + mHeight + " -> " + hbRecorderCodecInfo.isSizeSupported(mWidth, mHeight, mMimeType));
Log.e("HBRecorderCodecInfo", "Default Video Format = " + hbRecorderCodecInfo.getDefaultVideoFormat());

HashMap<String, String> supportedVideoMimeTypes = hbRecorderCodecInfo.getSupportedVideoMimeTypes();
Expand All @@ -169,7 +168,7 @@ protected void onCreate(Bundle savedInstanceState) {
for (int j = 0; j < supportedVideoFormats.size(); j++) {
Log.e("HBRecorderCodecInfo", "Available Video Formats : " + supportedVideoFormats.get(j));
}
}else{
} else {
Log.e("HBRecorderCodecInfo", "MimeType not supported");
}

Expand Down Expand Up @@ -222,7 +221,7 @@ && checkSelfPermission(Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION,
hasPermissions = true;
saveAudioPreference(true);
}
}else{
} else {
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS, PERMISSION_REQ_POST_NOTIFICATIONS)
&& checkSelfPermission(Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION, PERMISSION_REQ_ID_FOREGROUND_SERVICE_MEDIA_PROJECTION)) {
hasPermissions = true;
Expand All @@ -236,7 +235,7 @@ && checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECOR
hasPermissions = true;
saveAudioPreference(true);
}
}else{
} else {
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS, PERMISSION_REQ_POST_NOTIFICATIONS)) {
hasPermissions = true;
}
Expand Down Expand Up @@ -264,13 +263,13 @@ && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_RE
} else {
// else start recording
if (!hasAudioPermissions && isAudioEnabled) {
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)){
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) {
hasPermissions = true;
saveAudioPreference(true);
startRecordingScreen();
}

}else {
} else {
startRecordingScreen();
}
}
Expand Down Expand Up @@ -324,12 +323,12 @@ public void HBRecorderOnComplete() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//Update gallery depending on SDK Level
if (hbRecorder.wasUriSet()) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q ) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
updateGalleryUri();
} else {
refreshGalleryFile();
}
}else{
} else {
refreshGalleryFile();
}
}
Expand All @@ -348,7 +347,7 @@ public void HBRecorderOnError(int errorCode, String reason) {

if (errorCode == SETTINGS_ERROR) {
showLongToast(getString(R.string.settings_not_supported_message));
} else if ( errorCode == MAX_FILE_SIZE_REACHED_ERROR) {
} else if (errorCode == MAX_FILE_SIZE_REACHED_ERROR) {
showLongToast(getString(R.string.max_file_size_reached_message));
} else {
showLongToast(getString(R.string.general_recording_error_message));
Expand Down Expand Up @@ -384,7 +383,7 @@ public void onScanCompleted(String path, Uri uri) {
}

@RequiresApi(api = Build.VERSION_CODES.Q)
private void updateGalleryUri(){
private void updateGalleryUri() {
contentValues.clear();
contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
getContentResolver().update(mUri, contentValues, null, null);
Expand Down Expand Up @@ -412,6 +411,7 @@ private void startRecordingScreen() {
}

String output_format;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
// Example of how to set custom settings
private void customSettings() {
Expand Down Expand Up @@ -462,28 +462,8 @@ private void customSettings() {
}
}

//NOTE - THIS MIGHT NOT BE SUPPORTED SIZES FOR YOUR DEVICE
//Video Dimensions
String video_resolution = prefs.getString("key_video_resolution", null);
if (video_resolution != null) {
switch (video_resolution) {
case "0":
hbRecorder.setScreenDimensions(426, 240);
break;
case "1":
hbRecorder.setScreenDimensions(640, 360);
break;
case "2":
hbRecorder.setScreenDimensions(854, 480);
break;
case "3":
hbRecorder.setScreenDimensions(1280, 720);
break;
case "4":
hbRecorder.setScreenDimensions(1920, 1080);
break;
}
}
// setPrefsCustomDimensions(prefs);
setPrefCustomResolution(prefs);

//Video Frame Rate
String video_frame_rate = prefs.getString("key_video_fps", null);
Expand Down Expand Up @@ -562,6 +542,58 @@ private void customSettings() {

}

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setPrefCustomResolution(SharedPreferences prefs) {
String video_resolution = prefs.getString("key_video_resolution", null);
if (video_resolution != null) {
switch (video_resolution) {
case "0":
hbRecorder.setResolution(426);
break;
case "1":
hbRecorder.setResolution(640);
break;
case "2":
hbRecorder.setResolution(854);
break;
case "3":
hbRecorder.setResolution(1280);
break;
case "4":
hbRecorder.setResolution(1920);
break;
}
}
}


@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setPrefsCustomDimensions(SharedPreferences prefs) {
//NOTE - THIS MIGHT NOT BE SUPPORTED SIZES FOR YOUR DEVICE, AND WILL INTRODUCE BLACK BARS
// ON SIDE OF VIDEOS
//Video Dimensions
String video_resolution = prefs.getString("key_video_resolution", null);
if (video_resolution != null) {
switch (video_resolution) {
case "0":
hbRecorder.setScreenDimensions(426, 240);
break;
case "1":
hbRecorder.setScreenDimensions(640, 360);
break;
case "2":
hbRecorder.setScreenDimensions(854, 480);
break;
case "3":
hbRecorder.setScreenDimensions(1280, 720);
break;
case "4":
hbRecorder.setScreenDimensions(1920, 1080);
break;
}
}
}

//Get/Set the selected settings
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void quickSettings() {
Expand Down Expand Up @@ -629,7 +661,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (isAudioEnabled) {
checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO);
}else {
} else {
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE);
}
} else {
Expand Down Expand Up @@ -684,7 +716,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//Start screen recording
hbRecorder.startScreenRecording(data, resultCode);

}else{
} else {
startbtn.setText(R.string.start_recording);
}
}
Expand All @@ -698,6 +730,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
ContentResolver resolver;
ContentValues contentValues;
Uri mUri;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void setOutputPath() {
String filename = generateFileName();
Expand All @@ -709,16 +742,16 @@ private void setOutputPath() {
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename);
if (output_format != null) {
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, getMimeTypeForOutputFormat(output_format));
}else {
} else {
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
}
mUri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
//FILE NAME SHOULD BE THE SAME
hbRecorder.setFileName(filename);
hbRecorder.setOutputUri(mUri);
}else{
} else {
createFolder();
hbRecorder.setOutputPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) +"/HBRecorder");
hbRecorder.setOutputPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) + "/HBRecorder");
}
}

Expand Down
10 changes: 5 additions & 5 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="pref_video_resolution">
<item>426x240</item>
<item>640x360</item>
<item>854x480</item>
<item>1280x720</item>
<item>1920x1080</item>
<item>240p</item>
<item>360p</item>
<item>480p</item>
<item>720p</item>
<item>1080p</item>
<item>Default</item>
</string-array>
<string-array name="pref_video_framerate">
Expand Down
3 changes: 2 additions & 1 deletion hbrecorder/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
compileSdk 34

defaultConfig {
minSdk 17
minSdk 19
targetSdk 34

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down Expand Up @@ -34,6 +34,7 @@ dependencies {

implementation 'androidx.appcompat:appcompat:1.6.1'
testImplementation 'junit:junit:4.13.2'
implementation "androidx.window:window:1.2.0"
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
Expand Down
Loading