adding a better way of retrieving video durations

This commit is contained in:
tibbi 2020-05-04 10:54:30 +02:00
parent ed1733206b
commit bb84af3a3e
5 changed files with 34 additions and 19 deletions

View file

@ -7,7 +7,7 @@ buildscript {
propMinSdkVersion = 21
propTargetSdkVersion = propCompileSdkVersion
propVersionCode = 1
propVersionName = '5.27.2'
propVersionName = '5.27.3'
kotlin_version = '1.3.72'
}

View file

@ -108,13 +108,13 @@ class PropertiesDialog() {
fileDirItem.getResolution(activity)?.let { addProperty(R.string.resolution, it.formatAsResolution()) }
}
fileDirItem.path.isAudioSlow() -> {
fileDirItem.getDuration()?.let { addProperty(R.string.duration, it) }
fileDirItem.getDuration(activity)?.let { addProperty(R.string.duration, it) }
fileDirItem.getSongTitle()?.let { addProperty(R.string.song_title, it) }
fileDirItem.getArtist()?.let { addProperty(R.string.artist, it) }
fileDirItem.getAlbum()?.let { addProperty(R.string.album, it) }
}
fileDirItem.path.isVideoSlow() -> {
fileDirItem.getDuration()?.let { addProperty(R.string.duration, it) }
fileDirItem.getDuration(activity)?.let { addProperty(R.string.duration, it) }
fileDirItem.getResolution(activity)?.let { addProperty(R.string.resolution, it.formatAsResolution()) }
fileDirItem.getArtist()?.let { addProperty(R.string.artist, it) }
fileDirItem.getAlbum()?.let { addProperty(R.string.album, it) }

View file

@ -703,6 +703,35 @@ fun Context.getVideoResolution(path: String): Point? {
return point
}
fun Context.getVideoDuration(path: String): Int? {
val projection = arrayOf(
Video.Media.DURATION
)
val uri = Files.getContentUri("external")
val selection = "${Video.Media.DATA} = ?"
val selectionArgs = arrayOf(path)
try {
val cursor = contentResolver.query(uri, projection, selection, selectionArgs, null)
cursor?.use {
if (cursor.moveToFirst()) {
return Math.round(cursor.getIntValue(Video.Media.DURATION) / 1000.toDouble()).toInt()
}
}
} catch (ignored: Exception) {
}
try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(path)
return Math.round(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION).toInt() / 1000f)
} catch (ignored: Exception) {
}
return null
}
fun Context.getStringsPackageName() = getString(R.string.package_name)
fun Context.getFontSizeText() = getString(when (baseConfig.fontSize) {

View file

@ -88,20 +88,6 @@ fun String.containsNoMedia() = File(this).containsNoMedia()
fun String.doesThisOrParentHaveNoMedia() = File(this).doesThisOrParentHaveNoMedia()
fun String.getDuration() = getFileDurationSeconds()?.getFormattedDuration()
fun String.getFileDurationSeconds(): Int? {
return try {
val retriever = MediaMetadataRetriever()
retriever.setDataSource(this)
val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
val timeInMs = java.lang.Long.parseLong(time)
(timeInMs / 1000).toInt()
} catch (e: Exception) {
null
}
}
fun String.getFileArtist(): String? {
return try {
val retriever = MediaMetadataRetriever()

View file

@ -90,9 +90,9 @@ open class FileDirItem(val path: String, val name: String = "", var isDirectory:
fun getParentPath() = path.getParentPath()
fun getDuration() = path.getDuration()
fun getDuration(context: Context) = context.getVideoDuration(path)?.getFormattedDuration()
fun getFileDurationSeconds() = path.getFileDurationSeconds()
fun getFileDurationSeconds(context: Context) = context.getVideoDuration(path)
fun getArtist() = path.getFileArtist()