Returns duration of the song - Android Media

Android examples for Media:Song

Description

Returns duration of the song

Demo Code


//package com.java2s;

import android.media.MediaMetadataRetriever;

public class Main {
    /**//from w w w  .ja v a2 s . com
     * Returns duration of the song
     *
     * @param filePath Path to song
     * @return Songs duration, -1 on fail
     */
    public static int getDuration(String filePath) {
        try {
            MediaMetadataRetriever metaRetriver = new MediaMetadataRetriever();
            metaRetriver.setDataSource(filePath);
            return Integer
                    .valueOf(metaRetriver
                            .extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
        } catch (Exception e) {
            return -1;
        }
    }
}

Related Tutorials