Error playing audio file from Java via PulseAudio on Ubuntu

一笑奈何 提交于 2019-12-30 07:21:33

问题


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


回答1:


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.




回答2:


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




回答3:


The error message says that the input file format is wrong somehow.

If you gave us more information (file format, maybe where you got it, code that you use to open the file and how you configured the audio drivers), we might be able to help.

See this question for some code that you can try: How to play .wav files with java



来源:https://stackoverflow.com/questions/18942424/error-playing-audio-file-from-java-via-pulseaudio-on-ubuntu

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!