1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47:29 +00:00

Fix audio recording in dm

This commit is contained in:
Ammar Githam 2021-04-06 00:51:01 +09:00
parent 8030c7f220
commit bca38a2645
9 changed files with 205 additions and 197 deletions

View File

@ -20,7 +20,6 @@
android:fullBackupContent="@xml/backup_descriptor"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:requestLegacyExternalStorage="true"
android:supportsRtl="true"
android:theme="@style/AppTheme.Launcher"
tools:ignore="UnusedAttribute">

View File

@ -26,7 +26,6 @@ import awais.instagrabber.R;
import awais.instagrabber.adapters.MediaItemsAdapter;
import awais.instagrabber.databinding.LayoutMediaPickerBinding;
import awais.instagrabber.utils.MediaController;
import awais.instagrabber.utils.PermissionUtils;
import awais.instagrabber.utils.TextUtils;
import awais.instagrabber.viewmodels.MediaPickerViewModel;
@ -121,10 +120,10 @@ public class MediaPickerBottomDialogFragment extends BottomSheetDialogFragment {
if (requestCode == ATTACH_MEDIA_REQUEST_CODE) {
final Context context = getContext();
if (context == null) return;
final boolean hasAttachMediaPerms = PermissionUtils.hasAttachMediaPerms(context);
if (hasAttachMediaPerms) {
viewModel.loadMedia(context);
}
// final boolean hasAttachMediaPerms = PermissionUtils.hasAttachMediaPerms(context);
// if (hasAttachMediaPerms) {
viewModel.loadMedia(context);
// }
}
}
@ -133,10 +132,10 @@ public class MediaPickerBottomDialogFragment extends BottomSheetDialogFragment {
setupAlbumPicker();
final Context context = getContext();
if (context == null) return;
if (!PermissionUtils.hasAttachMediaPerms(context)) {
PermissionUtils.requestAttachMediaPerms(this, ATTACH_MEDIA_REQUEST_CODE);
return;
}
// if (!PermissionUtils.hasAttachMediaPerms(context)) {
// PermissionUtils.requestAttachMediaPerms(this, ATTACH_MEDIA_REQUEST_CODE);
// return;
// }
viewModel.loadMedia(context);
}

View File

