0

I know this question has already been asked on this site before, but my question deviates from it a little.

I want to play a .mp3 file without using external Java libraries such as javafx. Is this possible, and if so, how do you do it? I have nothing against the library javafx, I just want to know for educational purposes.

I know that with the javax libraries you can play .wav files quite easily.

public void playSound() {
try {
    AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File("D:/Music/test.wav").getAbsoluteFile());
    Clip clip = AudioSystem.getClip();
    clip.open(audioInputStream);
    clip.start();
    } catch(Exception e) {
    e.printStackTrace();
    }
}

If you can only play .wav files with this library, why only that one file type? Was it just the developers choice, or was there some sort of strategy in choosing this one file type? Is there some speed benefit from accessing .wav files over some other file type?

Community
  • 1
  • 1
syb0rg
  • 8,057
  • 9
  • 41
  • 81
  • Note that javafx can play mp3 files and is included in Oracle Java and OpenJDK for recent Java7 builds on Windows, OS X and Linux - it is not an external library for those systems. – jewelsea Feb 06 '13 at 01:22

5 Answers5

3

MP3 is patent encumbered and cannot be included in the standard set of codecs, as far as I know.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
2

Is this possible..?

Not until/unless Oracle provides an SPI for it in the J2SE, as explained in the Java Sound tag. Wiki. Given the situation as explained by Tom, I'd say that will be 'never'.

If you can only play .wav files with this library, why only that one file type?

Again, see the Wiki for how to get the types for which the J2SE provides inbuilt support.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

There is a small java library that provides all the source code and incorporates a sound player supporting mp3 and even MIDI files. It's very easy to use and you have all the code available. If you want to try: http://imr-lib.blogspot.com

Ismael
  • 11
  • 1
1

Without an additional library you cannot play mp3 files in Java. Period.

However, there are a number of different libraries for Java mp3 support:

  • Already mentioned JavaFX (Oracle started bundling JavaFX with Java 7, so it's readily available).
  • Javazoom, which seems to have been abandoned in 2008.
  • Several FFmpeg wrappers, like Xuggle, which typically require you to learn a new API, because they don't implement javax.sound.sampled.spi. You can find a list of FFmpeg based projects here.
  • SampledSP-libraries for OS X and Windows. This is a collection of libraries that implement javax.sound.sampled.spi to varying degrees (full disclosure - I wrote them).
Hendrik
  • 5,085
  • 24
  • 56
0

there is simple library called jlayer http://www.javazoom.net/javalayer/javalayer.html

zom8ie
  • 21
  • 1
  • 5