mirror of
https://github.com/SimpleMobileTools/Simple-Voice-Recorder.git
synced 2025-06-05 21:59:31 +02:00
add created files in MediaStore
This commit is contained in:
@@ -1,13 +1,16 @@
|
|||||||
package com.simplemobiletools.voicerecorder.activities
|
package com.simplemobiletools.voicerecorder.activities
|
||||||
|
|
||||||
|
import android.content.ContentValues
|
||||||
import android.content.Intent
|
import android.content.Intent
|
||||||
import android.graphics.drawable.Drawable
|
import android.graphics.drawable.Drawable
|
||||||
import android.media.MediaRecorder
|
import android.media.MediaRecorder
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
|
import android.provider.MediaStore
|
||||||
import android.view.Menu
|
import android.view.Menu
|
||||||
import android.view.MenuItem
|
import android.view.MenuItem
|
||||||
import com.simplemobiletools.commons.extensions.*
|
import com.simplemobiletools.commons.extensions.*
|
||||||
import com.simplemobiletools.commons.helpers.PERMISSION_RECORD_AUDIO
|
import com.simplemobiletools.commons.helpers.PERMISSION_RECORD_AUDIO
|
||||||
|
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
|
||||||
import com.simplemobiletools.commons.models.FAQItem
|
import com.simplemobiletools.commons.models.FAQItem
|
||||||
import com.simplemobiletools.voicerecorder.BuildConfig
|
import com.simplemobiletools.voicerecorder.BuildConfig
|
||||||
import com.simplemobiletools.voicerecorder.R
|
import com.simplemobiletools.voicerecorder.R
|
||||||
@@ -17,6 +20,7 @@ import java.io.IOException
|
|||||||
class MainActivity : SimpleActivity() {
|
class MainActivity : SimpleActivity() {
|
||||||
private var isRecording = false
|
private var isRecording = false
|
||||||
private var recorder: MediaRecorder? = null
|
private var recorder: MediaRecorder? = null
|
||||||
|
private var currFilePath = ""
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
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() {
|
private fun startRecording() {
|
||||||
val filename = "${getCurrentFormattedDateTime()}.3gp"
|
currFilePath = "${cacheDir}/${getCurrentFormattedDateTime()}.mp3"
|
||||||
recorder = MediaRecorder().apply {
|
recorder = MediaRecorder().apply {
|
||||||
setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
|
setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
|
||||||
setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
|
setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
|
||||||
setOutputFile(filename)
|
setAudioEncoder(MediaRecorder.AudioEncoder.AAC)
|
||||||
setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
|
setOutputFile(currFilePath)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
prepare()
|
prepare()
|
||||||
@@ -103,10 +108,36 @@ class MainActivity : SimpleActivity() {
|
|||||||
recorder?.apply {
|
recorder?.apply {
|
||||||
stop()
|
stop()
|
||||||
release()
|
release()
|
||||||
|
ensureBackgroundThread {
|
||||||
|
addFileInMediaStore()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
recorder = null
|
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 {
|
private fun getToggleButtonIcon(): Drawable {
|
||||||
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector
|
val drawable = if (isRecording) R.drawable.ic_stop_vector else R.drawable.ic_mic_vector
|
||||||
return resources.getColoredDrawableWithColor(drawable, getFABIconColor())
|
return resources.getColoredDrawableWithColor(drawable, getFABIconColor())
|
||||||
|
Reference in New Issue
Block a user