Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 331
pveeckhout Avatar asked Apr 27 '11 18:04

pveeckhout


1 Answers

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.

like image 74
Gordolio Avatar answered Oct 04 '22 02:10

Gordolio