APIdia Launch Partner
Top Description Fields Constructors Methods
javax.sound.sampled

public Class AudioSystem

extends Object
Class Inheritance
Imports
java.io.File, .IOException, .InputStream, .OutputStream, java.net.URL, java.util.ArrayList, .Collections, .HashSet, .List, .Objects, .Properties, .Set, .Vector, javax.sound.sampled.spi.AudioFileReader, .AudioFileWriter, .FormatConversionProvider, .MixerProvider, com.sun.media.sound.JDK13Services

The AudioSystem class acts as the entry point to the sampled-audio system resources. This class lets you query and access the mixers that are installed on the system. AudioSystem includes a number of methods for converting audio data between different formats, and for translating between audio files and streams. It also provides a method for obtaining a Line directly from the AudioSystem without dealing explicitly with mixers.

Properties can be used to specify the default mixer for specific line types. Both system properties and a properties file are considered. The "sound.properties" properties file is read from an implementation-specific location (typically it is the conf directory in the Java installation directory). The optional "javax.sound.config.file" system property can be used to specify the properties file that will be read as the initial configuration. If a property exists both as a system property and in the properties file, the system property takes precedence. If none is specified, a suitable default is chosen among the available devices. The syntax of the properties file is specified in Properties.load. The following table lists the available property keys and which methods consider them:

Audio System Property Keys
Property Key Interface Affected Method(s)
javax.sound.sampled.Clip Clip getLine, getClip
javax.sound.sampled.Port Port getLine
javax.sound.sampled.SourceDataLine SourceDataLine getLine, getSourceDataLine
javax.sound.sampled.TargetDataLine TargetDataLine getLine, getTargetDataLine
The property value consists of the provider class name and the mixer name, separated by the hash mark ("#"). The provider class name is the fully-qualified name of a concrete mixer provider class. The mixer name is matched against the String returned by the getName method of Mixer.Info. Either the class name, or the mixer name may be omitted. If only the class name is specified, the trailing hash mark is optional.

If the provider class is specified, and it can be successfully retrieved from the installed providers, the list of Mixer.Info objects is retrieved from the provider. Otherwise, or when these mixers do not provide a subsequent match, the list is retrieved from getMixerInfo to contain all available Mixer.Info objects.

If a mixer name is specified, the resulting list of Mixer.Info objects is searched: the first one with a matching name, and whose Mixer provides the respective line interface, will be returned. If no matching Mixer.Info object is found, or the mixer name is not specified, the first mixer from the resulting list, which provides the respective line interface, will be returned.

For example, the property javax.sound.sampled.Clip with a value "com.sun.media.sound.MixerProvider#SunClip" will have the following consequences when getLine is called requesting a Clip instance: if the class com.sun.media.sound.MixerProvider exists in the list of installed mixer providers, the first Clip from the first mixer with name "SunClip" will be returned. If it cannot be found, the first Clip from the first mixer of the specified provider will be returned, regardless of name. If there is none, the first Clip from the first Mixer with name "SunClip" in the list of all mixers (as returned by getMixerInfo) will be returned, or, if not found, the first Clip of the first Mixer that can be found in the list of all mixers is returned. If that fails, too, an IllegalArgumentException is thrown.

Authors
Kara Kytle, Florian Bomers, Matthias Pfisterer, Kevin P. Smith
Since
1.3
See Also
AudioFormat, AudioInputStream, Mixer, Line, Line.Info

Field Summary

Modifier and TypeField and Description
public static final int
NOT_SPECIFIED

An integer that stands for an unknown numeric value.

Constructor Summary

AccessConstructor and Description
private
AudioSystem()

Private no-args constructor for ensuring against instantiation.

Method Summary

Modifier and TypeMethod and Description
public static AudioFileFormat

Returns:

an AudioFileFormat object describing the stream's audio file format
getAudioFileFormat
(final InputStream
the input stream from which file format information should be extracted
stream
)

Obtains the audio file format of the provided input stream.

public static AudioFileFormat

Returns:

an AudioFileFormat object describing the audio file format
getAudioFileFormat
(final URL
the URL from which file format information should be extracted
url
)

Obtains the audio file format of the specified URL.

public static AudioFileFormat

