Skip to content

Commit

Permalink
* Extract Voice recording duration whenever needed to show in UI: #58
Browse files Browse the repository at this point in the history
  • Loading branch information
tuancoltech committed May 20, 2024
1 parent 6221418 commit 38c157d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import kotlin.io.path.createTempFile
class VoiceNote(
override val title: String = "",
override val description: String = "",
val duration: String = "",
var duration: String = "",
@IgnoredOnParcel
var path: Path = createTempFile(),
@IgnoredOnParcel
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.arkbuilders.arkmemo.repo.voices

import android.media.MediaMetadataRetriever
import android.util.Log
import dev.arkbuilders.arklib.computeId
import dev.arkbuilders.arklib.data.index.Resource
Expand All @@ -10,6 +11,7 @@ import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.repo.NotesRepo
import dev.arkbuilders.arkmemo.repo.NotesRepoHelper
import dev.arkbuilders.arkmemo.utils.listFiles
import dev.arkbuilders.arkmemo.utils.millisToString
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.withContext
import java.nio.file.Path
Expand All @@ -20,6 +22,7 @@ import kotlin.io.path.fileSize
import kotlin.io.path.extension
import kotlin.io.path.getLastModifiedTime
import kotlin.io.path.name
import kotlin.io.path.pathString

class VoiceNotesRepo @Inject constructor(
private val memoPreferences: MemoPreferences,
Expand Down Expand Up @@ -94,10 +97,25 @@ class VoiceNotesRepo @Inject constructor(
title = userNoteProperties.title,
description = userNoteProperties.description,
path = path,
duration = extractDuration(path.pathString),
resource = resource
)
}
}

fun extractDuration(path: String): String {
return try {
val metadataRetriever = MediaMetadataRetriever()
metadataRetriever.setDataSource(path)
val duration = metadataRetriever.extractMetadata(
MediaMetadataRetriever.METADATA_KEY_DURATION
)?.toLong() ?: 0L
millisToString(duration)
} catch (e: IllegalArgumentException) {
Log.e(VOICES_REPO, "extractDuration exception: " + e.message)
""
}
}
}

private const val VOICES_REPO = "voices-repo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class NotesListAdapter(
holder.layoutAudioView.root.isVisible = false
if (note is VoiceNote) {
holder.layoutAudioView.root.isVisible = true
holder.layoutAudioView.tvDuration.text = note.duration
holder.btnPlayPause.setOnClickListener {
onPlayPauseClick(note.path.toString())
handleMediaPlayerSideEffect(observeItemSideEffect(), holder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import dev.arkbuilders.arkmemo.models.GraphicNote
import dev.arkbuilders.arkmemo.models.Note
import dev.arkbuilders.arkmemo.models.TextNote
import dev.arkbuilders.arkmemo.models.VoiceNote
import dev.arkbuilders.arkmemo.repo.voices.VoiceNotesRepo
import kotlinx.coroutines.flow.collectLatest
import javax.inject.Inject
import javax.inject.Named
import kotlin.io.path.pathString

@HiltViewModel
class NotesViewModel @Inject constructor(
Expand Down Expand Up @@ -112,6 +114,9 @@ class NotesViewModel @Inject constructor(
note.resource?.let {
notes.removeIf { it.resource?.id == note.resource?.id }
}
if (note is VoiceNote) {
note.duration = (voiceNotesRepo as VoiceNotesRepo).extractDuration(note.path.pathString)
}
notes.add(note)
this.notes.value = notes
}
Expand Down

0 comments on commit 38c157d

Please sign in to comment.