获取音频文件时长的方法

public static int getDuration(File source) {
    int duration = 0;
    Uri uri = Uri.fromFile(source);
    MediaPlayer mediaPlayer = new MediaPlayer();
    try {
        mediaPlayer.setDataSource(App.getInstance(), uri);
        mediaPlayer.prepare();
        duration = mediaPlayer.getDuration();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return duration / 1000;

}

你可能感兴趣的:(工具)