Android 使用MediaMetadataRetriever时如何区分文件无效?
我想获取媒体文件的持续时间 我使用MediaMetadataRetriever获取媒体文件的持续时间,如下代码所示Android 使用MediaMetadataRetriever时如何区分文件无效?,android,Android,我想获取媒体文件的持续时间 我使用MediaMetadataRetriever获取媒体文件的持续时间,如下代码所示 File file = new File(viewTag.mFileNode.mName) ; String tempfilePath = file.getPath(); MediaMetadataRetriever fileDuration = new MediaMetadataRetriever()
File file = new File(viewTag.mFileNode.mName) ;
String tempfilePath = file.getPath();
MediaMetadataRetriever fileDuration = new MediaMetadataRetriever();
fileDuration.setDataSource(tempfilePath);
duration = fileDuration.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
Log.i(TAG, "duration = " + duration);
Long VideoDuration = Long.parseLong(duration);
Long minius = (VideoDuration/1000)/60;
Long seconds = (VideoDuration/1000) % 60;
viewTag.mTime.setText(String.valueOf(minius) + ":" + String.valueOf(seconds)) ;
它在媒体文件正常时工作,但在fileDuration.setDataSourcetempfilePath时崩溃;当媒体文件损坏时
媒体文件损坏时如何避免崩溃
我的想法是:
If(the media is normal){
//Use MediaMetadataRetriever to get the duration of file
}else if(the media file is damage) {
//doesn't Use MediaMetadataRetriever to get the duration of file
}
使用MediaMetadataRetriever时如何区分文件是否损坏
或者是否有其他方法获取媒体文件的持续时间?为什么不使用以下方法:
File file = new File(viewTag.mFileNode.mName) ;
String tempfilePath = file.getPath();
MediaMetadataRetriever fileDuration = new MediaMetadataRetriever();
try {
fileDuration.setDataSource(tempfilePath);
duration = fileDuration.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
Log.i(TAG, "duration = " + duration);
Long VideoDuration = Long.parseLong(duration);
Long minius = (VideoDuration/1000)/60;
Long seconds = (VideoDuration/1000) % 60;
viewTag.mTime.setText(String.valueOf(minius) + ":" + String.valueOf(seconds)) ;
} catch (Exception ex) {
// the file must be corrupt, do something else...
} finally {
// call release when you are done!
fileDuration.release();
}
也适用于损坏的文件。您也可以尝试该库。此库使用FFmpeg,它可以更好地处理损坏的文件。如果这个答案解决了你的问题,你能接受吗?