Skip to content

Commit

Permalink
Fix issue #174 and #176
Browse files Browse the repository at this point in the history
  • Loading branch information
HBiSoft committed Sep 15, 2024
1 parent 73acbec commit a5e3e15
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
Binary file modified app/release/HBRecorderDemo.apk
Binary file not shown.
52 changes: 34 additions & 18 deletions app/src/main/java/com/hbisoft/hbrecorderexample/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public class MainActivity extends AppCompatActivity implements HBRecorderListene
private static final int PERMISSION_REQ_ID_RECORD_AUDIO = 22;
private static final int PERMISSION_REQ_POST_NOTIFICATIONS = 33;
private static final int PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE = PERMISSION_REQ_ID_RECORD_AUDIO + 1;
private static final int PERMISSION_REQ_ID_FOREGROUND_SERVICE_MEDIA_PROJECTION = PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE + 1;
private boolean hasPermissions = false;

//Declare HBRecorder
Expand Down Expand Up @@ -199,31 +200,36 @@ private void initViews() {
private void setOnClickListeners() {
startbtn.setOnClickListener(v -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//first check if permissions was granted
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS, PERMISSION_REQ_POST_NOTIFICATIONS) && checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) {
// first check if permissions were granted
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { // SDK 34
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS, PERMISSION_REQ_POST_NOTIFICATIONS)
&& checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)
&& checkSelfPermission(Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION, PERMISSION_REQ_ID_FOREGROUND_SERVICE_MEDIA_PROJECTION)) {
hasPermissions = true;
}
}
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { // SDK 33
if (checkSelfPermission(Manifest.permission.POST_NOTIFICATIONS, PERMISSION_REQ_POST_NOTIFICATIONS)
&& checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) {
hasPermissions = true;
}
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)) {
hasPermissions = true;
}
} else {
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO) && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE)) {
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO, PERMISSION_REQ_ID_RECORD_AUDIO)
&& checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, PERMISSION_REQ_ID_WRITE_EXTERNAL_STORAGE)) {
hasPermissions = true;
}
}

if (hasPermissions) {
//check if recording is in progress
//and stop it if it is
// check if recording is in progress and stop it if it is
if (hbRecorder.isBusyRecording()) {
hbRecorder.stopScreenRecording();
startbtn.setText(R.string.start_recording);
}
//else start recording
else {
} else {
// else start recording
startRecordingScreen();
}
}
Expand Down Expand Up @@ -572,6 +578,7 @@ private boolean checkSelfPermission(String permission, int requestCode) {
}

//Handle permissions
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Expand All @@ -598,23 +605,28 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
startRecordingScreen();
} else {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
hasPermissions = true;
//Permissions was provided
//Start screen recording
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
startRecordingScreen();
}
checkSelfPermission(Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION, PERMISSION_REQ_ID_FOREGROUND_SERVICE_MEDIA_PROJECTION);
} else {
hasPermissions = false;
showLongToast("No permission for " + Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
}
break;
case PERMISSION_REQ_ID_FOREGROUND_SERVICE_MEDIA_PROJECTION:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
hasPermissions = true;
startRecordingScreen();
} else {
hasPermissions = false;
showLongToast("No permission for " + Manifest.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION);
}
break;
default:
break;
}
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Expand Down Expand Up @@ -647,7 +659,11 @@ private void setOutputPath() {
contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "HBRecorder");
contentValues.put(MediaStore.Video.Media.TITLE, filename);
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, getMimeTypeForOutputFormat(output_format));
if (output_format != null) {
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, getMimeTypeForOutputFormat(output_format));
}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);
Expand Down

0 comments on commit a5e3e15

Please sign in to comment.