Hey, Does anyone see anything wrong with this? I don't know what's up, it's throwing the IOException everyt time even though the mp3 is in the source folder. (I tried .wav as well). I'm trying to place this in a GUI, but testing it seperately first.

import java.io.*;
import sun.audio.*;
import java.io.File;
import javax.swing.*;
import java.awt.event.*;
public class Test2 {

   public static void main(String[] args) {

AudioPlayer player = AudioPlayer.player;
   AudioStream stream;
   AudioData data;
   ContinuousAudioDataStream loop = null;
   InputStream input = null;

       input = new FileInputStream("GarageBand.mp3");
       stream = new AudioStream(input);
       data = stream.getData();
       loop = new ContinuousAudioDataStream(data);

   catch(IOException err)
          System.out.println("fle not found");

3 Years
Discussion Span
Last Post by stultuske

You are ignoring whatever error messages you might be getting. There are many kinds of java.io.IOException, and each one will at least include a stack trace that will tell you exactly which line of your source code is causing the exception. The IOExceptions may even include a helpful error message if you are lucky. To ignore all of that and simply print "fle not found" is surely a bigger mistake than whatever is causing your exception to be thrown, especially when you clearly want more information about the cause of the exception. It is also usually a good idea to include the details of the exception along with your source code when asking for help.

Have you considered using javax.sound.sampled?


Thanks for the help.

When I use this as the catch:

catch(IOException error)
       //System.out.println("fle not found");

I end up getting: "java.io.IOException: could not create audio stream from input stream" as the output.
I've heard that using javax.sound.sampled is good for things like game sounds, because you have to use Clip class? But doesn't it have a few second limit? The issue is I have to play full songs. Any tips?


When I use a wav file I'm getting this output instead "java.io.IOException: could not create AudioData object" - Maybe mp3 doesn't work. It seems to read in the file fine now, but has issues making the AudioData.


Somehow I got it to work like this:

            input = new FileInputStream("01 You Know I Got the Time.wav");
            stream = new AudioStream(input);


that is because Java, by default, supports wav, but it doesn't support mp3.
if you want to play mp3 files, there are a few ways you can go:
1. the Java Media Framework (JMF), which is no longer supported and was, in my opinion, quite a pain to work with.
2. the JLayer library, which I found pretty easy to work with, and well documented.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.