Android获取视频音频的时长的方法

android当中获取视频音频的时长,我列举了三种。
1:获取视频URI后获取cursor

cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DURATION));

 

2:根据MediaPlayer获取:

MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(url);
mediaPlayer.prepare();
mediaPlayer.getDuration();

 

3:采用MediaMetadataRetriever的方式:

    public static String getRingDuring(String mUri){
        String duration=null;
        android.media.MediaMetadataRetriever mmr = new android.media.MediaMetadataRetriever();

        try {
            if (mUri != null) {
                HashMap<String, String> headers=null;
                if (headers == null) {
                    headers = new HashMap<String, String>();
                    headers.put("User-Agent", "Mozilla/5.0 (Linux; U; Android 4.4.2; zh-CN; MW-KW-001 Build/JRO03C) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/1.0.0.001 U4/0.8.0 Mobile Safari/533.1");
                }
                mmr.setDataSource(mUri, headers);
            }

             duration = mmr.extractMetadata(android.media.MediaMetadataRetriever.METADATA_KEY_DURATION);
        } catch (Exception ex) {
        } finally {
            mmr.release();
        }
        LogUtil.e("ryan","duration "+duration);
         return duration;
    }
posted @ 2017-07-25 12:10  一点点征服  阅读(11986)  评论(0编辑  收藏  举报