[英]I can't find how to get video duration in Google Photos API. Any clue?
The Google Photos library API does not return the duration of a video. Google 相册库 API 不返回视频的时长。 API provides a lot of misc information like camera model and other stuff, but missing very basic things, like video duration, archive status and file size
API 提供了很多杂项信息,例如相机 model 和其他内容,但缺少非常基本的信息,例如视频持续时间、存档状态和文件大小
If you have access to the file uri you could try something like this:如果您有权访问文件 uri,则可以尝试以下操作:
fun getFileDuration(context: Context, uri: Uri): Int {
try {
val metadataRetriever = MediaMetadataRetriever()
metadataRetriever.setDataSource(context, uri)
val durationStr = metadataRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
if (durationStr != null) return Integer.parseInt(durationStr)
} catch (e: Exception) {
e.printStackTrace()
Log.w("getFileDuration", "Error getting duration of file with uri $uri with ${e.message}")
}
return 0
}
func getFileDuration(_ uri: URL) -> Int {
let asset = AVURLAsset(url: uri)
let audioDuration = asset.duration.seconds * 1000
return Int(audioDuration)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.