How to get the length of an audio file in java

How to get the length of an audio file

Briefly explain the background

Use the LINE Messaging API to get the duration of the audio file when linking * the official LINE account with our web service chat tool called Channel Talk. By the way, I stumbled, so I shared it.

In order to synchronize the audio file uploaded by channel talk with the LINE official account so that it can be seen from the end user side, the audio file is sent to the Messaging API, but the length of the audio file must also be sent.

Implementation method

However, there is no API to get the length of this audio file from the file.

But the method is easy. You can implement it yourself. It can be calculated from the total number of frames that can be obtained from the audio file and the frame rate.

File file = new File(fileUrl);

try {
    //Preparing to get the metadata of the audio file
    AudioInputStream stream = AudioSystem.getAudioInputStream(file);

    //Total number of frames in the audio file
    long length = stream.getFrameLength();
    AudioFormat format = stream.getFormat();

    // frame ->Number of frames processed per second
    float frame = format.getSampleRate();
    //Total number of frames ➗ Number of frames processed per second=length
    int duration = (int)(length/frame);

} catch (UnsupportedAudioFileException e) {
    return false;

Maybe I should have done this from the beginning without searching for anything. .. .. It was easier than I expected!

Recommended Posts

How to get the length of an audio file in java
How to get the date in java
[Java] How to get the authority of the folder
How to get the absolute path of a directory running in Java
[Java] How to get the maximum value of HashMap
[Java] Get the file in the jar regardless of the environment
[Swift] How to get the number of elements in an array (super basic)
How to get the class name / method name running in Java
An unsupported Java version How to get rid of errors
How to derive the last day of the month in Java
How to get the class name of the argument of LoggerFactory.getLogger when using SLF4J in Java
Get the result of POST in Java
[Java] How to get the current directory
[Swift5] How to get an array and the complement of arrays
How to get the id of PRIMAY KEY auto_incremented in MyBatis
Get the public URL of a private Flickr file in Java
How to increment the value of Map in one line in Java
How to specify an array in the return value / argument of the method in the CORBA IDL file
How to get Class from Element in Java
How to get today's day of the week
How to solve an Expression Problem in Java
[Java] How to get the redirected final URL
The story of forgetting to close a file in Java and failing
How to find out the Java version of a compiled class file
[Java] How to get to the front of a specific string using the String class
How to find the total number of pages when paging in Java
How to get the value after "_" in Windows batch like Java -version
How to create your own annotation in Java and get the value
Get the URL of the HTTP redirect destination in Java
graphql-ruby: How to get the name of query or mutation in controller Note
[Java] How to omit the private constructor in Lombok
How to write Scala from the perspective of Java
How to get the ID of a user authenticated with Firebase in Swift
[Java] How to extract the file name from the path
Source used to get the redirect source URL in Java
[Java] How to easily get the longest character string of ArrayList using stream
As of April 2018 How to get Java 8 on Mac
[Java] Get the file path in the folder with List
[Android] How to get the setting language of the terminal
How to convert a file to a byte array in Java
[Rails] How to get the contents of strong parameters
Summary of how to implement default arguments in Java
How to set environment variables in the properties file of Spring boot application
[Swift] How to get the document ID of Firebase
[Rails] How to display an image in the view
Sample code to get the values of major SQL types in Java + MySQL 8.0
What to do if you can't get the text of an element in Selenium
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Java] How to get the key and value stored in Map by iterative processing
How to use git with the power of jgit in an environment without git commands
How to save a file with the specified extension under the directory specified in Java to the list
[Java] How to display the day of the week acquired by LocalDate, DateTimeformatter in Japanese
Summary of how to use the proxy set in IE when connecting with Java
How to correctly check the local HTML file in the browser
Get to the abbreviations from 5 examples of iterating Java lists
How to retrieve the hash value in an array in Ruby
How to get the longest information from Twitter as of 12/12/2016
How to add elements without specifying the length of the array
Get attributes and values from an XML file in Java
How to change the contents of the jar file without decompressing
How to switch Java in the OpenJDK era on Mac