3

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)

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
KyelJmD
  • 4,682
  • 9
  • 54
  • 77
  • Only [these](http://docs.oracle.com/javase/1.5.0/docs/api/javax/sound/sampled/AudioFileFormat.Type.html) are supported, not mp3. – Esailija Nov 03 '12 at 13:33
  • @Esailija is there anyway to check if it is an mp3 file? – KyelJmD Nov 03 '12 at 13:36
  • See also the **Service Provider Interface** & **MP3 decoding support** sections of the [Java Sound Wiki](http://stackoverflow.com/tags/javasound/info). – Andrew Thompson Nov 03 '12 at 13:52
  • 1
    [Please take a look at this topic.][1] [1]: http://stackoverflow.com/questions/5667454/playing-audio-file-in-java-application – Games Brainiac Nov 03 '12 at 13:59

2 Answers2

4

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.

Ivan Mushketyk
  • 8,107
  • 7
  • 50
  • 67
  • 1
    does it support other audio types besides mp3? – KyelJmD Nov 03 '12 at 13:41
  • I can't seem to see code examples for Apache Tika Library, How would I detect the type of the file? – KyelJmD Nov 03 '12 at 13:46
  • Okay, great it is working but how does Apache Tika, detects the file type? is it by its file extension or it decodes the file? because I'll be uploading the file from a file server, where I want a streamable file(audio clip – KyelJmD Nov 04 '12 at 00:42
  • @kyelJmD Apache Tika detects file format by its content, not by its extension. – Ivan Mushketyk Nov 06 '12 at 14:45
1

You need to add MP3SPI library so that java audio api could recognize and decode mp3 files.

Denis Tulskiy
  • 19,012
  • 6
  • 50
  • 68