How do I get a sound file's total time in Java?

前端 未结 2 469
离开以前
离开以前 2020-11-30 05:04

How do I get a sound file\'s total time in Java?

--UPDATE

Looks like this code does de work: long audioFileLength = audioFile.length();

    r         


        
2条回答
  •  旧时难觅i
    2020-11-30 05:21

    This is a easy way:

    FileInputStream fileInputStream = null;
    long duration = 0;
    
    try {
        fileInputStream = new FileInputStream(pathToFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    
    try {
        duration = Objects.requireNonNull(fileInputStream).getChannel().size() / 128;
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println(duration)
    

提交回复
热议问题