生活随笔
收集整理的這篇文章主要介紹了
Android获取视频文件时长
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用MediaMetadataRetriever獲取時長和寬高
public static int getLocalVideoDuration(String filePath
) {int duration
= 0;try {MediaMetadataRetriever mmr
= new MediaMetadataRetriever();mmr
.setDataSource(filePath
);duration
= Integer.parseInt(mmr
.extractMetadata
(MediaMetadataRetriever.METADATA_KEY_DURATION
))/1000;
} catch (Exception e
) {e
.printStackTrace();return duration
;}return duration
;}
使用MediaPlayer獲取時長
private String getDuration(String filePath
){MediaPlayer player
= new MediaPlayer();try {player
.setDataSource(filePath
); player
.prepare();} catch (Exception e
) {Log.d(TAG
, "getDuration: "+e
.toString());}double duration
= player
.getDuration();Log.d(TAG
, "getDuration: " + duration
);player
.release();return String.valueOf(duration
/1000);}
總結
以上是生活随笔為你收集整理的Android获取视频文件时长的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。