diff --git a/.idea/gradle.xml b/.idea/gradle.xml index c6f77c6..e199e83 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -1,11 +1,11 @@ + diff --git a/app/release/HBRecorderDemo.apk b/app/release/HBRecorderDemo.apk deleted file mode 100644 index ec7b56c..0000000 Binary files a/app/release/HBRecorderDemo.apk and /dev/null differ diff --git a/app/release/output.json b/app/release/output.json deleted file mode 100644 index c429e31..0000000 --- a/app/release/output.json +++ /dev/null @@ -1 +0,0 @@ -[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":1,"versionName":"1.0","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file diff --git a/app/src/main/java/com/hbisoft/hbrecorderexample/MainActivity.java b/app/src/main/java/com/hbisoft/hbrecorderexample/MainActivity.java index fe2ff3c..9303ea8 100644 --- a/app/src/main/java/com/hbisoft/hbrecorderexample/MainActivity.java +++ b/app/src/main/java/com/hbisoft/hbrecorderexample/MainActivity.java @@ -1,6 +1,8 @@ package com.hbisoft.hbrecorderexample; import android.Manifest; +import android.content.ContentResolver; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -14,6 +16,7 @@ import android.os.Bundle; import android.os.Environment; import android.preference.PreferenceManager; +import android.provider.MediaStore; import android.util.Log; import android.view.Menu; import android.view.MenuItem; @@ -37,6 +40,9 @@ import java.io.ByteArrayOutputStream; import java.io.File; +import java.sql.Date; +import java.text.SimpleDateFormat; +import java.util.Locale; /** @@ -69,6 +75,7 @@ * * */ +@SuppressWarnings({"deprecation", "SameParameterValue"}) public class MainActivity extends AppCompatActivity implements HBRecorderListener { //Permissions private static final int SCREEN_RECORD_REQUEST_CODE = 777; @@ -101,18 +108,14 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - createFolder(); initViews(); - mOnClickListeners(); - radioGroupCheckListener(); - recordAudioCheckBoxListener(); + setOnClickListeners(); + setRadioGroupCheckListener(); + setRecordAudioCheckBoxListener(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { //Init HBRecorder hbRecorder = new HBRecorder(this, this); - hbRecorder.setOutputPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) +"/HBRecorder"); - hbRecorder.setAudioBitrate(128000); - hbRecorder.setAudioSamplingRate(44100); //When the user returns to the application, some UI changes might be necessary, //check if recording is in progress and make changes accordingly @@ -124,6 +127,8 @@ protected void onCreate(Bundle savedInstanceState) { } //Create Folder + //Only call this on Android 9 and lower (getExternalStoragePublicDirectory is deprecated) + //This can still be used on Android 10> but you will have to add android:requestLegacyExternalStorage="true" in your Manifest private void createFolder() { File f1 = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES), "HBRecorder"); if (!f1.exists()) { @@ -142,7 +147,7 @@ private void initViews() { } //Start Button OnClickListener - private void mOnClickListeners() { + private void setOnClickListeners() { startbtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -171,7 +176,7 @@ public void onClick(View v) { } //Check if HD/SD Video should be recorded - private void radioGroupCheckListener() { + private void setRadioGroupCheckListener() { radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { @@ -191,7 +196,7 @@ public void onCheckedChanged(RadioGroup radioGroup, int checkedId) { } //Check if audio should be recorded - private void recordAudioCheckBoxListener() { + private void setRecordAudioCheckBoxListener() { recordAudioCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { @@ -208,13 +213,18 @@ public void HBRecorderOnComplete() { startbtn.setText(R.string.start_recording); showLongToast("Saved Successfully"); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - refreshGallery(); + //Update gallery depending on SDK Level + if (hbRecorder.wasUriSet()) { + updateGalleryUri(); + }else{ + refreshGalleryFile(); + } } } @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) - private void refreshGallery() { + private void refreshGalleryFile() { MediaScannerConnection.scanFile(this, new String[]{hbRecorder.getFilePath()}, null, new MediaScannerConnection.OnScanCompletedListener() { @@ -225,6 +235,12 @@ public void onScanCompleted(String path, Uri uri) { }); } + private void updateGalleryUri(){ + contentValues.clear(); + contentValues.put(MediaStore.Video.Media.IS_PENDING, 0); + getContentResolver().update(mUri, contentValues, null, null); + } + @Override public void HBRecorderOnError(int errorCode, String reason) { // Error 38 happens when @@ -419,6 +435,8 @@ private void customSettings() { //Get/Set the selected settings @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) private void quickSettings() { + hbRecorder.setAudioBitrate(128000); + hbRecorder.setAudioSamplingRate(44100); hbRecorder.recordHDVideo(wasHDSelected); hbRecorder.isAudioEnabled(isAudioEnabled); //Customise Notification @@ -492,6 +510,8 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (requestCode == SCREEN_RECORD_REQUEST_CODE) { if (resultCode == RESULT_OK) { + //Set file path or Uri depending on SDK version + setOutputPath(); //Start screen recording hbRecorder.startScreenRecording(data, resultCode, this); @@ -500,6 +520,40 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) { } } + //For Android 10> we will pass a Uri to HBRecorder + //This is not necessary - You can still use getExternalStoragePublicDirectory + //But then you will have to add android:requestLegacyExternalStorage="true" in your Manifest + //IT IS IMPORTANT TO SET THE FILE NAME THE SAME AS THE NAME YOU USE FOR TITLE AND DISPLAY_NAME + ContentResolver resolver; + ContentValues contentValues; + Uri mUri; + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) + private void setOutputPath() { + String filename = generateFileName(); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + resolver = getContentResolver(); + contentValues = new ContentValues(); + 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, "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{ + createFolder(); + hbRecorder.setOutputPath(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES) +"/HBRecorder"); + } + } + + //Generate a timestamp to be used as a file name + private String generateFileName() { + SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.getDefault()); + Date curDate = new Date(System.currentTimeMillis()); + return formatter.format(curDate).replace(" ", ""); + } + //Show Toast private void showLongToast(final String msg) { Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); diff --git a/hbrecorder/src/main/AndroidManifest.xml b/hbrecorder/src/main/AndroidManifest.xml index 8af6f19..7739822 100644 --- a/hbrecorder/src/main/AndroidManifest.xml +++ b/hbrecorder/src/main/AndroidManifest.xml @@ -3,7 +3,6 @@ package="com.hbisoft.hbrecorder">