Returns:

an AudioFileFormat object describing the audio file format
getAudioFileFormat
(final File
the File from which file format information should be extracted
file
)

Obtains the audio file format of the specified File.

private static List<AudioFileReader>

Returns:

a List of AudioFileReader objects representing the installed audio file readers. If no audio file readers are available on the system, an empty List is returned.
getAudioFileReaders
()

Obtains the set of audio file readers that are currently installed on the system.

public static AudioFileFormat.Type[]

Returns:

array of unique file types. If no file types are supported, an array of length 0 is returned.
getAudioFileTypes
()

Obtains the file types for which file writing support is provided by the system.

public static AudioFileFormat.Type[]

Returns:

array of file types. If no file types are supported, an array of length 0 is returned.
getAudioFileTypes
(AudioInputStream
the audio input stream for which audio file type support is queried
stream
)

Obtains the file types that the system can write from the audio input stream specified.

private static List<AudioFileWriter>

Returns:

a List of AudioFileWriter objects representing the available audio file writers. If no audio file writers are available on the system, an empty List is returned.
getAudioFileWriters
()

Obtains the set of audio file writers that are currently installed on the system.

public static AudioInputStream

Returns:

an audio input stream of the indicated encoding
getAudioInputStream
(AudioFormat.Encoding
the desired encoding after conversion
targetEncoding
,
AudioInputStream
the stream to be converted
sourceStream
)

Obtains an audio input stream of the indicated encoding, by converting the provided audio input stream.

public static AudioInputStream

Returns:

an audio input stream of the indicated format
getAudioInputStream
(AudioFormat
the desired audio format after conversion
targetFormat
,
AudioInputStream
the stream to be converted
sourceStream
)

Obtains an audio input stream of the indicated format, by converting the provided audio input stream.

public static AudioInputStream

Returns:

an AudioInputStream object based on the audio file data contained in the input stream
getAudioInputStream
(final InputStream
the input stream from which the AudioInputStream should be constructed
stream
)

Obtains an audio input stream from the provided input stream.

public static AudioInputStream

Returns:

an AudioInputStream object based on the audio file data pointed to by the URL
getAudioInputStream
(final URL
the URL for which the AudioInputStream should be constructed
url
)

Obtains an audio input stream from the URL provided.

public static AudioInputStream

Returns:

an AudioInputStream object based on the audio file data pointed to by the File
getAudioInputStream
(final File
the File for which the AudioInputStream should be constructed
file
)

Obtains an audio input stream from the provided File.

public static Clip

Returns:

the desired clip object
getClip
()

Obtains a clip that can be used for playing back an audio file or an audio stream.

public static Clip

Returns:

a clip object from the specified mixer
getClip
(Mixer.Info
a Mixer.Info object representing the desired mixer, or null for the system default mixer
mixerInfo
)

Obtains a clip from the specified mixer that can be used for playing back an audio file or an audio stream.

private static Mixer

Returns:

a Mixer that matches the requirements, or null if no default mixer found
getDefaultMixer
(List<MixerProvider>
the installed mixer providers
providers
,
Line.Info
The requested line type TargetDataLine.class, Clip.class or Port.class
info
)

Attempts to locate and return a default Mixer that provides lines of the specified type.

private static Mixer

Returns:

A Mixer that is considered appropriate, or null if none is found
getFirstMixer
(MixerProvider
The MixerProvider to check for Mixers
provider
,
Line.Info
The type of line the returned Mixer is required to support
info
,
boolean
If true, only Mixers that support mixing are returned for line types of SourceDataLine and Clip
isMixingRequired
)

From a given MixerProvider, return the first appropriate Mixer.

private static List<FormatConversionProvider>

Returns:

an array of FormatConversionProvider objects representing the available format converters. If no format converters readers are available on the system, an array of length 0 is returned.
getFormatConversionProviders
()

Obtains the set of format converters (codecs, transcoders, etc.) that are currently installed on the system.

public static Line

Returns:

a line of the requested kind
getLine
(Line.Info
a Line.Info object describing the desired kind of line
info
)

Obtains a line that matches the description in the specified Line.Info object.

public static Mixer

Returns:

