Java AudioSystem .wav文件的行为差异

时间:2022-10-01 19:45:04

I have a number of .wav files I play from Java. In this stripped down example, "drip" is audible but "swish" is not even though they run via the same code. There does not seem to be anything unusual about "swish": it plays from the Gnome desktop. I have a similar app, not quite as stripped down that plays "swish" but not "drip" so again it does not seem to be a problem tied to the nature of the data.

我有很多来自Java的.wav文件。在这个精简的例子中,“滴水”是可听见的,但即使它们通过相同的代码运行,“swish”也不会。 “swish”似乎没有任何异常:它来自Gnome桌面。我有一个类似的应用程序,不像被剥离那样播放“swish”而不是“drip”所以再次它似乎不是一个与数据的性质相关的问题。

Any ideas what is happening here? This bug was noticed after code that was known to be working was "redeployed" on a rebuilt system: reinstalled Ubuntu 10.10 and Eclipse Indigo. This bug happens with both Sun JDK 6 and OpenJDK 6. Edit The "rebuilt" system is actually different hardware as well: Intel E6500 (2 cores, 2.93 GHz) I have only installed Ubuntu, Eclipse, OpenJDK, Sun JDK. The previous system was an AMD 630 (4 cores, 2.8 GHz) with Ubuntu, Eclipse, OpenJDK (I think).

有什么想法在这里发生了什么?在已知正在工作的代码在重建系统上“重新部署”后,会发现此错误:重新安装的Ubuntu 10.10和Eclipse Indigo。 Sun JDK 6和OpenJDK 6都会出现此错误。编辑“重建”系统实际上也是不同的硬件:Intel E6500(2核,2.93 GHz)我只安装了Ubuntu,Eclipse,OpenJDK和Sun JDK。之前的系统是AMD 630(4核,2.8 GHz),带有Ubuntu,Eclipse,OpenJDK(我认为)。

Edit After some experimenting, it seems the first clip loaded will be the one that works. The TestSounder constructor loads them so that is where the order can be reversed. Perhaps the use of the static method AudioSystem.getLine has something to do with it since that is obviously the non-object oriented operation here.

编辑经过一些实验,似乎加载的第一个剪辑将是有效的剪辑。 TestSounder构造函数加载它们,以便顺序可以反转。也许使用静态方法AudioSystem.getLine与它有关,因为这显然是非面向对象的操作。

import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.Clip;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.LineUnavailableException;

public class TestSounder
{
    String resourcePathFromBin = "/resources/";

    Clip dripClip;
    Clip swishClip;

    public TestSounder()
    {
        dripClip = setupClip("drip.wav");
        swishClip = setupClip("swish.wav");
    }

    public void close()
    {
        dripClip.close();
        swishClip.close();
    }

    private Clip setupClip(String fileName)
    {
        Clip clip = null;
        try
        {
            AudioInputStream ais = 
                AudioSystem.getAudioInputStream(this.getClass().getResourceAsStream(resourcePathFromBin + 
                                                                                    fileName));
            AudioFormat af = ais.getFormat();
            DataLine.Info lineInfo = new DataLine.Info(Clip.class, af);
            clip = (Clip)AudioSystem.getLine(lineInfo);
            clip.open(ais);
        }
        catch (UnsupportedAudioFileException e)
        {
            assert false: "bug";
        }
        catch (IOException e)
        {
            assert false: "bug";
        }
        catch (LineUnavailableException e)
        {
            assert false: "bug";
        }
        return clip;
    }

    public void go(UtilityK.Sounds s)
    {
        Clip clip = null;
        if (s == UtilityK.Sounds.drip)
            clip = dripClip;
        if (s == UtilityK.Sounds.swish)
            clip = swishClip;

        clip.stop();
        clip.setFramePosition(0);
        clip.start();
        //while (clip.isRunning())
        //    TestSounder.delay();
    }

    public static void main(String[] args)
    {
        TestSounder obj = new TestSounder();

        obj.go(UtilityK.Sounds.swish);
        TestSounder.delay();

        obj.go(UtilityK.Sounds.drip);
        TestSounder.delay();

        obj.go(UtilityK.Sounds.swish);
        TestSounder.delay();

        obj.go(UtilityK.Sounds.drip);
        TestSounder.delay();

        obj.close();
    }

    private static void delay()
    {
        Thread.sleep(5000);
    }
}

public interface UtilityK
{
    enum Sounds { drip, swish };
}

1 个解决方案

#1


0  

Here is an alternative approach. It seems well suited to short sounds that might need to be played often or in quick succession as user feedback.

这是另一种方法。它似乎非常适合可能需要经常播放或作为用户反馈快速连续播放的短音。

link to a way to play two kinds of beeps that come from .wav files

链接到播放来自.wav文件的两种哔声的方法

#1


0  

Here is an alternative approach. It seems well suited to short sounds that might need to be played often or in quick succession as user feedback.

这是另一种方法。它似乎非常适合可能需要经常播放或作为用户反馈快速连续播放的短音。

link to a way to play two kinds of beeps that come from .wav files

链接到播放来自.wav文件的两种哔声的方法