error playing sound java (No line matching interface Clip supporting format)

15,476

Solution 1

You can actually play sound above 40 mb, if needed, thats how far i went :p, the problem is mostly eclipse, and to be more exact its the .metadata folder in your workspace i think its like a small plugin that only gets uploaded half of the time, so the problem lies with your editor and not the code, the code above is working perfectly since i could play songs without any trouble. Make sure your paths are correct, and try to get a correct version of the .metadata and you should be fine. A friend of mine had the same problem, and i gave him my copy of the workspace and .metadata and it worked perfectly.

Solution 2

I was experiencing this same problem on a raspberry pi. It would play the first 5 files just fine, then I'd get the error. It turned out that I was not closing the clip when I needed to.

Clip clip = AudioSystem.getClip();
clip.addLineListener(event -> {
    if(LineEvent.Type.STOP.equals(event.getType())) {
        clip.close();
    }
});
ByteArrayInputStream audioBytes = new ByteArrayInputStream(SOUNDS.get(file));
AudioInputStream inputStream = AudioSystem.getAudioInputStream(audioBytes);
clip.open(inputStream);
clip.start();

After adding the line listener and closing the clip when it stopped, the errors went away.

Share:
15,476
pveeckhout
Author by

pveeckhout

I'm a Bachelor in Science applied ICT. My primary programming language is Java, but I am also learning Ruby and C++ to expand on the basic knowledge from my education. I really love to program, I think it feels kind of like being a god in your own small universe. Once you one of your creations comes to “life”, there is no greater satisfaction in the world.

Updated on June 25, 2022

Comments

  • pveeckhout
    pveeckhout almost 2 years

    We are trying to integrated sound in one of our project, my team members don't get this error, and I get it on two different machines.

    Stack trace:

    Exception in thread "SoundPlayer" java.lang.IllegalArgumentException: No line matching interface Clip supporting format PCM_SIGNED 16000.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian, and buffers of 11129272 to 11129272 bytes is supported.
    at javax.sound.sampled.AudioSystem.getLine(Unknown Source)  
    at sound.Music.run(Music.java:86)  
    at java.lang.Thread.run(Unknown Source)
    

    Code:

    package sound;
    
    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.Clip;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.FloatControl;
    import javax.sound.sampled.LineEvent;
    import javax.sound.sampled.LineListener;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.UnsupportedAudioFileException;
    
    
    public class Music implements LineListener, Runnable
    {
    
    private File soundFile;
    private Thread thread;
    private static Music player;
    private Music audio;
    private Clip clip;
    
        public Music()
        {
        }
    
        public void playSiren(String musicFileName)
        {
            Music p = getPlayer();
            p.playSirenFile(musicFileName);
        }
    
        private void playSirenFile(String musicFileName)
        {
            this.soundFile = new File("Music/"+musicFileName+".wav");
            thread = new Thread(this);
            thread.setName("SoundPlayer");
            thread.start();
        }
    
        public void run()
        {
            try
            {
                AudioInputStream stream = AudioSystem.getAudioInputStream(this.soundFile);
                AudioFormat format = stream.getFormat();
    
    /**
    * we can't yet open the device for ALAW/ULAW playback, convert
    * ALAW/ULAW to PCM
    */
                if ((format.getEncoding() == AudioFormat.Encoding.ULAW) || (format.getEncoding() == AudioFormat.Encoding.ALAW))
                {
                    AudioFormat tmp = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    format.getSampleRate(),
                    format.getSampleSizeInBits() * 2, format.getChannels(),
                    format.getFrameSize() * 2, format.getFrameRate(), true);
                    stream = AudioSystem.getAudioInputStream(tmp, stream);
                    format = tmp;
                }
                DataLine.Info info = new DataLine.Info(Clip.class, stream
                .getFormat(), ((int) stream.getFrameLength() * format
                .getFrameSize()));
    
                clip = (Clip) AudioSystem.getLine(info);
                clip.addLineListener(this);
                clip.open(stream);
                clip.start();
                try
                {
                    thread.sleep(99);
                }
                catch (Exception e)
                {
                }
                while (clip.isActive() && thread != null)
                {
                    try
                    {
                        thread.sleep(99);
                    }
                    catch (Exception e)
                    {
                        break;
                    }
                }
                clip.loop(99999999);
            }
            catch (UnsupportedAudioFileException e)
            {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            catch (IOException e)
            {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
            catch (LineUnavailableException e)
            {
            // TODO Auto-generated catch block
            e.printStackTrace();
            }
        }
    
        private static Music getPlayer()
        {
            if (player == null)
            {
                player = new Music();
            }
            return player;
        }
    
        public void update(LineEvent event)
        {
        }
    
        public void stopClip()
        {
            clip.stop();
        }
    
        public void closeClip()
        {
            clip.close();
        }
    
        public void startClip()
        {
            clip.start();
        }
        public void volume(float volume)
        {
            /*
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            gainControl.setValue(-50.0f); // Reduce volume IN DECIBELS
            clip.start();
            */
        }
    }
    

    We call this class from domainController with

    audio = new Music();  
    audio.playSiren("stillAliveDecent");
    

    does Anyone have an idea how this exception can be resolved? I tried reinstalling my editor software (Eclipse) but to no avail.

    thanks allot in advance.

    Edit

    We just tried switching the sound file. We tried running it with much smaller file. this now works, but once we switch back to the larger .wav file (10+MB) I get the exception again.

    Only using smaller files is not really an option as we would like to use some self made songs which are quite long.

    Edit 2
    I'm quite sure it isn't a corrupted wav. we recompiled it, even used another wave of similar length and size, and i'm still the only one getting this error.

    some extra requested info:

    OS: Windows 7 64bit Ultimate
    JDK: 1.6.0_22

    Edit 3

    After some wave creating and playing we have come to the conclusion that for some reason I can't play wave's larger than 2MB.

    Still why aren't my teammates affected by this?

    • Kylar
      Kylar about 13 years
      Is it possible that the .wav file is slightly corrupt and has a bad file ending?
    • Favonius
      Favonius about 13 years
      @Pieter Van Eeckhout: It would help if you mention the OS and the JDK version you are using. Its working on my Windows Vista machine with JDK 1.6_b18. Also, the problem is related to eclipse only in case you have set your JRE to some very old version. Check your eclipse JRE and try again.
    • Favonius
      Favonius about 13 years
      @Pieter Van Eeckhout: Its a very old apple post: lists.apple.com/archives/java-dev/2005/May/msg00181.html. Check it out.
    • pveeckhout
      pveeckhout about 13 years
      @Kylar: that was our first thought too, so we recompiled the wav, and still had the same problem, so no we used another wav of similar length and size, same problem. @Favonius: Thanks for the suggestions and edit to the post (first post seem hard to get the formating right), I will add the extra info to the question
    • Rogach
      Rogach about 13 years
      The Clip object has some limitations on size of buffer, and from my experience it does not handle more than 1MB. Maybe you can do it better with SourceDataLine? Java Tutorial, Playing Back Audio: download.oracle.com/javase/tutorial/sound/playing.html"
    • Rogach
      Rogach about 13 years
      Also, I ran into a problem with Java sound on Linux - when you have a another player working in background (it was Banshee in my case) that player blocks Java access to sound. Maybe it is different under Windows.
  • pveeckhout
    pveeckhout about 13 years
    Thanks ths worked perfectly, one of my team members send me his complete workspace (including metadata and his .eclipse folder) i reinstalled eclipse with this existing workspace and then te clip plays perfectly. thanks alot