Playing already stored Clip File

Hey there!
I want to put sounds into my Game.

I already stored some Clip Objects and I want to play them with an external Class.

AudioInputStream wants to have an InputStream, but my Clips are already in the memory, how do I play them then? :clue:

When you say you “already stored some Clip objects”, have you already opened the source music with those clips using one of the two open() methods? That’s the only place I can recall where you would need to worry about the AudioInputStream. You seem to imply that you’ve already opened the music; if so, you no longer need to worry about the AudioInputStream.

To play your Clip just once, use the start() method.

myClip.start();

To play it several times, use the loop method. Either loop continuously:

myClip.loop(Clip.LOOP_CONTINUOUSLY);

or loop a specific number of times (eg 5):

myClip.loop(5);

You can stop the clip with stop(), and restart at the beginning with setFramePosition(0). (You could also use setFramePosition() with other values, for example to jump to the middle of a clip.)

I read an audio file, and store it in a linkedList

            AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(
                    "/Resources/Audio/Samples/" + st.nextToken()));
            Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class,
                    ais.getFormat()));
            sounds.get(soundName).add(clip);

when passing the Clip to my new Player-Class, it doesn’t play with “clip.start()”.

You still need to open the clip before you can play it. Think of it this way - there’s nothing in your code snippet that actually tells the Clip about the AIS you opened in the line above. Try something like this, where you tell that clip to open the input stream:


            AudioInputStream ais = AudioSystem.getAudioInputStream(getClass().getResourceAsStream(
                    "/Resources/Audio/Samples/" + st.nextToken()));
            Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class,
                    ais.getFormat()));
            clip.open(ais);
            sounds.get(soundName).add(clip);

Is this okay? I want to put a lot of sounds into my LinkedList and I’m afraid, that this will eat a lot of RAM…

I think so. If I recall correctly, the sound library will read from the input stream in chunks, just a little bit ahead of what’s currently playing. Then it will discard those chunks after the current position moves past that point. So by creating the clip and opening the stream, it doesn’t really cache the music, rather you’re just passing a reference to the data which will be read in small chunks when you play it.

okay thanks.
But I can play these sounds only once.
How do I reset the AudioInputStream from a Clip?
Since they’re all loaded into my LinkedList after clip.open(ais), I have no Idea how I make them reusable

The usual way to play a clip again is to stop it, set the position back to the beginning, and start it again.


clip.stop();
clip.setFramePosition(0);
clip.start();

However I usually do this with a Clip opened on a byte array, I haven’t tried it with a Clip opened on an AudioInputStream. Please give it a try, and if it doesn’t work then we can try something more complicated.

Hey, thanks for the answer, but I found another solution since resetting the framePosition doesn’t allow to play the same sound multiple times at the same time.

I converted my File into a byte[] and put it into the Object SoundEffect.
When invoking the playSound() of SoundEffect, the method opens a new AudioInputStream.

Clip data IS stored in RAM. I think there might be some confusion here with SourceDataLine playback, which reads from files.

What is SoundEffect? It is not listed as a class in Java SE7. Is it something you wrote for yourself? Glad to hear you found a solution.

True, Clips do not support concurrent playback. TinySound does, though, and has the additional benefit that all the sounds are merged/mixed into a single output. This allows compatibility on more platforms. Some Linux systems will not allow you to play two sounds at once, they only support a single output.