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