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

前端 未结 2 466
离开以前
离开以前 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条回答
  • 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)
    
    0 讨论(0)
  • 2020-11-30 05:27

    Given a File you can write

    File file = ...;
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
    AudioFormat format = audioInputStream.getFormat();
    long frames = audioInputStream.getFrameLength();
    double durationInSeconds = (frames+0.0) / format.getFrameRate();  
    
    0 讨论(0)
提交回复
热议问题