读取本地视频文件时间

 /**
     * 获取本地视频时长
     *
     * @return
     */
    public static int getLocalVideoDuration(String videoPath) {
        int duration;
        try {
            MediaMetadataRetriever mmr = new MediaMetadataRetriever();
            mmr.setDataSource(videoPath);
            duration = Integer.parseInt(mmr.extractMetadata
                    (MediaMetadataRetriever.METADATA_KEY_DURATION));
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return duration;
    }

将其转化为时分秒

/**
 * @author chenli
 * @create 2020/2/18 0018
 * @Describe
 */
public class TimeFormatUtil {
    /**
     * @param t 毫秒
     */
    public static String format(int t) {

        if (t < 60000) {
            return (t % 60000) / 1000 + "秒";
        } else if ((t >= 60000) && (t < 3600000)) {
            return getString((t % 3600000) / 60000) + ":" + getString((t % 60000) / 1000);
        } else {
            return getString(t / 3600000) + ":" + getString((t % 3600000) / 60000) + ":" + getString((t % 60000) / 1000);
        }
    }


    private static String getString(int t) {
        String m = "";
        if (t > 0) {
            if (t < 10) {
                m = "0" + t;
            } else {
                m = t + "";
            }
        } else {
            m = "00";
        }
        return m;
    }
}
发布了89 篇原创文章 · 获赞 33 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/chenli_001/article/details/104376321