简体   繁体   中英

Java AudioSystem .wav file discrepancy in behaviour

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.

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).

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.

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 };
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM