-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from xyoye/dev_4.1.0
Dev 4.1.0
- Loading branch information
Showing
15 changed files
with
415 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
common_component/src/main/java/com/xyoye/common_component/utils/meida/VideoExtension.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.xyoye.common_component.utils.meida | ||
|
||
import com.xyoye.common_component.config.AppConfig | ||
|
||
/** | ||
* Created by xyoye on 2024/2/4 | ||
*/ | ||
|
||
object VideoExtension { | ||
|
||
// 视频文件扩展名分隔符 | ||
private const val SEPARATOR = "," | ||
|
||
// 默认支持的视频文件扩展名 | ||
private val defaultSupport = arrayOf( | ||
"3gp", "asf", "asx", "avi", | ||
"dat", "flv", "m2ts", "m3u8", | ||
"m4s", "m4v", "mkv", "mov", | ||
"mp4", "mpe", "mpeg", "mpg", | ||
"rm", "rmvb", "vob", "wmv" | ||
) | ||
|
||
// 当前支持的视频文件扩展名 | ||
private val _support = defaultSupport.toMutableList() | ||
val support: List<String> get() = _support | ||
|
||
// 当前支持的视频文件扩展名文本 | ||
val supportText: String get() = _support.joinToString(SEPARATOR) | ||
|
||
init { | ||
refresh() | ||
} | ||
|
||
/** | ||
* 重置为默认支持的视频文件扩展名 | ||
*/ | ||
fun resetDefault() { | ||
AppConfig.putSupportVideoExtension(defaultSupport.joinToString(SEPARATOR)) | ||
refresh() | ||
} | ||
|
||
/** | ||
* 更新支持的视频文件扩展名 | ||
*/ | ||
fun update(extensionText: String): Boolean { | ||
return update(extensionText.split(SEPARATOR)) | ||
} | ||
|
||
/** | ||
* 更新支持的视频文件扩展名 | ||
*/ | ||
fun update(extensions: List<String>): Boolean { | ||
val storeExtensions = extensions.filter { it.isNotBlank() }.map { it.lowercase() } | ||
if (storeExtensions.isEmpty()) { | ||
return false | ||
} | ||
AppConfig.putSupportVideoExtension(storeExtensions.joinToString(SEPARATOR)) | ||
refresh() | ||
return true | ||
} | ||
|
||
/** | ||
* 是否是支持的视频文件扩展名 | ||
*/ | ||
fun isSupport(extension: String): Boolean { | ||
return _support.contains(extension.lowercase()) | ||
} | ||
|
||
/** | ||
* 从磁盘中获取支持的视频文件扩展名 | ||
*/ | ||
private fun refresh() { | ||
val stored = AppConfig.getSupportVideoExtension()?.split(SEPARATOR) | ||
if (stored.isNullOrEmpty()) { | ||
return | ||
} | ||
_support.clear() | ||
_support.addAll(stored) | ||
} | ||
} |
90 changes: 90 additions & 0 deletions
90
common_component/src/main/java/com/xyoye/common_component/utils/meida/VideoScan.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.xyoye.common_component.utils.meida | ||
|
||
import android.media.MediaMetadataRetriever | ||
import android.net.Uri | ||
import com.xyoye.common_component.base.app.BaseApplication | ||
import com.xyoye.common_component.extension.isInvalid | ||
import com.xyoye.common_component.utils.isVideoFile | ||
import com.xyoye.data_component.entity.VideoEntity | ||
import java.io.File | ||
|
||
/** | ||
* Created by xyoye on 2024/2/4 | ||
*/ | ||
|
||
object VideoScan { | ||
|
||
/** | ||
* 遍历路径内的视频文件 | ||
*/ | ||
fun traverse(filePath: String): List<VideoEntity> { | ||
return try { | ||
traverse(File(filePath)) | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
emptyList() | ||
} | ||
} | ||
|
||
/** | ||
* 遍历文件内的视频文件,包括文件自身 | ||
*/ | ||
private fun traverse(file: File): List<VideoEntity> { | ||
if (file.isFile) { | ||
return if (file.isInvalid()) { | ||
return emptyList() | ||
} else if (isVideoFile(file.absolutePath)) { | ||
listOf(generateVideoEntity(file)) | ||
} else { | ||
emptyList() | ||
} | ||
} | ||
|
||
if (file.exists().not() || file.canRead().not()) { | ||
return emptyList() | ||
} | ||
return file.listFiles() | ||
?.flatMap { traverse(it) } | ||
?: emptyList() | ||
} | ||
|
||
/** | ||
* 生成视频数据库实体 | ||
*/ | ||
private fun generateVideoEntity(file: File): VideoEntity { | ||
return VideoEntity( | ||
0, | ||
0, | ||
0, | ||
file.absolutePath, | ||
file.parentFile?.absolutePath.orEmpty(), | ||
null, | ||
null, | ||
getVideoDuration(file), | ||
file.length(), | ||
isFilter = false, | ||
isExtend = true | ||
) | ||
} | ||
|
||
/** | ||
* 获取视频时长 | ||
*/ | ||
private fun getVideoDuration(videoFile: File): Long { | ||
if (videoFile.isInvalid()) { | ||
return 0 | ||
} | ||
val retriever = MediaMetadataRetriever() | ||
return try { | ||
retriever.setDataSource(BaseApplication.getAppContext(), Uri.fromFile(videoFile)) | ||
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION) | ||
?.toLongOrNull() | ||
?: 0 | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
0 | ||
} finally { | ||
retriever.release() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.