@ -52,7 +52,7 @@ public final class DownloadUtils {
private static final String DIR_DOWNLOADS = "Downloads";
private static final String DIR_CAMERA = "Camera";
private static final String DIR_EDIT = "Edit";
private static final String DIR_RECORDINGS = "Recordings";
private static final String DIR_RECORDINGS = "Sent Recordings";
private static final String DIR_TEMP = "Temp";
private static final String DIR_BACKUPS = "Backups";
@ -96,7 +96,7 @@ public final class DownloadUtils {
throw new ReselectDocumentTreeException(uri);
}
root = DocumentFile.fromTreeUri(context, uri);
Log.d(TAG, "init: " + root);
// Log.d(TAG, "init: " + root);
// final File parent = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
// final DocumentFile documentFile = DocumentFile.fromFile(parent);
// Log.d(TAG, "init: " + documentFile);
@ -111,9 +111,8 @@ public final class DownloadUtils {
if (dirs != null) {
for (final String dir : dirs) {
final DocumentFile subDirFile = subDir.findFile(dir);
if (subDirFile == null) {
subDir = subDir.createDirectory(dir);
}
final boolean exists = subDirFile != null && subDirFile.exists();
subDir = exists ? subDirFile : subDir.createDirectory(dir);
}
}
return subDir;

View File

@ -87,83 +87,83 @@ public class MediaController {
Cursor cursor = null;
try {
if (PermissionUtils.hasAttachMediaPerms(context)) {
cursor = MediaStore.Images.Media.query(context.getContentResolver(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
PROJECTION_PHOTOS,
null,
null,
(Build.VERSION.SDK_INT > 28
? MediaStore.Images.Media.DATE_TAKEN
: MediaStore.Images.Media.DATE_MODIFIED) + " DESC");
if (cursor != null) {
int imageIdColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int bucketIdColumn = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
int bucketNameColumn = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int dateColumn = cursor.getColumnIndex(Build.VERSION.SDK_INT > 28 ? MediaStore.Images.Media.DATE_TAKEN
: MediaStore.Images.Media.DATE_MODIFIED);
int orientationColumn = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
int widthColumn = cursor.getColumnIndex(MediaStore.Images.Media.WIDTH);
int heightColumn = cursor.getColumnIndex(MediaStore.Images.Media.HEIGHT);
int sizeColumn = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
// if (PermissionUtils.hasAttachMediaPerms(context)) {
cursor = MediaStore.Images.Media.query(context.getContentResolver(),
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
PROJECTION_PHOTOS,
null,
null,
(Build.VERSION.SDK_INT > 28
? MediaStore.Images.Media.DATE_TAKEN
: MediaStore.Images.Media.DATE_MODIFIED) + " DESC");
if (cursor != null) {
int imageIdColumn = cursor.getColumnIndex(MediaStore.Images.Media._ID);
int bucketIdColumn = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_ID);
int bucketNameColumn = cursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dataColumn = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int dateColumn = cursor.getColumnIndex(Build.VERSION.SDK_INT > 28 ? MediaStore.Images.Media.DATE_TAKEN
: MediaStore.Images.Media.DATE_MODIFIED);
int orientationColumn = cursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
int widthColumn = cursor.getColumnIndex(MediaStore.Images.Media.WIDTH);
int heightColumn = cursor.getColumnIndex(MediaStore.Images.Media.HEIGHT);
int sizeColumn = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
while (cursor.moveToNext()) {
String path = cursor.getString(dataColumn);
if (TextUtils.isEmpty(path)) {
continue;
}
int imageId = cursor.getInt(imageIdColumn);
int bucketId = cursor.getInt(bucketIdColumn);
String bucketName = cursor.getString(bucketNameColumn);
long dateTaken = cursor.getLong(dateColumn);
int orientation = cursor.getInt(orientationColumn);
int width = cursor.getInt(widthColumn);
int height = cursor.getInt(heightColumn);
long size = cursor.getLong(sizeColumn);
MediaEntry mediaEntry = new MediaEntry(bucketId, imageId, dateTaken, path, orientation, -1, false, width, height, size);
if (allPhotosAlbum == null) {
allPhotosAlbum = new AlbumEntry(0, context.getString(R.string.all_photos), mediaEntry);
photoAlbumsSorted.add(0, allPhotosAlbum);
}
if (allMediaAlbum == null) {
allMediaAlbum = new AlbumEntry(0, context.getString(R.string.all_media), mediaEntry);
mediaAlbumsSorted.add(0, allMediaAlbum);
}
allPhotosAlbum.addPhoto(mediaEntry);
allMediaAlbum.addPhoto(mediaEntry);
AlbumEntry albumEntry = mediaAlbums.get(bucketId);
if (albumEntry == null) {
albumEntry = new AlbumEntry(bucketId, bucketName, mediaEntry);
mediaAlbums.put(bucketId, albumEntry);
if (mediaCameraAlbumId == null && cameraFolder != null && path.startsWith(cameraFolder)) {
mediaAlbumsSorted.add(0, albumEntry);
mediaCameraAlbumId = bucketId;
} else {
mediaAlbumsSorted.add(albumEntry);
}
}
albumEntry.addPhoto(mediaEntry);
albumEntry = photoAlbums.get(bucketId);
if (albumEntry == null) {
albumEntry = new AlbumEntry(bucketId, bucketName, mediaEntry);
photoAlbums.put(bucketId, albumEntry);
if (photoCameraAlbumId == null && cameraFolder != null && path.startsWith(cameraFolder)) {
photoAlbumsSorted.add(0, albumEntry);
photoCameraAlbumId = bucketId;
} else {
photoAlbumsSorted.add(albumEntry);
}
}
albumEntry.addPhoto(mediaEntry);
while (cursor.moveToNext()) {
String path = cursor.getString(dataColumn);
if (TextUtils.isEmpty(path)) {
continue;
}
int imageId = cursor.getInt(imageIdColumn);
int bucketId = cursor.getInt(bucketIdColumn);
String bucketName = cursor.getString(bucketNameColumn);
long dateTaken = cursor.getLong(dateColumn);
int orientation = cursor.getInt(orientationColumn);
int width = cursor.getInt(widthColumn);
int height = cursor.getInt(heightColumn);
long size = cursor.getLong(sizeColumn);
MediaEntry mediaEntry = new MediaEntry(bucketId, imageId, dateTaken, path, orientation, -1, false, width, height, size);
if (allPhotosAlbum == null) {
allPhotosAlbum = new AlbumEntry(0, context.getString(R.string.all_photos), mediaEntry);
photoAlbumsSorted.add(0, allPhotosAlbum);
}
if (allMediaAlbum == null) {
allMediaAlbum = new AlbumEntry(0, context.getString(R.string.all_media), mediaEntry);
mediaAlbumsSorted.add(0, allMediaAlbum);
}
allPhotosAlbum.addPhoto(mediaEntry);
allMediaAlbum.addPhoto(mediaEntry);
AlbumEntry albumEntry = mediaAlbums.get(bucketId);
if (albumEntry == null) {
albumEntry = new AlbumEntry(bucketId, bucketName, mediaEntry);
mediaAlbums.put(bucketId, albumEntry);
if (mediaCameraAlbumId == null && cameraFolder != null && path.startsWith(cameraFolder)) {
mediaAlbumsSorted.add(0, albumEntry);
mediaCameraAlbumId = bucketId;
} else {
mediaAlbumsSorted.add(albumEntry);
}
}
albumEntry.addPhoto(mediaEntry);
albumEntry = photoAlbums.get(bucketId);
if (albumEntry == null) {
albumEntry = new AlbumEntry(bucketId, bucketName, mediaEntry);
photoAlbums.put(bucketId, albumEntry);
if (photoCameraAlbumId == null && cameraFolder != null && path.startsWith(cameraFolder)) {
photoAlbumsSorted.add(0, albumEntry);
photoCameraAlbumId = bucketId;
} else {
photoAlbumsSorted.add(albumEntry);
}
}
albumEntry.addPhoto(mediaEntry);
}
}
// }
} catch (Throwable e) {
Log.e(TAG, "loadGalleryAlbums: ", e);
} finally {
@ -177,79 +177,79 @@ public class MediaController {
}
try {
if (PermissionUtils.hasAttachMediaPerms(context)) {
cursor = MediaStore.Images.Media.query(context.getContentResolver(),
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
PROJECTION_VIDEO,
MediaStore.Video.Media.MIME_TYPE + "=?",
new String[]{"video/mp4"},
(Build.VERSION.SDK_INT > 28
? MediaStore.Video.Media.DATE_TAKEN
: MediaStore.Video.Media.DATE_MODIFIED) + " DESC");
if (cursor != null) {
int imageIdColumn = cursor.getColumnIndex(MediaStore.Video.Media._ID);
int bucketIdColumn = cursor.getColumnIndex(MediaStore.Video.Media.BUCKET_ID);
int bucketNameColumn = cursor.getColumnIndex(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
int dataColumn = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
int dateColumn = cursor.getColumnIndex(Build.VERSION.SDK_INT > 28 ? MediaStore.Video.Media.DATE_TAKEN
: MediaStore.Video.Media.DATE_MODIFIED);
int durationColumn = cursor.getColumnIndex(MediaStore.Video.Media.DURATION);
int widthColumn = cursor.getColumnIndex(MediaStore.Video.Media.WIDTH);
int heightColumn = cursor.getColumnIndex(MediaStore.Video.Media.HEIGHT);
int sizeColumn = cursor.getColumnIndex(MediaStore.Video.Media.SIZE);
// if (PermissionUtils.hasAttachMediaPerms(context)) {
cursor = MediaStore.Images.Media.query(context.getContentResolver(),
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
PROJECTION_VIDEO,
MediaStore.Video.Media.MIME_TYPE + "=?",
new String[]{"video/mp4"},
(Build.VERSION.SDK_INT > 28
? MediaStore.Video.Media.DATE_TAKEN
: MediaStore.Video.Media.DATE_MODIFIED) + " DESC");
if (cursor != null) {
int imageIdColumn = cursor.getColumnIndex(MediaStore.Video.Media._ID);
int bucketIdColumn = cursor.getColumnIndex(MediaStore.Video.Media.BUCKET_ID);
int bucketNameColumn = cursor.getColumnIndex(MediaStore.Video.Media.BUCKET_DISPLAY_NAME);
int dataColumn = cursor.getColumnIndex(MediaStore.Video.Media.DATA);
int dateColumn = cursor.getColumnIndex(Build.VERSION.SDK_INT > 28 ? MediaStore.Video.Media.DATE_TAKEN
: MediaStore.Video.Media.DATE_MODIFIED);
int durationColumn = cursor.getColumnIndex(MediaStore.Video.Media.DURATION);
int widthColumn = cursor.getColumnIndex(MediaStore.Video.Media.WIDTH);
int heightColumn = cursor.getColumnIndex(MediaStore.Video.Media.HEIGHT);
int sizeColumn = cursor.getColumnIndex(MediaStore.Video.Media.SIZE);
while (cursor.moveToNext()) {
String path = cursor.getString(dataColumn);
if (TextUtils.isEmpty(path)) {
continue;
}
int imageId = cursor.getInt(imageIdColumn);
int bucketId = cursor.getInt(bucketIdColumn);
String bucketName = cursor.getString(bucketNameColumn);
long dateTaken = cursor.getLong(dateColumn);
long duration = cursor.getLong(durationColumn);
int width = cursor.getInt(widthColumn);
int height = cursor.getInt(heightColumn);
long size = cursor.getLong(sizeColumn);
MediaEntry mediaEntry = new MediaEntry(bucketId, imageId, dateTaken, path, -1, duration, true, width, height, size);
if (allVideosAlbum == null) {
allVideosAlbum = new AlbumEntry(0, context.getString(R.string.all_videos), mediaEntry);
allVideosAlbum.videoOnly = true;
int index = 0;
if (allMediaAlbum != null) {
index++;
}
if (allPhotosAlbum != null) {
index++;
}
mediaAlbumsSorted.add(index, allVideosAlbum);
}
if (allMediaAlbum == null) {
allMediaAlbum = new AlbumEntry(0, context.getString(R.string.all_media), mediaEntry);
mediaAlbumsSorted.add(0, allMediaAlbum);
}
allVideosAlbum.addPhoto(mediaEntry);
allMediaAlbum.addPhoto(mediaEntry);
AlbumEntry albumEntry = mediaAlbums.get(bucketId);
if (albumEntry == null) {
albumEntry = new AlbumEntry(bucketId, bucketName, mediaEntry);
mediaAlbums.put(bucketId, albumEntry);
if (mediaCameraAlbumId == null && cameraFolder != null && path.startsWith(cameraFolder)) {
mediaAlbumsSorted.add(0, albumEntry);
mediaCameraAlbumId = bucketId;
} else {
mediaAlbumsSorted.add(albumEntry);
}
}
albumEntry.addPhoto(mediaEntry);
while (cursor.moveToNext()) {
String path = cursor.getString(dataColumn);
if (TextUtils.isEmpty(path)) {
continue;
}
int imageId = cursor.getInt(imageIdColumn);
int bucketId = cursor.getInt(bucketIdColumn);
String bucketName = cursor.getString(bucketNameColumn);
long dateTaken = cursor.getLong(dateColumn);
long duration = cursor.getLong(durationColumn);
int width = cursor.getInt(widthColumn);
int height = cursor.getInt(heightColumn);
long size = cursor.getLong(sizeColumn);
MediaEntry mediaEntry = new MediaEntry(bucketId, imageId, dateTaken, path, -1, duration, true, width, height, size);
if (allVideosAlbum == null) {
allVideosAlbum = new AlbumEntry(0, context.getString(R.string.all_videos), mediaEntry);
allVideosAlbum.videoOnly = true;
int index = 0;
if (allMediaAlbum != null) {
index++;
}
if (allPhotosAlbum != null) {
index++;
}
mediaAlbumsSorted.add(index, allVideosAlbum);
}
if (allMediaAlbum == null) {
allMediaAlbum = new AlbumEntry(0, context.getString(R.string.all_media), mediaEntry);
mediaAlbumsSorted.add(0, allMediaAlbum);
}
allVideosAlbum.addPhoto(mediaEntry);
allMediaAlbum.addPhoto(mediaEntry);
AlbumEntry albumEntry = mediaAlbums.get(bucketId);
if (albumEntry == null) {
albumEntry = new AlbumEntry(bucketId, bucketName, mediaEntry);
mediaAlbums.put(bucketId, albumEntry);
if (mediaCameraAlbumId == null && cameraFolder != null && path.startsWith(cameraFolder)) {
mediaAlbumsSorted.add(0, albumEntry);
mediaCameraAlbumId = bucketId;
} else {
mediaAlbumsSorted.add(albumEntry);
}
}
albumEntry.addPhoto(mediaEntry);
}
}
// }
} catch (Throwable e) {
Log.e(TAG, "loadGalleryAlbums: ", e);
} finally {

View File

@ -2,13 +2,17 @@ package awais.instagrabber.utils;
import android.content.ContentResolver;
import android.database.Cursor;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.io.FileDescriptor;
public final class MediaUtils {
private static final String TAG = MediaUtils.class.getSimpleName();
private static final String[] PROJECTION_VIDEO = {
@ -64,34 +68,24 @@ public final class MediaUtils {
@NonNull final Uri uri,
@NonNull final OnInfoLoadListener<VideoInfo> listener) {
AppExecutors.getInstance().tasksThread().submit(() -> {
try (Cursor cursor = MediaStore.Video.query(contentResolver, uri, PROJECTION_AUDIO)) {
if (cursor == null) {
if (listener != null) {
listener.onLoad(null);
}
return;
}
int durationColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DURATION);
int sizeColumn = cursor.getColumnIndex(MediaStore.Audio.Media.SIZE);
if (cursor.moveToNext()) {
if (listener != null) {
listener.onLoad(new VideoInfo(
cursor.getLong(durationColumn),
0,
0,
cursor.getLong(sizeColumn)
));
}
try (ParcelFileDescriptor parcelFileDescriptor = contentResolver.openFileDescriptor(uri, "r")) {
final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
final MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(fileDescriptor);
final String duration = mediaMetadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
if (listener != null) {
listener.onLoad(new VideoInfo(
Long.parseLong(duration),
0,
0,
0
));
}
} catch (Exception e) {
Log.e(TAG, "getVoiceInfo: ", e);
if (listener != null) {
listener.onFailure(e);
}
return;
}
if (listener != null) {
listener.onLoad(null);
}
});
}

View File

@ -11,32 +11,31 @@ import androidx.core.content.PermissionChecker;
import androidx.fragment.app.Fragment;
import static android.Manifest.permission.CAMERA;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.Manifest.permission.RECORD_AUDIO;
import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
import static androidx.core.content.PermissionChecker.checkSelfPermission;
public class PermissionUtils {
public static final String[] AUDIO_RECORD_PERMS = new String[]{WRITE_EXTERNAL_STORAGE, RECORD_AUDIO};
public static final String[] ATTACH_MEDIA_PERMS = new String[]{READ_EXTERNAL_STORAGE};
public static final String[] AUDIO_RECORD_PERMS = new String[]{RECORD_AUDIO};
// public static final String[] ATTACH_MEDIA_PERMS = new String[]{READ_EXTERNAL_STORAGE};
public static final String[] CAMERA_PERMS = new String[]{CAMERA};
public static boolean hasAudioRecordPerms(@NonNull final Context context) {
return checkSelfPermission(context, WRITE_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED
&& checkSelfPermission(context, RECORD_AUDIO) == PermissionChecker.PERMISSION_GRANTED;
return // checkSelfPermission(context, WRITE_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED
// &&
checkSelfPermission(context, RECORD_AUDIO) == PermissionChecker.PERMISSION_GRANTED;
}
public static void requestAudioRecordPerms(final Fragment fragment, final int requestCode) {
fragment.requestPermissions(AUDIO_RECORD_PERMS, requestCode);
}
public static boolean hasAttachMediaPerms(@NonNull final Context context) {
return checkSelfPermission(context, READ_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED;
}
// public static boolean hasAttachMediaPerms(@NonNull final Context context) {
// return checkSelfPermission(context, READ_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED;
// }
public static void requestAttachMediaPerms(final Fragment fragment, final int requestCode) {
fragment.requestPermissions(ATTACH_MEDIA_PERMS, requestCode);
}
// public static void requestAttachMediaPerms(final Fragment fragment, final int requestCode) {
// fragment.requestPermissions(ATTACH_MEDIA_PERMS, requestCode);
// }
public static boolean hasCameraPerms(final Context context) {
return ContextCompat.checkSelfPermission(context, CAMERA) == PackageManager.PERMISSION_GRANTED;

View File

@ -509,7 +509,11 @@ public final class Utils {
public static void scanDocumentFile(@NonNull final Context context,
@NonNull final DocumentFile documentFile,
@NonNull final OnScanCompletedListener callback) {
if (!documentFile.isFile()) return;
if (!documentFile.isFile() || !documentFile.exists()) {
Log.d(TAG, "scanDocumentFile: " + documentFile);
callback.onScanCompleted(null, null);
return;
}
File file = null;
try {
file = getDocumentFileRealPath(context, documentFile);
@ -532,6 +536,8 @@ public final class Utils {
if (type.equalsIgnoreCase("primary")) {
return new File(Environment.getExternalStorageDirectory(), split[1]);
} else if (type.equalsIgnoreCase("raw")) {
return new File(split[1]);
} else {
if (volumes == null) {
final StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

View File

@ -11,6 +11,7 @@ import android.webkit.MimeTypeMap;
import androidx.annotation.NonNull;
import androidx.documentfile.provider.DocumentFile;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
@ -44,13 +45,14 @@ public class VoiceRecorder {
public void startRecording(final Application application) {
stopped = false;
ParcelFileDescriptor parcelFileDescriptor = null;
try {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
deleteTempAudioFile();
audioTempFile = getAudioRecordFile();
final ParcelFileDescriptor parcelFileDescriptor = application.getContentResolver().openFileDescriptor(audioTempFile.getUri(), "rwt");
parcelFileDescriptor = application.getContentResolver().openFileDescriptor(audioTempFile.getUri(), "rwt");
recorder.setOutputFile(parcelFileDescriptor.getFileDescriptor());
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.HE_AAC);
recorder.setAudioEncodingBitRate(AUDIO_BIT_RATE);
@ -66,6 +68,12 @@ public class VoiceRecorder {
} catch (Exception e) {
Log.e(TAG, "Audio recording failed", e);
deleteTempAudioFile();
} finally {
if (parcelFileDescriptor != null) {
try {
parcelFileDescriptor.close();
} catch (IOException ignored) {}
}
}
}
@ -145,7 +153,11 @@ public class VoiceRecorder {
@NonNull
private DocumentFile getAudioRecordFile() {
final String name = String.format("%s-%s.%s", FILE_PREFIX, SIMPLE_DATE_FORMAT.format(new Date()), EXTENSION);
return recordingsDir.createFile(MIME_TYPE, name);
DocumentFile file = recordingsDir.findFile(name);
if (file == null || !file.exists()) {
file = recordingsDir.createFile(MIME_TYPE, name);
}
return file;
}
private void deleteTempAudioFile() {

View File

@ -174,16 +174,16 @@ public class DirectThreadViewModel extends AndroidViewModel {
return;
}
Log.d(TAG, "onComplete: scan complete");
MediaUtils.getVoiceInfo(contentResolver, uri, new MediaUtils.OnInfoLoadListener<MediaUtils.VideoInfo>() {
MediaUtils.getVoiceInfo(contentResolver, result.getFile().getUri(), new MediaUtils.OnInfoLoadListener<MediaUtils.VideoInfo>() {
@Override
public void onLoad(@Nullable final MediaUtils.VideoInfo videoInfo) {
if (videoInfo == null) return;
threadManager.sendVoice(data,
uri,
result.getFile().getUri(),
result.getWaveform(),
result.getSamplingFreq(),
videoInfo == null ? 0 : videoInfo.duration,
videoInfo == null ? 0 : videoInfo.size);
result.getFile().length());
}
@Override