the app doesn't crash anymore when timeout or server down

This commit is contained in:
Mathieu B 2023-03-25 12:34:57 +01:00
parent a2f8b76ac7
commit eb5c8b905f

View File

@ -31,6 +31,7 @@ import com.github.squti.androidwaverecorder.RecorderState
import com.github.squti.androidwaverecorder.WaveRecorder
import org.json.JSONObject
import java.io.File
import java.io.IOException
import kotlin.concurrent.thread
@ -148,6 +149,7 @@ fun RecordingFooterButton(
var isListening: Boolean by remember { mutableStateOf(false) }
var isProcessing: Boolean by remember { mutableStateOf(false) }
var isError: Boolean by remember { mutableStateOf(false) }
// Add a listener for the waveRecorder to record when isListening is true and then process the audio when done listening
audioRecorder.waveRecorder.onStateChangeListener = {
@ -157,12 +159,18 @@ fun RecordingFooterButton(
thread {
isListening = false
isProcessing = true
isError = false
try {
processMessage(
processAudio(audioRecorder.getOutputFile()),
navController,
uiState
)
} catch (e: IOException) {
isError = true
e.printStackTrace()
}
isProcessing = false
audioRecorder.getOutputFile().delete()
@ -192,7 +200,7 @@ fun RecordingFooterButton(
DotsFlashing(7.dp, 3, 300, MaterialTheme.colorScheme.secondary, 2.dp)
} else {
Icon(
painter = painterResource(id = R.drawable.ic_baseline_mic_24),
painter = painterResource(id = (if (isError) R.drawable.ic_baseline_error_24 else R.drawable.ic_baseline_mic_24)),
contentDescription = "microphone"
)
}