feat: Add concatenate functionality

This commit is contained in:
Myzel394 2023-08-02 14:34:02 +02:00
parent f79315e0c3
commit 7b19be595c
No known key found for this signature in database
GPG Key ID: 79CC92F37B3E1A2B
3 changed files with 41 additions and 17 deletions

View File

@ -71,5 +71,7 @@ dependencies {
annotationProcessor 'com.google.dagger:hilt-compiler:2.46.1' annotationProcessor 'com.google.dagger:hilt-compiler:2.46.1'
implementation "androidx.hilt:hilt-navigation-compose:1.0.0" implementation "androidx.hilt:hilt-navigation-compose:1.0.0"
implementation 'com.arthenica:ffmpeg-kit-full:5.1'
coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3' coreLibraryDesugaring 'com.android.tools:desugar_jdk_libs:2.0.3'
} }

View File

@ -79,24 +79,19 @@ fun AudioRecorder() {
Text(text = if (isRecording) "Stop" else "Start") Text(text = if (isRecording) "Stop" else "Start")
} }
if (!isRecording && service != null) if (!isRecording && service != null)
LazyColumn() { Button(
val items = service!!.getRecordingFilePaths().toList() onClick = {
val path = service!!.concatenateAudios()
items(items.size) { val player = MediaPlayer().apply {
val path = items[it] setDataSource(path)
prepare()
Button(
onClick = {
val player = MediaPlayer().apply {
setDataSource(path)
prepare()
start()
}
}
) {
Text(text = "Play $it")
} }
}
player.start()
},
) {
Text(text = "Convert")
} }
} }
} }

View File

@ -9,8 +9,11 @@ import android.os.Build
import android.os.Handler import android.os.Handler
import android.os.IBinder import android.os.IBinder
import android.os.Looper import android.os.Looper
import android.util.Log
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import com.arthenica.ffmpegkit.FFmpegKit
import com.arthenica.ffmpegkit.ReturnCode
import kotlinx.coroutines.NonCancellable.start import kotlinx.coroutines.NonCancellable.start
import java.io.File import java.io.File
import java.time.LocalDateTime import java.time.LocalDateTime
@ -19,7 +22,7 @@ import java.util.Date
import java.util.UUID; import java.util.UUID;
const val INTERVAL_DURATION = 30000L const val INTERVAL_DURATION = 10000L
class RecorderService: Service() { class RecorderService: Service() {
private val binder = LocalBinder() private val binder = LocalBinder()
@ -74,6 +77,30 @@ class RecorderService: Service() {
} }
} }
fun concatenateAudios(): String {
val paths = getRecordingFilePaths().joinToString("|")
val outputFile = "$fileFolder/concatenated.${getFileExtensions()}"
val command = "-i \"concat:$paths\" -acodec copy $outputFile"
val session = FFmpegKit.execute(command)
if (!ReturnCode.isSuccess(session.returnCode)) {
Log.d(
"Audio Concatenation",
String.format(
"Command failed with state %s and rc %s.%s",
session.getState(),
session.getReturnCode(),
session.getFailStackTrace()
)
);
throw Exception("Failed to concatenate audios")
}
return outputFile
}
private fun startNewRecording() { private fun startNewRecording() {
if (!isRecording) { if (!isRecording) {
return return