the requested mixer
getMixer
(final Mixer.Info
a Mixer.Info object representing the desired mixer, or null for the system default mixer
info
)

Obtains the requested audio mixer.

public static Mixer.Info[]

Returns:

an array of info objects for the currently installed mixers. If no mixers are available on the system, an array of length 0 is returned.
getMixerInfo
()

Obtains an array of mixer info objects that represents the set of audio mixers that are currently installed on the system.

private static List<Mixer.Info>

Returns:

a List of info objects for the currently installed mixers. If no mixers are available on the system, an empty List is returned.
getMixerInfoList
()

Like getMixerInfo, but return List.

private static List<Mixer.Info>

Returns:

a List of info objects for the currently installed mixers. If no mixers are available on the system, an empty List is returned.
getMixerInfoList
(List<MixerProvider>
The list of MixerProviders
providers
)

Like getMixerInfo, but return List.

private static List<MixerProvider>

Returns:

the list of MixerProviders currently installed on the system
getMixerProviders
()

Obtains the list of MixerProviders currently installed on the system.

private static Mixer

Returns:

A Mixer matching the requirements, or null if none is found
getNamedMixer
(String
The name of the Mixer to be returned
mixerName
,
MixerProvider
The MixerProvider to check for Mixers
provider
,
Line.Info
The type of line the returned Mixer is required to support
info
)

Return a Mixer with a given name from a given MixerProvider.

private static Mixer

Returns:

A Mixer matching the requirements, or null if none is found
getNamedMixer
(String
The name of the Mixer to be returned
mixerName
,
List<MixerProvider>
The List of MixerProviders to check for Mixers
providers
,
Line.Info
The type of line the returned Mixer is required to support
info
)

From a List of MixerProviders, return a Mixer with a given name.

private static MixerProvider

Returns:

A MixerProvider of the requested class, or null if none is found
getNamedProvider
(String
The class name of the provider to be returned
providerClassName
,
List<MixerProvider>
The list of MixerProviders that is searched
providers
)

Return a MixerProvider of a given class from the list of MixerProviders.

private static List<?>

Returns:

a List of instances of providers for the requested service. If no providers are available, a vector of length 0 will be returned.
getProviders
(Class<?>
The type of providers requested. This should be one of AudioFileReader.class, AudioFileWriter.class, FormatConversionProvider.class, MixerProvider.class, MidiDeviceProvider.class, MidiFileReader.class, MidiFileWriter.class or SoundbankReader.class.
providerClass
)

Obtains the set of services currently installed on the system using the SPI mechanism in 1.3.

public static SourceDataLine

Returns:

the desired SourceDataLine object
getSourceDataLine
(AudioFormat
an AudioFormat object specifying the supported audio format of the returned line, or null for any audio format
format
)

Obtains a source data line that can be used for playing back audio data in the format specified by the AudioFormat object.

public static SourceDataLine

Returns:

the desired SourceDataLine object
getSourceDataLine
(AudioFormat
an AudioFormat object specifying the supported audio format of the returned line, or null for any audio format
format
,
Mixer.Info
a Mixer.Info object representing the desired mixer, or null for the system default mixer
mixerinfo
)

Obtains a source data line that can be used for playing back audio data in the format specified by the AudioFormat object, provided by the mixer specified by the Mixer.Info object.

public static Line.Info[]

Returns:

an array of Line.Info objects describing source lines matching the type requested. If no matching source lines are supported, an array of length 0 is returned.
getSourceLineInfo
(Line.Info
a Line.Info object that specifies the kind of lines about which information is requested
info
)

Obtains information about all source lines of a particular type that are supported by the installed mixers.

public static TargetDataLine

Returns:

the desired TargetDataLine object
getTargetDataLine
(AudioFormat
an AudioFormat object specifying the supported audio format of the returned line, or null for any audio format
format
)

Obtains a target data line that can be used for recording audio data in the format specified by the AudioFormat object.

public static TargetDataLine

Returns:

the desired TargetDataLine object
getTargetDataLine
(AudioFormat
an AudioFormat object specifying the supported audio format of the returned line, or null for any audio format
format
,
Mixer.Info
a Mixer.Info object representing the desired mixer, or null for the system default mixer
mixerinfo
)

