This commit is contained in:
Benoit Marty 2021-07-13 15:57:35 +02:00
parent 6283846108
commit a52d5f6d12
3 changed files with 36 additions and 37 deletions

View File

@ -17,7 +17,6 @@
package im.vector.app.core.utils
import io.reactivex.Observable
import timber.log.Timber
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicLong

View File

@ -51,8 +51,8 @@ class VoiceMessageHelper @Inject constructor(
private val amplitudeList = mutableListOf<Int>()
private var amplitudeTimer: CountUpTimer? = null
private var playbackTimer: CountUpTimer? = null
private var amplitudeTicker: CountUpTimer? = null
private var playbackTicker: CountUpTimer? = null
init {
if (!outputDirectory.exists()) {
@ -162,27 +162,27 @@ class VoiceMessageHelper @Inject constructor(
seekTo(currentPlaybackTime)
}
}
startPlaybackTimer(id)
startPlaybackTicker(id)
}
private fun stopPlayback() {
mediaPlayer?.stop()
stopPlaybackTimer()
stopPlaybackTicker()
}
private fun startRecordingAmplitudes() {
amplitudeTimer?.stop()
amplitudeTimer = CountUpTimer(100).apply {
amplitudeTicker?.stop()
amplitudeTicker = CountUpTimer(100).apply {
tickListener = object : CountUpTimer.TickListener {
override fun onTick(milliseconds: Long) {
onAmplitudeTimerTick()
onAmplitudeTick()
}
}
resume()
}
}
private fun onAmplitudeTimerTick() {
private fun onAmplitudeTick() {
try {
val maxAmplitude = mediaRecorder.maxAmplitude
amplitudeList.add(maxAmplitude)
@ -197,36 +197,36 @@ class VoiceMessageHelper @Inject constructor(
}
private fun stopRecordingAmplitudes() {
amplitudeTimer?.stop()
amplitudeTimer = null
amplitudeTicker?.stop()
amplitudeTicker = null
}
private fun startPlaybackTimer(id: String) {
playbackTimer?.stop()
playbackTimer = CountUpTimer().apply {
private fun startPlaybackTicker(id: String) {
playbackTicker?.stop()
playbackTicker = CountUpTimer().apply {
tickListener = object : CountUpTimer.TickListener {
override fun onTick(milliseconds: Long) {
onPlaybackTimerTick(id)
onPlaybackTick(id)
}
}
resume()
}
onPlaybackTimerTick(id)
onPlaybackTick(id)
}
private fun onPlaybackTimerTick(id: String) {
private fun onPlaybackTick(id: String) {
if (mediaPlayer?.isPlaying.orFalse()) {
val currentPosition = mediaPlayer?.currentPosition ?: 0
playbackTracker.updateCurrentPlaybackTime(id, currentPosition)
} else {
playbackTracker.stopPlayback(id)
stopPlaybackTimer()
stopPlaybackTicker()
}
}
private fun stopPlaybackTimer() {
playbackTimer?.stop()
playbackTimer = null
private fun stopPlaybackTicker() {
playbackTicker?.stop()
playbackTicker = null
}
fun stopAllVoiceActions() {

View File

@ -67,7 +67,7 @@ class VoiceMessageRecorderView @JvmOverloads constructor(
private var lastX: Float = 0f
private var lastY: Float = 0f
private var recordingTimer: CountUpTimer? = null
private var recordingTicker: CountUpTimer? = null
private val dimensionConverter = DimensionConverter(context.resources)
@ -81,7 +81,7 @@ class VoiceMessageRecorderView @JvmOverloads constructor(
fun initVoiceRecordingViews() {
hideRecordingViews(animationDuration = 0)
stopRecordingTimer()
stopRecordingTicker()
views.voiceMessageMicButton.isVisible = true
views.voiceMessageSendButton.isVisible = false
@ -89,7 +89,7 @@ class VoiceMessageRecorderView @JvmOverloads constructor(
private fun initListeners() {
views.voiceMessageSendButton.setOnClickListener {
stopRecordingTimer()
stopRecordingTicker()
hideRecordingViews(animationDuration = 0)
views.voiceMessageSendButton.isVisible = false
recordingState = RecordingState.NONE
@ -97,7 +97,7 @@ class VoiceMessageRecorderView @JvmOverloads constructor(
}
views.voiceMessageDeletePlayback.setOnClickListener {
stopRecordingTimer()
stopRecordingTicker()
hideRecordingViews(animationDuration = 0)
views.voiceMessageSendButton.isVisible = false
recordingState = RecordingState.NONE
@ -120,7 +120,7 @@ class VoiceMessageRecorderView @JvmOverloads constructor(
MotionEvent.ACTION_DOWN -> {
val recordingStarted = callback?.onVoiceRecordingStarted().orFalse()
if (recordingStarted) {
startRecordingTimer()
startRecordingTicker()
renderToast(context.getString(R.string.voice_message_release_to_send_toast))
recordingState = RecordingState.STARTED
showRecordingViews()
@ -134,7 +134,7 @@ class VoiceMessageRecorderView @JvmOverloads constructor(
}
MotionEvent.ACTION_UP -> {
if (recordingState != RecordingState.LOCKED) {
stopRecordingTimer()
stopRecordingTicker()
val isCancelled = recordingState == RecordingState.NONE || recordingState == RecordingState.CANCELLED
callback?.onVoiceRecordingEnded(isCancelled)
recordingState = RecordingState.NONE
@ -230,27 +230,27 @@ class VoiceMessageRecorderView @JvmOverloads constructor(
return abs(views.voiceMessageLockImage.y + views.voiceMessageLockImage.height - views.voiceMessageLockArrow.y) < 10
}
private fun startRecordingTimer() {
recordingTimer?.stop()
recordingTimer = CountUpTimer().apply {
private fun startRecordingTicker() {
recordingTicker?.stop()
recordingTicker = CountUpTimer().apply {
tickListener = object : CountUpTimer.TickListener {
override fun onTick(milliseconds: Long) {
onRecordingTimerTick(milliseconds)
onRecordingTick(milliseconds)
}
}
resume()
}
onRecordingTimerTick(0L)
onRecordingTick(0L)
}
private fun onRecordingTimerTick(milliseconds: Long) {
private fun onRecordingTick(milliseconds: Long) {
renderRecordingTimer(milliseconds / 1_000)
val timeDiffToRecordingLimit = BuildConfig.VOICE_MESSAGE_DURATION_LIMIT_MS - milliseconds
if (timeDiffToRecordingLimit <= 0) {
views.voiceMessageRecordingLayout.post {
recordingState = RecordingState.PLAYBACK
showPlaybackViews()
stopRecordingTimer()
stopRecordingTicker()
}
} else if (timeDiffToRecordingLimit in 10_000..10_999) {
views.voiceMessageRecordingLayout.post {
@ -298,9 +298,9 @@ class VoiceMessageRecorderView @JvmOverloads constructor(
}
}
private fun stopRecordingTimer() {
recordingTimer?.stop()
recordingTimer = null
private fun stopRecordingTicker() {
recordingTicker?.stop()
recordingTicker = null
}
private fun showRecordingViews() {