Java Sound negative frame length

R

Randolph M. Jones

I am using the following chunk of code to load a sound file for later
playback. I am using this to load in and play some .WAV files. I
have successfully run this on a Windows 2000 machine (using JDK 1.4.2)
and a Mac OS 10.2 machine (using JDK 1.4.1). However, I've had a
couple of students run the same code, with the same audio files, on a
Windows XP Pro machine (using JDK 1.4.2). When he runs the program,
the call to audio_file.getFrameLength() is returning -1 instead of the
true number of frames in the sound sample. Can anybody shed light on
possible causes/remedies for this? The Java documentation says that
this method may return a negative result when it can't return the
actual frame length, but it doesn't say under what conditions this
would happen.

Thanks in advance,
Randy Jones

public SoundClip(String file_name)
{
AudioInputStream is;
AudioFileFormat audio_file;
int offset;
// First, try to open the requested audio file
try
{
URL resource = getClass().getResource(file_name);
audio_file = AudioSystem.getAudioFileFormat(resource);
// Remember the audio format of this file, for when it's time
to play the data
this.audio_format = audio_file.getFormat();
is = AudioSystem.getAudioInputStream(resource);
} catch (Exception e)
{
System.out.println("Could not open " + file_name + " for
audio.");
return;
}
// Unless the file information is lying to us, the amount of
data in the
// file (in bytes) should be the "frame length" of the file
times the
// "frame size" of the audio format being used.
data =
new byte[audio_file.getFrameLength() *
audio_format.getFrameSize()];
// Read the audio data until we have it all. We need this loop
because we're
// going to get pieces of the file in chunks, and there's no
telling ahead of
// time how big those chunks are going to be. So we just keep
slurping them
// up and stuffing the data into the array until the array is
full.
for (offset = 0; offset >= 0;)
{
try
{
offset = is.read(data, offset, data.length - offset);
} catch (Exception e)
{
System.out.println("Error reading audio data from " +
file_name);
return;
}
}
}
 

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,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top