Home > java, maven, music > Play MP3 or OGG using javax.sound.sampled, mp3spi, vorbisspi

Play MP3 or OGG using javax.sound.sampled, mp3spi, vorbisspi

I tried to come up with the simplest possible way of writing a Java class that can play mp3 and ogg files, using standard Java Sound APIs, with purely Open Source libraries from the public Maven Central repositories.

The LGPL-licensed mp3spi and vorbisspi libraries from javazoom.net satisfy these requirements and worked for me right away. As service provider implementations (SPI), they transparently add support for the mp3 and ogg audio formats to javax.sound.sampled, simply by being in the classpath.

For my AudioFilePlayer class below I basically took the example code from javazoom and simplified it as much as possible. Please note that it requires Java 7 as it uses try-with-resources.

Maven dependencies

    We have to explicitly instruct Maven to use tritonus-share 0.3.7-2 
    and NOT 0.3.7-1, otherwise vorbisspi won't work.


package net.doepner.audio;

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

import static javax.sound.sampled.AudioSystem.getAudioInputStream;
import static javax.sound.sampled.AudioFormat.Encoding.PCM_SIGNED;

public class AudioFilePlayer {

    public static void main(String[] args) {
        final AudioFilePlayer player = new AudioFilePlayer ();

    public void play(String filePath) {
        final File file = new File(filePath);

        try (final AudioInputStream in = getAudioInputStream(file)) {
            final AudioFormat outFormat = getOutFormat(in.getFormat());
            final Info info = new Info(SourceDataLine.class, outFormat);

            try (final SourceDataLine line =
                     (SourceDataLine) AudioSystem.getLine(info)) {

                if (line != null) {
                    stream(getAudioInputStream(outFormat, in), line);

        } catch (UnsupportedAudioFileException 
               | LineUnavailableException 
               | IOException e) {
            throw new IllegalStateException(e);

    private AudioFormat getOutFormat(AudioFormat inFormat) {
        final int ch = inFormat.getChannels();
        final float rate = inFormat.getSampleRate();
        return new AudioFormat(PCM_SIGNED, rate, 16, ch, ch * 2, rate, false);

    private void stream(AudioInputStream in, SourceDataLine line) 
        throws IOException {
        final byte[] buffer = new byte[65536];
        for (int n = 0; n != -1; n = in.read(buffer, 0, buffer.length)) {
            line.write(buffer, 0, n);
Categories: java, maven, music
  1. December 6, 2016 at 17:28

    Hi, I am doing an AWS blog post on the new Polly API and I would like to use your class here. Do you mind if I cross reference your blog? Thanks, Nick

    • December 7, 2016 at 17:39

      Sure, go ahead. The code is nothing too special, I consider it public domain, so no need to worry about copyright. :)

  2. marianaghdz
    September 1, 2016 at 22:16

    Thank you it’ll be helpful

  3. barbara
    June 6, 2016 at 23:27

    Is there any way to play more than one file at a time? Even if I put it in another thread it doesn’t work.. =T

    • June 7, 2016 at 07:49

      You shouldn’t need threads for concurrent audio. It might be a matter of your audio setup. If you are on Linux, make sure you have Pulseaudio installed. What Operating system are you using?

  4. May 15, 2016 at 20:27

    Sure. Do you have email?

  5. May 15, 2016 at 00:10

    Hi Oliver can´t stopAll()…!!! Why?

    • May 15, 2016 at 17:37

      Can you explain more what your problem is?

  6. March 27, 2016 at 12:39

    But how can I stop it when I want ?

  7. February 12, 2015 at 19:46

    If the audio resource is to be packed inside a jar, you would use URL instead of File. Also, you would use a relative address. Common practice: make a subfolder to the folder of the source code, named “audio”. Then invoke via:
    URL url = this.getClass().getResource(“./audio/yourMusicCue.wav”);
    ais = AudioSystem.getAudioInputStream(url);

  8. Ben Hubbell
    November 29, 2014 at 07:35

    My ears prefer AAC to Ogg Vorbis and MP3. I would be grateful if you would add the ability to play M4A/AAC files to your Java music player.

    • November 29, 2014 at 07:42

      Thanks for the comment, Ben!

      I might try adding http://sourceforge.net/projects/jaadec/ into the mix. Cannot promise anything though, since my time for working on this is limited. :o)

      Related question: What do you think about FLAC ?

  9. January 17, 2014 at 19:48

    Thanks for the simplifications! Just one more question! In which folder should the audio file be saved, in order not to throw exception?

    • January 17, 2014 at 22:43

      You can save the file anywhere. Just make sure you pass the full path to the play(..) method. For example:

      • January 19, 2014 at 05:31

        Thanks for the reply!

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: