Skip to content

Commit

Permalink
when a voice message is played, starts the download of the next ones …
Browse files Browse the repository at this point in the history
…if any, computes their durations and starts playing the next one at the end of the current. Plays a doodle between them

Signed-off-by: Giacomo Pacini <[email protected]>
  • Loading branch information
Ruggero1912 committed Dec 16, 2024
1 parent 3330b3d commit d7a82f8
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ class IncomingVoiceMessageViewHolder(incomingView: View, payload: Any) :
}

private fun handleIsPlayingVoiceMessageState(message: ChatMessage) {
colorizeMessageBubble(message)
if (message.isPlayingVoiceMessage) {
showPlayButton()
binding.playPauseBtn.icon = ContextCompat.getDrawable(
Expand All @@ -199,6 +200,7 @@ class IncomingVoiceMessageViewHolder(incomingView: View, payload: Any) :
binding.seekbar.max = message.voiceMessageDuration * ONE_SEC
binding.seekbar.progress = message.voiceMessageSeekbarProgress
} else {
showVoiceMessageDuration(message)
binding.playPauseBtn.visibility = View.VISIBLE
binding.playPauseBtn.icon = ContextCompat.getDrawable(
context!!,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ class OutcomingVoiceMessageViewHolder(outcomingView: View) :
}

private fun handleIsPlayingVoiceMessageState(message: ChatMessage) {
colorizeMessageBubble(message)
if (message.isPlayingVoiceMessage) {
showPlayButton()
binding.playPauseBtn.icon = ContextCompat.getDrawable(
Expand All @@ -233,6 +234,7 @@ class OutcomingVoiceMessageViewHolder(outcomingView: View) :
binding.seekbar.max = message.voiceMessageDuration * ONE_SEC
binding.seekbar.progress = message.voiceMessageSeekbarProgress
} else {
showVoiceMessageDuration(message)
binding.playPauseBtn.visibility = View.VISIBLE
binding.playPauseBtn.icon = ContextCompat.getDrawable(
context!!,
Expand Down
74 changes: 71 additions & 3 deletions app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import android.database.Cursor
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable
import android.media.AudioFocusRequest
import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioRecord
import android.media.MediaMetadataRetriever
import android.media.MediaPlayer
import android.net.Uri
import android.os.Build
Expand Down Expand Up @@ -1614,16 +1619,70 @@ class ChatActivity :
}

private fun startPlayback(message: ChatMessage, doPlay: Boolean = true) {
if (!active) {
//if (!active) {
// don't begin to play voice message if screen is not visible anymore.
// this situation might happen if file is downloading but user already left the chatview.
// If user returns to chatview, the old chatview instance is not attached anymore
// and he has to click the play button again (which is considered to be okay)
return
}
// return
//}

initMediaPlayer(message)

val id = message.id.toString()
val index = adapter?.getMessagePositionById(id) ?: 0

var nextMessage : ChatMessage? = null
for (i in 1..5) {
if(index - i < 0){
break
}
val curMsg = adapter?.items?.get(index - i)?.item
if(curMsg is ChatMessage) {
if(nextMessage == null) {
nextMessage = curMsg as ChatMessage
}

if(curMsg.isVoiceMessage){
val filename = curMsg.selectedIndividualHashMap!!["name"]
val file = File(context.cacheDir, filename!!)
if (!file.exists()) {
downloadFileToCache(curMsg as ChatMessage, false) {
curMsg.isDownloadingVoiceMessage = false
curMsg.voiceMessageDuration = try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(file.absolutePath) // Set the audio file as the data source
val durationStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
retriever.release() // Always release the retriever to free resources
(durationStr?.toIntOrNull() ?: 0 )/ 1000 // Convert to int (seconds)
} catch (e: Exception) {
e.printStackTrace()
0
}
adapter?.update(curMsg)
}
}else{
if(curMsg.voiceMessageDuration == 0) {
curMsg.voiceMessageDuration = try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(file.absolutePath) // Set the audio file as the data source
val durationStr = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
retriever.release() // Always release the retriever to free resources
(durationStr?.toIntOrNull() ?: 0 )/ 1000 // Convert to int (seconds)
} catch (e: Exception) {
e.printStackTrace()
0
}
adapter?.update(curMsg)
}
}
}
}
}

val hasConsecutiveVoiceMessage = if(nextMessage != null) nextMessage.isVoiceMessage else false


mediaPlayer?.let {
if (!it.isPlaying && doPlay) {
chatViewModel.audioRequest(true) {
Expand Down Expand Up @@ -1656,6 +1715,15 @@ class ChatActivity :
message.voiceMessageSeekbarProgress = 0
adapter?.update(message)
stopMediaPlayer(message)
if(hasConsecutiveVoiceMessage){
val defaultMediaPlayer = MediaPlayer.create(context, R.raw
.next_voice_message_doodle)
defaultMediaPlayer.setOnCompletionListener {
defaultMediaPlayer.release()
setUpWaveform(nextMessage as ChatMessage, doPlay)
}
defaultMediaPlayer.start()
}
}
}
}
Expand Down
Binary file not shown.

0 comments on commit d7a82f8

Please sign in to comment.