Skip to content

Commit

Permalink
* Move extractDuration method to util class for better re-usability.
Browse files Browse the repository at this point in the history
* Minor Lint warning fixes
  • Loading branch information
tuancoltech committed Sep 3, 2024
1 parent 044f267 commit 5f7fab0
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ class ArkAudioRecorderImpl @Inject constructor(
}

override fun maxAmplitude(): Int {
try {
return recorder?.maxAmplitude ?: 0
return try {
recorder?.maxAmplitude ?: 0
} catch (e: Exception) {
return 0
0
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
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,8 +9,8 @@ import dev.arkbuilders.arkmemo.models.VoiceNote
import dev.arkbuilders.arkmemo.preferences.MemoPreferences
import dev.arkbuilders.arkmemo.repo.NotesRepo
import dev.arkbuilders.arkmemo.repo.NotesRepoHelper
import dev.arkbuilders.arkmemo.utils.extractDuration
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 Down Expand Up @@ -111,20 +110,6 @@ class VoiceNotesRepo @Inject constructor(
)
}
}

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: Exception) {
Log.e(VOICES_REPO, "extractDuration exception: " + e.message)
""
}
}
}

private const val VOICES_REPO = "VoiceNotesRepo"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package dev.arkbuilders.arkmemo.ui.fragments

import android.os.Build
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
Expand Down Expand Up @@ -32,6 +31,7 @@ import dev.arkbuilders.arkmemo.ui.adapters.EqualSpacingItemDecoration
import dev.arkbuilders.arkmemo.ui.viewmodels.GraphicNotesViewModel
import dev.arkbuilders.arkmemo.utils.getBrushSize
import dev.arkbuilders.arkmemo.utils.getColorCode
import dev.arkbuilders.arkmemo.utils.getParcelableCompat
import dev.arkbuilders.arkmemo.utils.gone
import dev.arkbuilders.arkmemo.utils.observeSaveResult
import dev.arkbuilders.arkmemo.utils.setDrawableColor
Expand Down Expand Up @@ -59,17 +59,9 @@ class EditGraphicNotesFragment: BaseEditNoteFragment() {
super.onCreate(savedInstanceState)
notesViewModel.init {}
observeSaveResult(notesViewModel.getSaveNoteResultLiveData())
if (arguments != null) {
@Suppress("DEPRECATION")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
requireArguments().getParcelable(GRAPHICAL_NOTE_KEY, GraphicNote::class.java)?.let {
note = it
graphicNotesViewModel.onNoteOpened(note)
}
else requireArguments().getParcelable<GraphicNote>(GRAPHICAL_NOTE_KEY)?.let {
note = it
graphicNotesViewModel.onNoteOpened(note)
}
arguments?.getParcelableCompat(GRAPHICAL_NOTE_KEY, GraphicNote::class.java)?.let {
note = it
graphicNotesViewModel.onNoteOpened(note)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ 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 dev.arkbuilders.arkmemo.utils.extractDuration
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.collectLatest
Expand Down Expand Up @@ -137,7 +137,7 @@ class NotesViewModel @Inject constructor(
notes.removeIf { it.resource?.id == parentResId ?: note.resource?.id }
}
if (note is VoiceNote) {
note.duration = (voiceNotesRepo as VoiceNotesRepo).extractDuration(note.path.pathString)
note.duration = extractDuration(note.path.pathString)
}
notes.add(note)
this.notes.value = notes
Expand Down
19 changes: 19 additions & 0 deletions app/src/main/java/dev/arkbuilders/arkmemo/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package dev.arkbuilders.arkmemo.utils
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.media.MediaMetadataRetriever
import android.os.Build
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
Expand Down Expand Up @@ -108,3 +110,20 @@ fun millisToString(duration: Long): String {
if (remainingSeconds <= 9) "0$remainingSeconds" else remainingSeconds
}"
}

/**
* Extract duration of a media file and return it in human-readable format
*/
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: Exception) {
Log.e("ExtractDuration", "extractDuration exception: " + e.message)
""
}
}

0 comments on commit 5f7fab0

Please sign in to comment.