Stop playback when starting to record

pull/277/head
Kevin 3 years ago
parent 8a23e1f290
commit 64e3bbb0d1

@ -85,11 +85,20 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
} }
private suspend fun readAudioSamples(file: File): FloatArray = withContext(Dispatchers.IO) { private suspend fun readAudioSamples(file: File): FloatArray = withContext(Dispatchers.IO) {
stopPlayback()
startPlayback(file)
return@withContext decodeWaveFile(file)
}
private suspend fun stopPlayback() = withContext(Dispatchers.Main) {
mediaPlayer?.stop() mediaPlayer?.stop()
mediaPlayer?.release() mediaPlayer?.release()
mediaPlayer = null
}
private suspend fun startPlayback(file: File) = withContext(Dispatchers.Main) {
mediaPlayer = MediaPlayer.create(application, file.absolutePath.toUri()) mediaPlayer = MediaPlayer.create(application, file.absolutePath.toUri())
mediaPlayer?.start() mediaPlayer?.start()
return@withContext decodeWaveFile(file)
} }
private suspend fun transcribeAudio(file: File) { private suspend fun transcribeAudio(file: File) {
@ -120,6 +129,7 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
isRecording = false isRecording = false
recordedFile?.let { transcribeAudio(it) } recordedFile?.let { transcribeAudio(it) }
} else { } else {
stopPlayback()
val file = getTempFileForRecording() val file = getTempFileForRecording()
recorder.startRecording(file) { e -> recorder.startRecording(file) { e ->
viewModelScope.launch { viewModelScope.launch {
@ -147,11 +157,8 @@ class MainScreenViewModel(private val application: Application) : ViewModel() {
runBlocking { runBlocking {
whisperContext?.release() whisperContext?.release()
whisperContext = null whisperContext = null
stopPlayback()
} }
mediaPlayer?.stop()
mediaPlayer?.release()
mediaPlayer = null
} }
companion object { companion object {

Loading…
Cancel
Save