我想获得媒体文件的持续时间。
我使用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.setDataSource(tempfilePath);
时崩溃。
如何在媒体文件损坏时避免崩溃?
我的想法是:
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时如何区分文件损坏?
或是否有其他方法可以获取媒体文件的持续时间?
答案 0 :(得分:0)
为什么不使用以下方法:
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();
}
FFmpegMediaMetadataRetriever也适用于损坏的文件。您也可以尝试该库。