Storing a sound as an object

R

Robert Dodier

Hi,

I'd like to load a sound file, store the sound as an object, and then
play it back at some later time. What are the classes appropriate
for that scheme? I have a lot of experience programming Java, aside
from audio, so really all I need is a pointer to the right approach,
and I think I can figure out the rest.

Thanks for your help. I found a lot of stuff about Java audio
programming,
but I didn't see (or couldn't recognize) a solution to this problem.

Robert Dodier
 
E

Eric Sosman

Robert said:
Hi,

I'd like to load a sound file, store the sound as an object, and then
play it back at some later time. What are the classes appropriate
for that scheme? I have a lot of experience programming Java, aside
from audio, so really all I need is a pointer to the right approach,
and I think I can figure out the rest.

Thanks for your help. I found a lot of stuff about Java audio
programming,
but I didn't see (or couldn't recognize) a solution to this problem.

The javax.sound.sampled package looks like a promising start.
 
M

Mark Space

Robert said:
Hi,

I'd like to load a sound file, store the sound as an object, and then
play it back at some later time. What are the classes appropriate
for that scheme? I have a lot of experience programming Java, aside
from audio, so really all I need is a pointer to the right approach,
and I think I can figure out the rest.

Thanks for your help. I found a lot of stuff about Java audio
programming,
but I didn't see (or couldn't recognize) a solution to this problem.

Robert Dodier

I did some poking at the Java audio and media packages a long while
back. One obvious answer would be to store the sound a a file with a
know format (.wav, etc.) on disk then use
javax.sound.sampled.spi.AudioFileReader object to open and play back the
sound.

Sun makes a sample app that's pretty good for demonstrating their audio
stuff. I got it to read input from the microphone on my laptop (just me
speaking) and then play it back. Try doing a search for JavaSoundDemo.jar.
 
P

Peter Duniho

I'd like to load a sound file, store the sound as an object, and then
play it back at some later time. What are the classes appropriate
for that scheme? I have a lot of experience programming Java, aside
from audio, so really all I need is a pointer to the right approach,
and I think I can figure out the rest.

I just did this. You don't need the SPI packages...those are for
implementing audio services ("service provider interface"), not needed for
using them. You do need the javax.sound.sampled package.

The basic unit you need is the Clip class. You get one from the
AudioSystem class, and initialize it with an AudioInputStream, also
obtainable from the AudioSystem class using an existing InputStream (e.g..
created from a file, or from a resource in your .jar file).

Here's some sample code from my effort, followed by some notes regarding
"gotchas" I ran into:

// some variable used to store the reference to the
// Clip...put it wherever is appropriate for your needs
Clip clip;

// A stream that will read your data. In my case, I get this using
// Class.getResourceAsStream, but you can just as easily open a file
Stream streamSource;

// Then, where you want to initialize the Clip:
InputStream stream = new BufferedInputStream(streamSource);
AudioInputStream ais = AudioSystem.getAudioInputStream(stream);
clip = AudioSystem.getClip();
clip.open(ais);
ais.close();

clip.addLineListener(new LineListener()
{
public void update(LineEvent event)
{
if (event.getType() == LineEvent.Type.STOP)
{
clip.stop();
clip.setFramePosition(0);
}
}
});

// Finally, wherever you want to play the Clip:
if (clip != null && !clip.isActive())
{
clip.start();
}

Now, for the gotchas:

1) On at least one Java implementation (Mac OS), you must stop the
clip before you can start it again, even if the audio has actually reached
the end
2) On at least one Java implementation (Windows), you must also reset
the clip's position back to the beginning before playing it again (on the
Mac, the clip resets automatically if you stop it when it's reached the
end)
3) You need to use a BufferedInputStream to read the data, at least
when reading it as a resource. Without buffering, something goes wrong
and the clip can't be correctly initialized (an exception is thrown,
though I don't recall off the top of my head what exception it was).

I don't know Java well enough to know for sure whether closing the
AudioInputStream is sufficient for also closing the wrapped
BufferedInputStream and the original Stream (from the resource in my
case). I'm assuming it is, but someone might correct me on that point. :)

As you can see, the basic idea is actually very simple and not hard to
implement at all. Good luck and have fun!

Pete
 
R

Roedy Green

Thanks for your help. I found a lot of stuff about Java audio
programming,
but I didn't see (or couldn't recognize) a solution to this problem.

You could read the file and store it as GZipped ByteArrayStream
 
R

Robert Dodier

Pete,

Thanks a lot for your help. I will take a look at the Clip class.
I appreciate your detailed notes very much.

Robert Dodier
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top