Exception in reading an MP3 file through AudioSystem.getAudioInputStream(file) - java

I am trying to read an MP3 file through class javax.sound.sampled.AudioSystem but I am getting an UnsupportedAudioFileException.
My code trying to read the audio file looks like:-
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(file);
I am getting the following exception:-
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
Does AudioSystem class not support mp3 format? If not then what formats does it supports? Or Am I doing some mistake here?

No it doesn't support MP3 (hence the UnsupportedAudioFileException). The supported files are quite basic (WAV and that sort of thing), so for any advanced codecs you'll need separate libraries.

Related

Adding audio from WAV file to MP4 using JCODEC

I'm trying to figure out how to add an audio from an existing WAV file to an existing MP4 file using jcodec. Looked at https://github.com/jcodec/jcodec/issues/187, https://github.com/jcodec/jcodec/issues/164 to get some clues, still cannot figure out how to do it.

Could not get audio input stream from file

We use the following code in our application to try to read the duration of an MP3 file:
final File file = new File(filename);
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
While trying to get an AudioInputStream out of a file an UnsupportedAudioFileException is thrown. I used the same code for a JUnit test in another project where the exception does not occur instead an is returned.
As I debugged into the Method getAudioInputStream(file) I found out that the exception is thrown because of getAudioFileReaders() returned an empty provider list. That is not the case in the other project in which my JUnit test is.
So I got two questions:
1. Why is the provider list empty?
2. Do I need to configure something in order to get at least one provider?
Assuming you are trying to open a .mp3, you need a Java SPI which adds the audio format support.
You can take a look at MP3SPI library from Javazoom which would let you open mp3 files. They have VorbisSPI for ogg format support as well.
I'd like to point out that you cannot play .mp3 only with pure java API
The AudioStream class does not supports mp3 natively.
You can check it on the javadoc for all the available formats
So i wonder how it's possible that on another computer you are able to run it,
if java itself doesn't allow it.

Decoding Mp3 file in java

I am trying to design Shazam like app for Desktop in java.I took input from Microphone having format
sample rate=44100,sample size in bits=16,Channels=1,signed=true and bigendian=true. and perform FFT.Similarly for creating Database of MP3 songs i am decoding MP3 file in the same format as input from microphone.
and this process is giving me too much samples and that slows FFT process of mp3 files.
So my doubt is that how i can handle this problem.

Convert MP3 to PCM in Java

I want to convert MP3 file to PCM in Java.
How to do that?
Get the mp3plugin.jar of the JMF.
Add it to the run-time class-path of the app. to provide a decoder SPI for MP3.
Get an AudioInputStream for the MP3 from the AudioSystem of Java Sound.
Convert it to PCM using getAudioInputStream(AudioFormat,AudioInputStream).

Audio Files accurate seek to

Which Audio format should I use to get the best accurate seek to? (play the song from an offset)
I whant this to be accurate on the millisecond.
is that Possible?
I can say that the format of the audio file matters. I had this problem with long mp3 file. As the mp3 files aren't designed for such accurate seeking you can check this answer for more details
In my case the bitrate of the mp3 file was low, It was 96Kps. and the VBR data wasn't correct which caused the mp3 file to seek wrongly on android.
My intent was to make android and IOS app that seeks accurately through this file but I failed to find the correct format that is suitable for both.
For the IOS, I used MP3 Diags and applied custom transformations to remove and rebuild the VBR
For Android, I converted the file to m4a format and it worked perfectly. I used Audacity and installed the FFmpeg package to be able to export to m4a format.
This is the file sample that contains the problem I mentioned
The format doesn't matter, as long as it's compatible with Android's MediaPlayer class. It has a seekTo() method that is accrate to the millisecond.

Categories