diff --git a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt index 76a6344..50d54c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/voicerecorder/activities/MainActivity.kt @@ -1,13 +1,16 @@ package com.simplemobiletools.voicerecorder.activities +import android.content.ContentValues import android.content.Intent import android.graphics.drawable.Drawable import android.media.MediaRecorder import android.os.Bundle +import android.provider.MediaStore import android.view.Menu import android.view.MenuItem import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_RECORD_AUDIO +import com.simplemobiletools.commons.helpers.ensureBackgroundThread import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.voicerecorder.BuildConfig import com.simplemobiletools.voicerecorder.R @@ -17,6 +20,7 @@ import java.io.IOException class MainActivity : SimpleActivity() { private var isRecording = false private var recorder: MediaRecorder? = null + private var currFilePath = "" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -81,13 +85,14 @@ class MainActivity : SimpleActivity() { } } + // mp4 output format with aac encoding should produce good enough mp3 files according to https://stackoverflow.com/a/33054794/1967672 private fun startRecording() { - val filename = "${getCurrentFormattedDateTime()}.3gp" + currFilePath = "${cacheDir}/${getCurrentFormattedDateTime()}.mp3" recorder = MediaRecorder().apply { setAudioSource(MediaRecorder.AudioSource.CAMCORDER) - setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP) - setOutputFile(filename) - setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB) + setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) + setAudioEncoder(MediaRecorder.AudioEncoder.AAC) + setOutputFile(currFilePath) try { prepare() @@ -103,10 +108,36 @@ class MainActivity : SimpleActivity() { recorder?.apply { stop() release() + ensureBackgroundThread { + addFileInMediaStore() + } } recorder = null } + private fun addFileInMediaStore() { + val resolver = applicationContext.contentResolver + + val audioCollection = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY) + + val storeFilename = currFilePath.getFilenameFromPath() + val newSongDetails = ContentValues().apply { + put(MediaStore.Audio.Media.DISPLAY_NAME, storeFilename) + put(MediaStore.Audio.Media.TITLE, storeFilename) + put(MediaStore.Audio.Media.MIME_TYPE, storeFilename.getMimeType()) + } + + val newUri = resolver.insert(audioCollection, newSongDetails) + if (newUri == null) { + toast(R.string.unknown_error_occurred) + return + } + + val outputStream = contentResolver.openOutputStream(newUri) + val inputStream = getFileInputStreamSync(currFilePath) + inputStream!!.copyTo(outputStream!!, DEFAULT_BUFFER_SIZE) + } + private fun getToggleButtonIcon(): Drawable { val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector return resources.getColoredDrawableWithColor(drawable, getFABIconColor())