Skip to content

Commit

Permalink
Found the bug, partial fix
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Nov 20, 2023
1 parent 7860f2d commit 0da83d3
Showing 1 changed file with 44 additions and 34 deletions.
78 changes: 44 additions & 34 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,16 @@ class ChatActivity :

private val filesToUpload: MutableList<String> = 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
Expand Down Expand Up @@ -517,7 +522,7 @@ class ChatActivity :
if (isMicInputAudioThreadRunning) {
stopMicInputRecordingAnimation()
}
if (isVoiceRecordingInProgress) {
if (mediaRecorderState == MediaRecorderState.RECORDING) {
stopAudioRecording()
}
if (currentlyPlayedVoiceMessage != null) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -1109,8 +1115,6 @@ class ChatActivity :
sendVoiceRecordingLayoutParams.removeRule(BELOW)
sendVoiceRecordingLayoutParams.addRule(BELOW, R.id.audioRecordDuration)
}

isVoiceRecordingInProgress = !isVoiceRecordingInProgress
}

binding.messageInputView.deleteVoiceRecording.setOnClickListener {
Expand Down Expand Up @@ -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
}

Expand All @@ -1186,7 +1190,7 @@ class ChatActivity :

MotionEvent.ACTION_UP -> {
Log.d(TAG, "ACTION_UP. stop recording??")
if (!isVoiceRecordingInProgress ||
if (mediaRecorderState != MediaRecorderState.RECORDING ||
!isRecordAudioPermissionGranted() ||
isVoiceRecordingLocked
) {
Expand Down Expand Up @@ -1217,7 +1221,7 @@ class ChatActivity :
MotionEvent.ACTION_MOVE -> {
Log.d(TAG, "ACTION_MOVE.")

if (!isVoiceRecordingInProgress || !isRecordAudioPermissionGranted()) {
if (mediaRecorderState != MediaRecorderState.RECORDING || !isRecordAudioPermissionGranted()) {
return true
}

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -2133,46 +2123,63 @@ 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)
setAudioChannels(VOICE_MESSAGE_CHANNELS)

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")
Expand All @@ -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)
Expand Down

0 comments on commit 0da83d3

Please sign in to comment.