0

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 };
}
H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
  • What encoding and format are the 2 WAVs? BTW - for all the catches, call `e.printStackTrace()`. – Andrew Thompson Jan 08 '12 at 14:25
  • @Andrew Thompson, I used an web service to convert some sounds to .wav. I do not remember what the original format was. I have 11 such files and all worked with the same source code on my previous computer. – H2ONaCl Jan 09 '12 at 03:10
  • ..and what is the output from the stack traces when opening those WAVs? – Andrew Thompson Jan 09 '12 at 03:11
  • The stacks are identical for both `swish.wav` and `drip.wav` at the point just under the declare/assign of the AudioInputStream. – H2ONaCl Jan 09 '12 at 03:35

1 Answers1

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

Community
  • 1
  • 1
H2ONaCl
  • 10,644
  • 14
  • 70
  • 114
  • I have yet another rebuilt system and this is once again broken. It hangs inside the static method AudioSystem.getLine. The flakiness of these Java audio objects is notable that it may be best to omit this kind of functionality from projects that do not absolutely require it. – H2ONaCl Sep 07 '18 at 22:06