public class SoundReader
extends java.lang.Object
Supported file types
Valid sound file formats are WAVE (.wav), AIFF (.aif, .aiff), AU (.au). Valid sample rates are 8000, 11025, 22050, 44100, and 48000 Hz. Both 8 bit and 16 bit audio are supported. Mono and stereo files are supported.
Usage
The path to the sound file, specified as a URL, is given as a constructor parameter. The constructor also takes an array length parameter, which is explained below. The constructor will attempt to open the specified file.
After invoking the constructor, the getSamples() method should be repeatedly invoked to read samples from the specified sound file. The getSamples() method takes a multidimensional array as a parameter. The first index represents the channel number (0 for first channel, 1 for second channel, etc.). The second index represents the sample index within a channel. For each channel i, the size of the array, getSamplesArray[i].length, must be equal to the constructor parameter getSamplesArraySize. Otherwise an exception will occur. When the end of the sound file is reached, this method will return null.
The getChannels(), getSampleRate(), and getBitsPerSample() methods may be invoked at any time to obtain information about the format of the sound file.
When no more samples are desired, the closeFile() method should be invoked to close the sound file. An exception will occur if getSamples() is invoked at any point after closeFile() is invoked.
Security Issues
Applications have no restrictions on the capturing or playback of audio. Applets, however, may by default only capture audio from a file specified as a URL on the same machine as the one the applet was loaded from. Applet code is not allowed to read or write native files. The .java.policy file may be modified to grant applets more privileges.
Note: Requires Java 2 v1.3.0 or later.
SoundWriter
Red (cxh) |
Red (vogel) |
Constructor and Description |
---|
SoundReader(java.lang.String sourceURL,
int getSamplesArraySize)
Construct a sound reader object that reads audio samples
from a sound file specified as a string describing a
URL and open the file at the specified URL.
|
SoundReader(java.net.URL soundURL,
int getSamplesArraySize)
Construct a sound reader object that reads audio samples
from a sound file specified as a URL and open the file at
the specified URL.
|
Modifier and Type | Method and Description |
---|---|
void |
closeFile()
Close the file at the specified URL.
|
int |
getBitsPerSample()
Return the number of bits per audio sample.
|
int |
getChannels()
Return the number of audio channels.
|
float |
getSampleRate()
Return the sampling rate in Hz.
|
double[][] |
getSamples()
Return an array of captured audio samples.
|
public SoundReader(java.lang.String sourceURL, int getSamplesArraySize) throws java.io.IOException
sourceURL
- A string describing a URL.getSamplesArraySize
- The number of samples per channel
returned by getSamples().java.io.IOException
- If opening the sourceURL throws it,
if the file format is not supported or if there is no audio
to play.public SoundReader(java.net.URL soundURL, int getSamplesArraySize) throws java.io.IOException
soundURL
- The URL of a sound file.getSamplesArraySize
- The number of samples per channel
returned by getSamples().java.io.IOException
- If opening the sourceURL throws it,
if the file format is not supported or if there is no audio
to play.public int getChannels() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- If this method is called
before openFile() is called or after closeFile()
is called.public float getSampleRate() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- If this method is called
after closeFile()
is called.public double[][] getSamples() throws java.io.IOException, java.lang.IllegalStateException
The array size is set by the getSamplesSize parameter in the constructor.
java.io.IOException
- If there is a problem reading the audio
samples from the input file.java.lang.IllegalStateException
- If closeFile() has already
been called.public void closeFile() throws java.io.IOException
java.io.IOException
- If there is a problem closing the
file.public int getBitsPerSample() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- If this method is called
after closeFile()
is called.