Search code examples
javaaudiomp3javasoundogg

Java detecting an audio file (mp3)


I have this code that reads an mp3 file

import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

    public class Sound {
        public static void main(String[] args) {
            File sampleFile = new File("test.mp3");
            try {
                AudioSystem.getAudioFileFormat(sampleFile);
            } catch (UnsupportedAudioFileException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

The problem here is that it is returning file not supported exception, the file here is an mp3 file. Java doesn't support mp3 files? if so what are others to validate an audio file?(like ogg, wav)


Solution

  • You may take a look at Apache Tika library. It can detect type of a file by its content and extract file metadata. It supports mp3 format.

    Here is an example of file type detection with Apache Tika.