Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error playing audio file from Java via PulseAudio on Ubuntu

The message on the shell is:

Exception in thread "main" java.lang.IllegalArgumentException: Invalid format
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.createStream(PulseAudioDataLine.java:142)
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:99)
    at org.classpath.icedtea.pulseaudio.PulseAudioDataLine.open(PulseAudioDataLine.java:283)
    at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:402)
    at org.classpath.icedtea.pulseaudio.PulseAudioClip.open(PulseAudioClip.java:453)
    at reprod.ReproducirFichero(reprod.java:16)
    at reprod.main(reprod.java:44)

I try to download new drivers for audio, i try to reinstall openJDK 7 and openJRE 7 and also i try to install java 7.

I have proved my code in another computer and it works, the desktop board that i use is an intel d525mw, the audio format that i´m trying to play is .wav.The version of linux that I use is Ubuntu 12.04.3.Please I need help.Thanks

here is party of my code, and i try to play a .wav audio format

import javax.sound.sampled.*;

public class reprod {

public static void play(){
    try {
        Clip cl = AudioSystem.getClip();

        File f = new File("/home/usr/Desktop/d.wav");
        AudioInputStream ais = AudioSystem.getAudioInputStream(f);

        cl.open(ais);

        cl.start();
        System.out.println("playing...");

        while (cl.isRunning())
            Thread.sleep(4000);


        cl.close();

the version of linux that I use is Ubuntu 12.04.3

like image 622
blackhawk87 Avatar asked Dec 15 '25 12:12

blackhawk87


2 Answers

I solved the problem by simply passing the parameter null into AudioSystem.getClip().

I don't know why this exception occured, I run this project before on Windows, and it worked... After on Linux and here, it didn't work.

like image 88
Alessandro Domeneghetti Avatar answered Dec 17 '25 00:12

Alessandro Domeneghetti


I had the same problem and found this code to work:

File soundFile = new File("/home/usr/Desktop/d.wav");
AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = soundIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
clip.open(soundIn);
clip.start();
while(clip.isRunning())
{
   Thread.yield();
}

The key is in soundIn.getFormat(). To quote the docs:

Obtains the audio format of the sound data in this audio input stream.

Source: http://ubuntuforums.org/showthread.php?t=1469572

like image 20
Nateowami Avatar answered Dec 17 '25 02:12

Nateowami



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!