Prevent crashes on Android 13

This commit is contained in:
Ensar Sarajčić 2023-09-29 11:29:07 +02:00
parent cd62cabb52
commit 9a8c2f9210
1 changed files with 73 additions and 13 deletions

View File

@ -20,6 +20,7 @@ import com.simplemobiletools.voicerecorder.extensions.getAllRecordings
import com.simplemobiletools.voicerecorder.helpers.getAudioFileContentUri import com.simplemobiletools.voicerecorder.helpers.getAudioFileContentUri
import com.simplemobiletools.voicerecorder.models.Recording import com.simplemobiletools.voicerecorder.models.Recording
import linc.com.amplituda.Amplituda import linc.com.amplituda.Amplituda
import linc.com.amplituda.AmplitudaResult
import linc.com.amplituda.callback.AmplitudaSuccessListener import linc.com.amplituda.callback.AmplitudaSuccessListener
import linc.com.library.AudioTool import linc.com.library.AudioTool
import java.io.File import java.io.File
@ -87,12 +88,40 @@ class EditRecordingActivity : SimpleActivity() {
} }
private fun updateVisualization() { private fun updateVisualization() {
Amplituda(this).processAudio(currentRecording.path) Amplituda(this).apply {
.get(AmplitudaSuccessListener { try {
binding.recordingVisualizer.recreate() val uri = Uri.parse(currentRecording.path)
binding.recordingVisualizer.clearEditing()
binding.recordingVisualizer.putAmplitudes(it.amplitudesAsList()) fun handleAmplitudes(amplitudaResult: AmplitudaResult<*>) {
}) binding.recordingVisualizer.recreate()
binding.recordingVisualizer.clearEditing()
binding.recordingVisualizer.putAmplitudes(amplitudaResult.amplitudesAsList())
}
when {
DocumentsContract.isDocumentUri(this@EditRecordingActivity, uri) -> {
processAudio(contentResolver.openInputStream(uri)).get(AmplitudaSuccessListener {
handleAmplitudes(it)
})
}
currentRecording.path.isEmpty() -> {
processAudio(contentResolver.openInputStream(getAudioFileContentUri(currentRecording.id.toLong()))).get(AmplitudaSuccessListener {
handleAmplitudes(it)
})
}
else -> {
processAudio(currentRecording.path).get(AmplitudaSuccessListener {
handleAmplitudes(it)
})
}
}
} catch (e: Exception) {
showErrorToast(e)
return
}
}
} }
private fun setupColors() { private fun setupColors() {
@ -136,8 +165,7 @@ class EditRecordingActivity : SimpleActivity() {
val durationMillisPart = String.format("%.3f", durationMillis - durationMillis.toInt()).replace("0.", "") val durationMillisPart = String.format("%.3f", durationMillis - durationMillis.toInt()).replace("0.", "")
val startFormatted = (startMillis.toInt()).getFormattedDuration(true) + ".$startMillisPart" val startFormatted = (startMillis.toInt()).getFormattedDuration(true) + ".$startMillisPart"
val durationFormatted = (durationMillis.toInt()).getFormattedDuration(true) + ".$durationMillisPart" val durationFormatted = (durationMillis.toInt()).getFormattedDuration(true) + ".$durationMillisPart"
AudioTool.getInstance(this) modifyAudioFile(currentRecording)
.withAudio(File(currentRecording.path))
.cutAudio(startFormatted, durationFormatted) { .cutAudio(startFormatted, durationFormatted) {
progressStart = binding.recordingVisualizer.startPosition progressStart = binding.recordingVisualizer.startPosition
playRecording(it.path, null, it.name, durationMillis.toInt(), true) playRecording(it.path, null, it.name, durationMillis.toInt(), true)
@ -165,8 +193,9 @@ class EditRecordingActivity : SimpleActivity() {
fun merge() { fun merge() {
if (leftPart != null && rightPart != null) { if (leftPart != null && rightPart != null) {
ensureBackgroundThread { ensureBackgroundThread {
val tempFile = File.createTempFile("${currentRecording.title}.edit.", ".${currentRecording.title.getFilenameExtension()}", cacheDir)
AudioTool.getInstance(this) AudioTool.getInstance(this)
.joinAudios(arrayOf(leftPart, rightPart), "${currentRecording.path}.edit.${currentRecording.path.getFilenameExtension()}") { .joinAudios(arrayOf(leftPart, rightPart), tempFile.path) {
runOnUiThread { runOnUiThread {
currentRecording = Recording(-1, it.name, it.path, it.lastModified().toInt(), (startMillis + realEnd).toInt(), it.getProperSize(false).toInt()) currentRecording = Recording(-1, it.name, it.path, it.lastModified().toInt(), (startMillis + realEnd).toInt(), it.getProperSize(false).toInt())
updateVisualization() updateVisualization()
@ -177,14 +206,12 @@ class EditRecordingActivity : SimpleActivity() {
} }
} }
AudioTool.getInstance(this) modifyAudioFile(currentRecording)
.withAudio(File(currentRecording.path))
.cutAudio("00:00:00", startFormatted) { .cutAudio("00:00:00", startFormatted) {
leftPart = it leftPart = it
merge() merge()
} }
AudioTool.getInstance(this) modifyAudioFile(currentRecording)
.withAudio(File(currentRecording.path))
.cutAudio(endFormatted, realEndFormatted) { .cutAudio(endFormatted, realEndFormatted) {
rightPart = it rightPart = it
merge() merge()
@ -204,6 +231,7 @@ class EditRecordingActivity : SimpleActivity() {
progressStart = 0f progressStart = 0f
binding.recordingVisualizer.clearEditing() binding.recordingVisualizer.clearEditing()
currentRecording = recording currentRecording = recording
updateVisualization()
playRecording(currentRecording.path, currentRecording.id, currentRecording.title, currentRecording.duration, true) playRecording(currentRecording.path, currentRecording.id, currentRecording.title, currentRecording.duration, true)
} }
else -> return@setOnMenuItemClickListener false else -> return@setOnMenuItemClickListener false
@ -359,4 +387,36 @@ class EditRecordingActivity : SimpleActivity() {
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
} }
private fun modifyAudioFile(recording: Recording): AudioTool {
return AudioTool.getInstance(this)
.withAudio(copyToTempFile(recording))
}
private fun copyToTempFile(recording: Recording): File {
try {
val uri = Uri.parse(recording.path)
when {
DocumentsContract.isDocumentUri(this@EditRecordingActivity, uri) -> {
val tempFile = File.createTempFile(recording.title, ".${recording.title.getFilenameExtension()}", cacheDir)
contentResolver.openInputStream(uri)?.copyTo(tempFile.outputStream())
return tempFile
}
recording.path.isEmpty() -> {
val tempFile = File.createTempFile(recording.title, ".${recording.title.getFilenameExtension()}", cacheDir)
contentResolver.openInputStream(getAudioFileContentUri(recording.id.toLong()))?.copyTo(tempFile.outputStream())
return tempFile
}
else -> {
return File(recording.path)
}
}
} catch (e: Exception) {
showErrorToast(e)
return File(recording.path)
}
}
} }