handle fails at fetching alternative file duration and size gracefully

This commit is contained in:
tibbi 2020-04-02 19:18:03 +02:00
parent a4a5f180ea
commit be14e65029
1 changed files with 13 additions and 5 deletions

View File

@ -231,15 +231,23 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
} }
private fun getDurationFromUri(id: Long): Long { private fun getDurationFromUri(id: Long): Long {
return try {
val retriever = MediaMetadataRetriever() val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, getAudioFileContentUri(id)) retriever.setDataSource(context, getAudioFileContentUri(id))
val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION) val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
return Math.round(time.toLong() / 1000.toDouble()) Math.round(time.toLong() / 1000.toDouble())
} catch (e: Exception) {
0L
}
} }
private fun getSizeFromUri(id: Long): Int { private fun getSizeFromUri(id: Long): Int {
val recordingUri = getAudioFileContentUri(id) val recordingUri = getAudioFileContentUri(id)
return context.contentResolver.openInputStream(recordingUri)?.available() ?: 0 return try {
context.contentResolver.openInputStream(recordingUri)?.available() ?: 0
} catch (e: Exception) {
0
}
} }
private fun initMediaPlayer() { private fun initMediaPlayer() {