Playing MP3 sound in a Java applet.

Posted on 2006-04-24
Last Modified: 2010-05-18
Anyone gotten JavaZOOM's MP3 SPI working?

Using their code example at, and R.J. Lorimer's example at (which is essentially the same thing), I can only play AIF and AU files.  When I attempt to play an MP3, I get an error, "javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL([path to audio file])."

The procedure in my code is:

      private void mp3Play(URL urlMP3) {
            try {
                  AudioInputStream audioIn = AudioSystem.getAudioInputStream(urlMP3);
                  AudioFormat baseFormat = audioIn.getFormat();
                  AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                                                                                    baseFormat.getChannels() * 2,
                  AudioInputStream dataIn = AudioSystem.getAudioInputStream(decodedFormat, audioIn);
                  byte[] data = new byte[4096];
                  DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
                  SourceDataLine line = null;
                  line = (SourceDataLine)AudioSystem.getLine(info);
                  if (line != null) {
                        int nBytesRead = 0, nBytesWritten = 0;

                        while (nBytesRead != -1) {
                              nBytesRead =, 0, data.length);
                              if (nBytesRead != -1)
                                    nBytesWritten = line.write(data, 0, nBytesRead);

            } catch (Exception e) {
                  System.out.println("MP3 PLAYBACK ERROR: " + e.toString() + "(" + urlMP3.toString() + ")");

Again, this code works fine if I specify an AIF or AU file.  I suspect that the format of the MP3 file that I'm trying to play is incompatible with the code, but I haven't tried encoding the sound differently, yet.


-- MiloDC
Question by:MiloDC

    Author Comment

    Update: Tried different encoding, still no luck...

    -- MiloDC
    LVL 92

    Expert Comment

    how did u install the mp3 service provider?

    Author Comment

    > Comment from objects
    > how did u install the mp3 service provider?

    Man, I just put jl1.0.jar, tritonus_share.jar, and mp3spi1.9.4.jar in my CLASSPATH.  No further set-up is outlined on either of the two pages that I mentioned, above; is there more to installing the SPI than this?  If so, is it going to be possible for me to employ the MP3 SPI in an applet?

    -- MiloDC
    LVL 92

    Accepted Solution

    try putting them on your server and adding them in the archive parameter of your applet tag

    Author Comment

    Awesome, objects, that did it!

    I just added ARCHIVE="jl1.0.jar,mp3spi1.9.4.jar,tritonus_share.jar" to the APPLET tag, and it worked.


    -- MiloDC

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Suggested Solutions

    Title # Comments Views Activity
    computer science syllabus 3 37
    for i loop in grovy 1 25
    java  and programming certification ? 4 25
    @SBGen Method 3 16
    If you have upgraded to Java2 update 10 on a Microsoft Windows client, you may have discovered that your Java application does not work as it did before.  For example, the colors of your Java2D graphic may be all wrong for no apparent reason. Aft…
    After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
    This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now