fixed media recorder bug

Signed-off-by: rapterjet2004 <juliuslinus1@gmail.com>
This commit is contained in:
rapterjet2004 2023-08-24 09:08:05 -05:00 committed by Marcel Hibbe
parent b5714abded
commit cc0815de0f
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B

View File

@ -348,6 +348,7 @@ class ChatActivity :
AudioFormat.CHANNEL_IN_MONO, AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT AudioFormat.ENCODING_PCM_16BIT
) )
private var voiceRecordDuration = 0L
// messy workaround for a mediaPlayer bug, don't delete // messy workaround for a mediaPlayer bug, don't delete
private var lastRecordMediaPosition: Int = 0 private var lastRecordMediaPosition: Int = 0
@ -1163,7 +1164,7 @@ class ChatActivity :
showRecordAudioUi(false) showRecordAudioUi(false)
voiceRecordEndTime = System.currentTimeMillis() voiceRecordEndTime = System.currentTimeMillis()
val voiceRecordDuration = voiceRecordEndTime - voiceRecordStartTime voiceRecordDuration = voiceRecordEndTime - voiceRecordStartTime
if (voiceRecordDuration < MINIMUM_VOICE_RECORD_DURATION) { if (voiceRecordDuration < MINIMUM_VOICE_RECORD_DURATION) {
Log.d(TAG, "voiceRecordDuration: $voiceRecordDuration") Log.d(TAG, "voiceRecordDuration: $voiceRecordDuration")
Snackbar.make( Snackbar.make(
@ -2075,6 +2076,7 @@ class ChatActivity :
try { try {
start() start()
Log.d(TAG, "recording started")
isVoiceRecordingInProgress = true isVoiceRecordingInProgress = true
} catch (e: IllegalStateException) { } catch (e: IllegalStateException) {
Log.e(TAG, "start for audio recording failed") Log.e(TAG, "start for audio recording failed")
@ -2087,15 +2089,17 @@ class ChatActivity :
private fun stopAndSendAudioRecording() { private fun stopAndSendAudioRecording() {
if (isVoiceRecordingInProgress) { if (isVoiceRecordingInProgress) {
stopAudioRecording() stopAudioRecording()
Log.d(TAG, "stopped and sent audio recording")
val uri = Uri.fromFile(File(currentVoiceRecordFile))
uploadFile(uri.toString(), true)
} }
val uri = Uri.fromFile(File(currentVoiceRecordFile))
uploadFile(uri.toString(), true)
} }
private fun stopAndDiscardAudioRecording() { private fun stopAndDiscardAudioRecording() {
if (isVoiceRecordingInProgress) { if (isVoiceRecordingInProgress) {
stopAudioRecording() stopAudioRecording()
Log.d(TAG, "stopped and discarded audio recording")
} }
val cachedFile = File(currentVoiceRecordFile) val cachedFile = File(currentVoiceRecordFile)
@ -2109,12 +2113,17 @@ class ChatActivity :
recorder?.apply { recorder?.apply {
try { try {
stop() Log.d(TAG, "recording stopped with $voiceRecordDuration")
if (voiceRecordDuration > MINIMUM_VOICE_RECORD_TO_STOP) {
stop()
}
release() release()
isVoiceRecordingInProgress = false isVoiceRecordingInProgress = false
Log.d(TAG, "stopped recorder. isVoiceRecordingInProgress = false") Log.d(TAG, "stopped recorder. isVoiceRecordingInProgress = false")
} catch (e: java.lang.IllegalStateException) { } catch (e: java.lang.IllegalStateException) {
error("error while stopping recorder!" + e) error("error while stopping recorder!" + e)
} catch (e: java.lang.RuntimeException) {
error("error while stopping recorder!" + e)
} }
VibrationUtils.vibrateShort(context) VibrationUtils.vibrateShort(context)
@ -4241,6 +4250,7 @@ class ChatActivity :
private const val REQUEST_CODE_SELECT_REMOTE_FILES = 888 private const val REQUEST_CODE_SELECT_REMOTE_FILES = 888
private const val OBJECT_MESSAGE: String = "{object}" private const val OBJECT_MESSAGE: String = "{object}"
private const val MINIMUM_VOICE_RECORD_DURATION: Int = 1000 private const val MINIMUM_VOICE_RECORD_DURATION: Int = 1000
private const val MINIMUM_VOICE_RECORD_TO_STOP: Int = 200
private const val VOICE_RECORD_CANCEL_SLIDER_X: Int = -50 private const val VOICE_RECORD_CANCEL_SLIDER_X: Int = -50
private const val VOICE_RECORD_LOCK_BUTTON_Y: Int = -130 private const val VOICE_RECORD_LOCK_BUTTON_Y: Int = -130
private const val VOICE_MESSAGE_META_DATA = "{\"messageType\":\"voice-message\"}" private const val VOICE_MESSAGE_META_DATA = "{\"messageType\":\"voice-message\"}"