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 headers=null; 
    if (headers == null) { headers = new HashMap();         
    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; }

你可能感兴趣的:(android 获取音频时长)