From 0da83d37daf9218a62f1d00b8f8181641c171fd9 Mon Sep 17 00:00:00 2001 From: Julius Linus Date: Mon, 20 Nov 2023 10:54:22 -0600 Subject: [PATCH] Found the bug, partial fix Signed-off-by: rapterjet2004 --- .../com/nextcloud/talk/chat/ChatActivity.kt | 78 +++++++++++-------- 1 file changed, 44 insertions(+), 34 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 996dfcc20b..74d5564a94 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -340,11 +340,16 @@ class ChatActivity : private val filesToUpload: MutableList = ArrayList() private lateinit var sharedText: String - var isVoiceRecordingInProgress: Boolean = false var currentVoiceRecordFile: String = "" var isVoiceRecordingLocked: Boolean = false private var isVoicePreviewPlaying: Boolean = false + private var recorder: MediaRecorder? = null + private enum class MediaRecorderState { + INITIAL, INITIALIZED, CONFIGURED, PREPARED, RECORDING, RELEASED, ERROR + } + private var mediaRecorderState: MediaRecorderState = MediaRecorderState.INITIAL + private var voicePreviewMediaPlayer: MediaPlayer? = null private var voicePreviewObjectAnimator: ObjectAnimator? = null var mediaPlayer: MediaPlayer? = null @@ -517,7 +522,7 @@ class ChatActivity : if (isMicInputAudioThreadRunning) { stopMicInputRecordingAnimation() } - if (isVoiceRecordingInProgress) { + if (mediaRecorderState == MediaRecorderState.RECORDING) { stopAudioRecording() } if (currentlyPlayedVoiceMessage != null) { @@ -1036,7 +1041,7 @@ class ChatActivity : } else { showMicrophoneButton(true) } - } else if (isVoiceRecordingInProgress) { + } else if (mediaRecorderState == MediaRecorderState.RECORDING) { binding.messageInputView.playPauseBtn.visibility = View.GONE binding.messageInputView.seekBar.visibility = View.GONE } else { @@ -1074,8 +1079,9 @@ class ChatActivity : binding.messageInputView.seekBar.setOnTouchListener(OnTouchListener { _, _ -> true }) binding.messageInputView.micInputCloud.setOnClickListener { - if (isVoiceRecordingInProgress) { + if (mediaRecorderState == MediaRecorderState.RECORDING) { recorder?.stop() + mediaRecorderState = MediaRecorderState.INITIAL stopMicInputRecordingAnimation() voiceRecordPauseTime = binding.messageInputView.audioRecordDuration.base - SystemClock.elapsedRealtime() binding.messageInputView.audioRecordDuration.stop() @@ -1095,7 +1101,7 @@ class ChatActivity : sendVoiceRecordingLayoutParams.removeRule(BELOW) sendVoiceRecordingLayoutParams.addRule(BELOW, R.id.voice_preview_container) } else { - restartAudio() + initMediaRecorder(currentVoiceRecordFile) startMicInputRecordingAnimation() binding.messageInputView.audioRecordDuration.base = SystemClock.elapsedRealtime() binding.messageInputView.audioRecordDuration.start() @@ -1109,8 +1115,6 @@ class ChatActivity : sendVoiceRecordingLayoutParams.removeRule(BELOW) sendVoiceRecordingLayoutParams.addRule(BELOW, R.id.audioRecordDuration) } - - isVoiceRecordingInProgress = !isVoiceRecordingInProgress } binding.messageInputView.deleteVoiceRecording.setOnClickListener { @@ -1175,7 +1179,7 @@ class ChatActivity : MotionEvent.ACTION_CANCEL -> { Log.d(TAG, "ACTION_CANCEL. same as for UP") - if (!isVoiceRecordingInProgress || !isRecordAudioPermissionGranted()) { + if (mediaRecorderState != MediaRecorderState.RECORDING || !isRecordAudioPermissionGranted()) { return true } @@ -1186,7 +1190,7 @@ class ChatActivity : MotionEvent.ACTION_UP -> { Log.d(TAG, "ACTION_UP. stop recording??") - if (!isVoiceRecordingInProgress || + if (mediaRecorderState != MediaRecorderState.RECORDING || !isRecordAudioPermissionGranted() || isVoiceRecordingLocked ) { @@ -1217,7 +1221,7 @@ class ChatActivity : MotionEvent.ACTION_MOVE -> { Log.d(TAG, "ACTION_MOVE.") - if (!isVoiceRecordingInProgress || !isRecordAudioPermissionGranted()) { + if (mediaRecorderState != MediaRecorderState.RECORDING || !isRecordAudioPermissionGranted()) { return true } @@ -1326,20 +1330,6 @@ class ChatActivity : } } - private fun restartAudio() { - recorder = MediaRecorder().apply { - setAudioSource(MediaRecorder.AudioSource.MIC) - setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) - setOutputFile(currentVoiceRecordFile) - setAudioEncoder(MediaRecorder.AudioEncoder.AAC) - setAudioSamplingRate(VOICE_MESSAGE_SAMPLING_RATE) - setAudioEncodingBitRate(VOICE_MESSAGE_ENCODING_BIT_RATE) - setAudioChannels(VOICE_MESSAGE_CHANNELS) - prepare() - start() - } - } - private fun endVoiceRecordingUI() { stopPreviewVoicePlaying() showRecordAudioUi(false) @@ -2133,10 +2123,19 @@ class ChatActivity : animation.repeatMode = Animation.REVERSE binding.messageInputView.microphoneEnabledInfo.startAnimation(animation) + initMediaRecorder(file) + VibrationUtils.vibrateShort(context) + } + + private fun initMediaRecorder(file: String) { recorder = MediaRecorder().apply { setAudioSource(MediaRecorder.AudioSource.MIC) - setOutputFile(file) + mediaRecorderState = MediaRecorderState.INITIALIZED + setOutputFormat(MediaRecorder.OutputFormat.MPEG_4) + mediaRecorderState = MediaRecorderState.CONFIGURED + + setOutputFile(file) setAudioEncoder(MediaRecorder.AudioEncoder.AAC) setAudioSamplingRate(VOICE_MESSAGE_SAMPLING_RATE) setAudioEncodingBitRate(VOICE_MESSAGE_ENCODING_BIT_RATE) @@ -2144,35 +2143,43 @@ class ChatActivity : try { prepare() + mediaRecorderState = MediaRecorderState.PREPARED } catch (e: IOException) { Log.e(TAG, "prepare for audio recording failed") } try { start() + mediaRecorderState = MediaRecorderState.RECORDING Log.d(TAG, "recording started") - isVoiceRecordingInProgress = true } catch (e: IllegalStateException) { Log.e(TAG, "start for audio recording failed") } - VibrationUtils.vibrateShort(context) } } private fun stopAndSendAudioRecording() { stopAudioRecording() Log.d(TAG, "stopped and sent audio recording") - val uri = Uri.fromFile(File(currentVoiceRecordFile)) - uploadFile(uri.toString(), true) + + if (mediaRecorderState != MediaRecorderState.ERROR) { + val uri = Uri.fromFile(File(currentVoiceRecordFile)) + uploadFile(uri.toString(), true) + } else { + mediaRecorderState = MediaRecorderState.INITIAL + } } private fun stopAndDiscardAudioRecording() { stopAudioRecording() Log.d(TAG, "stopped and discarded audio recording") - val cachedFile = File(currentVoiceRecordFile) cachedFile.delete() + + if (mediaRecorderState == MediaRecorderState.ERROR) { + mediaRecorderState = MediaRecorderState.INITIAL + } } @Suppress("Detekt.TooGenericExceptionCaught") @@ -2185,14 +2192,17 @@ class ChatActivity : Log.d(TAG, "recording stopped with $voiceRecordDuration") try { stop() - isVoiceRecordingInProgress = false - Log.d(TAG, "stopped recorder. isVoiceRecordingInProgress = false") + mediaRecorderState = MediaRecorderState.INITIAL + Log.d(TAG, "stopped recorder") } catch (e: java.lang.RuntimeException) { - Log.e(TAG, "error while stopping recorder!" + e) + mediaRecorderState = MediaRecorderState.ERROR + Log.e(TAG, "error while stopping recorder! with state $mediaRecorderState $e") } release() + mediaRecorderState = MediaRecorderState.RELEASED } catch (e: java.lang.IllegalStateException) { - Log.e(TAG, "error while stopping recorder!" + e) + mediaRecorderState = MediaRecorderState.ERROR + Log.e(TAG, "error while stopping recorder! with state $mediaRecorderState $e") } VibrationUtils.vibrateShort(context)