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.
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