Obtains a target data line that can be used for recording audio data in the format specified by the AudioFormat object, provided by the mixer specified by the Mixer.Info object.

public static AudioFormat.Encoding[]

Returns:

array of encodings. If sourceEncoding is not supported, an array of length 0 is returned. Otherwise, the array will have a length of at least 1, representing sourceEncoding (no conversion).
getTargetEncodings
(AudioFormat.Encoding
the encoding for which conversion support is queried
sourceEncoding
)

Obtains the encodings that the system can obtain from an audio input stream with the specified encoding using the set of installed format converters.

public static AudioFormat.Encoding[]

Returns:

array of encodings. If sourceFormatis not supported, an array of length 0 is returned. Otherwise, the array will have a length of at least 1, representing the encoding of sourceFormat (no conversion).
getTargetEncodings
(AudioFormat
the audio format for which conversion is queried
sourceFormat
)

Obtains the encodings that the system can obtain from an audio input stream with the specified format using the set of installed format converters.

public static AudioFormat[]

Returns:

array of formats. If no formats of the specified encoding are supported, an array of length 0 is returned.
getTargetFormats
(AudioFormat.Encoding
the desired encoding after conversion
targetEncoding
,
AudioFormat
the audio format before conversion
sourceFormat
)

Obtains the formats that have a particular encoding and that the system can obtain from a stream of the specified format using the set of installed format converters.

public static Line.Info[]

Returns:

an array of Line.Info objects describing target lines matching the type requested. If no matching target lines are supported, an array of length 0 is returned.
getTargetLineInfo
(Line.Info
a Line.Info object that specifies the kind of lines about which information is requested
info
)

Obtains information about all target lines of a particular type that are supported by the installed mixers.

private static boolean

Returns:

true if the mixer is considered appropriate according to the rules given above, false otherwise
isAppropriateMixer
(Mixer
The mixer to check
mixer
,
Line.Info
The line to check
lineInfo
,
boolean
Is the mixing required or not
isMixingRequired
)

Checks if a Mixer is appropriate.

public static boolean

Returns:

true if the conversion is supported, otherwise false
isConversionSupported
(AudioFormat.Encoding
the desired encoding after conversion
targetEncoding
,
AudioFormat
the audio format before conversion
sourceFormat
)

Indicates whether an audio input stream of the specified encoding can be obtained from an audio input stream that has the specified format.

public static boolean

Returns:

true if the conversion is supported, otherwise false
isConversionSupported
(AudioFormat
the desired audio format after conversion
targetFormat
,
AudioFormat
the audio format before conversion
sourceFormat
)

Indicates whether an audio input stream of a specified format can be obtained from an audio input stream of another specified format.

public static boolean

Returns:

true if the file type is supported, otherwise false
isFileTypeSupported
(AudioFileFormat.Type
the file type for which write capabilities are queried
fileType
)

Indicates whether file writing support for the specified file type is provided by the system.

public static boolean

Returns:

true if the file type is supported for this audio input stream, otherwise false
isFileTypeSupported
(AudioFileFormat.Type
the file type for which write capabilities are queried
fileType
,
AudioInputStream
the stream for which file-writing support is queried
stream
)

Indicates whether an audio file of the specified file type can be written from the indicated audio input stream.

public static boolean

Returns:

true if at least one matching line is supported, otherwise false
isLineSupported
(Line.Info
a Line.Info object describing the line for which support is queried
info
)

Indicates whether the system supports any lines that match the specified Line.Info object.

public static int

Returns:

the number of bytes written to the output stream
write
(final AudioInputStream
the audio input stream containing audio data to be written to the file
stream
,
final AudioFileFormat.Type
the kind of audio file to write
fileType
,
final OutputStream
the stream to which the file data should be written
out
)

Writes a stream of bytes representing an audio file of the specified file type to the output stream provided.

public static int

Returns:

the number of bytes written to the file
write
(final AudioInputStream
the audio input stream containing audio data to be written to the file
stream
,
final AudioFileFormat.Type
the kind of audio file to write
fileType
,
final File
the external file to which the file data should be written
out
)

Writes a stream of bytes representing an audio file of the specified file type to the external file provided.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait