javax.sound.sampled
Interface Clip

All Superinterfaces:
DataLine sample code for javax.sound.sampled.DataLine definition code for javax.sound.sampled.DataLine , Line sample code for javax.sound.sampled.Line definition code for javax.sound.sampled.Line

public interface Clip
extends DataLine sample code for javax.sound.sampled.DataLine definition code for javax.sound.sampled.DataLine

The Clip interface represents a special kind of data line whose audio data can be loaded prior to playback, instead of being streamed in real time.

Because the data is pre-loaded and has a known length, you can set a clip to start playing at any position in its audio data. You can also create a loop, so that when the clip is played it will cycle repeatedly. Loops are specified with a starting and ending sample frame, along with the number of times that the loop should be played.

Clips may be obtained from a Mixer sample code for javax.sound.sampled.Mixer definition code for javax.sound.sampled.Mixer that supports lines of this type. Data is loaded into a clip when it is opened.

Playback of an audio clip may be started and stopped using the start and stop methods. These methods do not reset the media position; start causes playback to continue from the position where playback was last stopped. To restart playback from the beginning of the clip's audio data, simply follow the invocation of stop sample code for javax.sound.sampled.DataLine.stop() definition code for javax.sound.sampled.DataLine.stop() with setFramePosition(0), which rewinds the media to the beginning of the clip.

Since:
1.3

Nested Class Summary
 
Nested classes/interfaces inherited from interface javax.sound.sampled.DataLine sample code for javax.sound.sampled.DataLine definition code for javax.sound.sampled.DataLine
DataLine.Info sample code for javax.sound.sampled.DataLine.Info definition code for javax.sound.sampled.DataLine.Info
 
Field Summary
static int LOOP_CONTINUOUSLY sample code for javax.sound.sampled.Clip.LOOP_CONTINUOUSLY definition code for javax.sound.sampled.Clip.LOOP_CONTINUOUSLY
          A value indicating that looping should continue indefinitely rather than complete after a specific number of loops.
 
Method Summary
 int getFrameLength sample code for javax.sound.sampled.Clip.getFrameLength() definition code for javax.sound.sampled.Clip.getFrameLength() ()
          Obtains the media length in sample frames.
 long getMicrosecondLength sample code for javax.sound.sampled.Clip.getMicrosecondLength() definition code for javax.sound.sampled.Clip.getMicrosecondLength() ()
          Obtains the media duration in microseconds
 void loop sample code for javax.sound.sampled.Clip.loop(int) definition code for javax.sound.sampled.Clip.loop(int) (int count)
          Starts looping playback from the current position.
 void open sample code for javax.sound.sampled.Clip.open(javax.sound.sampled.AudioFormat, byte[], int, int) definition code for javax.sound.sampled.Clip.open(javax.sound.sampled.AudioFormat, byte[], int, int) (AudioFormat sample code for javax.sound.sampled.AudioFormat definition code for javax.sound.sampled.AudioFormat  format, byte[] data, int offset, int bufferSize)
          Opens the clip, meaning that it should acquire any required system resources and become operational.
 void open sample code for javax.sound.sampled.Clip.open(javax.sound.sampled.AudioInputStream) definition code for javax.sound.sampled.Clip.open(javax.sound.sampled.AudioInputStream) (AudioInputStream sample code for javax.sound.sampled.AudioInputStream definition code for javax.sound.sampled.AudioInputStream  stream)
          Opens the clip with the format and audio data present in the provided audio input stream.
 void setFramePosition sample code for javax.sound.sampled.Clip.setFramePosition(int) definition code for javax.sound.sampled.Clip.setFramePosition(int) (int frames)
          Sets the media position in sample frames.
 void setLoopPoints sample code for javax.sound.sampled.Clip.setLoopPoints(int, int) definition code for javax.sound.sampled.Clip.setLoopPoints(int, int) (int start, int end)
          Sets the first and last sample frames that will be played in the loop.
 void setMicrosecondPosition sample code for javax.sound.sampled.Clip.setMicrosecondPosition(long) definition code for javax.sound.sampled.Clip.setMicrosecondPosition(long) (long microseconds)
          Sets the media position in microseconds.
 
Methods inherited from interface javax.sound.sampled.DataLine sample code for javax.sound.sampled.DataLine definition code for javax.sound.sampled.DataLine
available sample code for javax.sound.sampled.DataLine.available() definition code for javax.sound.sampled.DataLine.available() , drain sample code for javax.sound.sampled.DataLine.drain() definition code for javax.sound.sampled.DataLine.drain() , flush sample code for javax.sound.sampled.DataLine.flush() definition code for javax.sound.sampled.DataLine.flush() , getBufferSize sample code for javax.sound.sampled.DataLine.getBufferSize() definition code for javax.sound.sampled.DataLine.getBufferSize() , getFormat sample code for javax.sound.sampled.DataLine.getFormat() definition code for javax.sound.sampled.DataLine.getFormat() , getFramePosition sample code for javax.sound.sampled.DataLine.getFramePosition() definition code for javax.sound.sampled.DataLine.getFramePosition() , getLevel sample code for javax.sound.sampled.DataLine.getLevel() definition code for javax.sound.sampled.DataLine.getLevel() , getLongFramePosition sample code for javax.sound.sampled.DataLine.getLongFramePosition() definition code for javax.sound.sampled.DataLine.getLongFramePosition() , getMicrosecondPosition sample code for javax.sound.sampled.DataLine.getMicrosecondPosition() definition code for javax.sound.sampled.DataLine.getMicrosecondPosition() , isActive sample code for javax.sound.sampled.DataLine.isActive() definition code for javax.sound.sampled.DataLine.isActive() , isRunning sample code for javax.sound.sampled.DataLine.isRunning() definition code for javax.sound.sampled.DataLine.isRunning() , start sample code for javax.sound.sampled.DataLine.start() definition code for javax.sound.sampled.DataLine.start() , stop sample code for javax.sound.sampled.DataLine.stop() definition code for javax.sound.sampled.DataLine.stop()
 
Methods inherited from interface javax.sound.sampled.Line sample code for javax.sound.sampled.Line definition code for javax.sound.sampled.Line
addLineListener sample code for javax.sound.sampled.Line.addLineListener(javax.sound.sampled.LineListener) definition code for javax.sound.sampled.Line.addLineListener(javax.sound.sampled.LineListener) , close sample code for javax.sound.sampled.Line.close() definition code for javax.sound.sampled.Line.close() , getControl sample code for javax.sound.sampled.Line.getControl(javax.sound.sampled.Control.Type) definition code for javax.sound.sampled.Line.getControl(javax.sound.sampled.Control.Type) , getControls sample code for javax.sound.sampled.Line.getControls() definition code for javax.sound.sampled.Line.getControls() , getLineInfo sample code for javax.sound.sampled.Line.getLineInfo() definition code for javax.sound.sampled.Line.getLineInfo() , isControlSupported sample code for javax.sound.sampled.Line.isControlSupported(javax.sound.sampled.Control.Type) definition code for javax.sound.sampled.Line.isControlSupported(javax.sound.sampled.Control.Type) , isOpen sample code for javax.sound.sampled.Line.isOpen() definition code for javax.sound.sampled.Line.isOpen() , open sample code for javax.sound.sampled.Line.open() definition code for javax.sound.sampled.Line.open() , removeLineListener sample code for javax.sound.sampled.Line.removeLineListener(javax.sound.sampled.LineListener) definition code for javax.sound.sampled.Line.removeLineListener(javax.sound.sampled.LineListener)
 

Field Detail

LOOP_CONTINUOUSLY sample code for javax.sound.sampled.Clip.LOOP_CONTINUOUSLY

static final int LOOP_CONTINUOUSLY
A value indicating that looping should continue indefinitely rather than complete after a specific number of loops.

See Also:
loop(int) sample code for javax.sound.sampled.Clip.loop(int) definition code for javax.sound.sampled.Clip.loop(int) , Constant Field Values
Method Detail

open sample code for javax.sound.sampled.Clip.open(javax.sound.sampled.AudioFormat, byte[], int, int) definition code for javax.sound.sampled.Clip.open(javax.sound.sampled.AudioFormat, byte[], int, int)

void open(AudioFormat sample code for javax.sound.sampled.AudioFormat definition code for javax.sound.sampled.AudioFormat  format,
          byte[] data,
          int offset,
          int bufferSize)
          throws LineUnavailableException sample code for javax.sound.sampled.LineUnavailableException definition code for javax.sound.sampled.LineUnavailableException 
Opens the clip, meaning that it should acquire any required system resources and become operational. The clip is opened with the format and audio data indicated. If this operation succeeds, the line is marked as open and an OPEN sample code for javax.sound.sampled.LineEvent.Type.OPEN definition code for javax.sound.sampled.LineEvent.Type.OPEN event is dispatched to the line's listeners.

Invoking this method on a line which is already open is illegal and may result in an IllegalStateException.

Note that some lines, once closed, cannot be reopened. Attempts to reopen such a line will always result in a LineUnavailableException sample code for javax.sound.sampled.LineUnavailableException definition code for javax.sound.sampled.LineUnavailableException .

Parameters:
format - the format of the supplied audio data
data - a byte array containing audio data to load into the clip
offset - the point at which to start copying, expressed in bytes from the beginning of the array
bufferSize - the number of bytes of data to load into the clip from the array.
Throws:
LineUnavailableException sample code for javax.sound.sampled.LineUnavailableException definition code for javax.sound.sampled.LineUnavailableException - if the line cannot be opened due to resource restrictions
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the buffer size does not represent an integral number of sample frames, or if format is not fully specified or invalid
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - if the line is already open
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if the line cannot be opened due to security restrictions
See Also:
Line.close() sample code for javax.sound.sampled.Line.close() definition code for javax.sound.sampled.Line.close() , Line.isOpen() sample code for javax.sound.sampled.Line.isOpen() definition code for javax.sound.sampled.Line.isOpen() , LineListener sample code for javax.sound.sampled.LineListener definition code for javax.sound.sampled.LineListener

open sample code for javax.sound.sampled.Clip.open(javax.sound.sampled.AudioInputStream) definition code for javax.sound.sampled.Clip.open(javax.sound.sampled.AudioInputStream)

void open(AudioInputStream sample code for javax.sound.sampled.AudioInputStream definition code for javax.sound.sampled.AudioInputStream  stream)
          throws LineUnavailableException sample code for javax.sound.sampled.LineUnavailableException definition code for javax.sound.sampled.LineUnavailableException ,
                 IOException sample code for java.io.IOException definition code for java.io.IOException 
Opens the clip with the format and audio data present in the provided audio input stream. Opening a clip means that it should acquire any required system resources and become operational. If this operation input stream. If this operation succeeds, the line is marked open and an OPEN sample code for javax.sound.sampled.LineEvent.Type.OPEN definition code for javax.sound.sampled.LineEvent.Type.OPEN event is dispatched to the line's listeners.

Invoking this method on a line which is already open is illegal and may result in an IllegalStateException.

Note that some lines, once closed, cannot be reopened. Attempts to reopen such a line will always result in a LineUnavailableException sample code for javax.sound.sampled.LineUnavailableException definition code for javax.sound.sampled.LineUnavailableException .

Parameters:
stream - an audio input stream from which audio data will be read into the clip
Throws:
LineUnavailableException sample code for javax.sound.sampled.LineUnavailableException definition code for javax.sound.sampled.LineUnavailableException - if the line cannot be opened due to resource restrictions
IOException sample code for java.io.IOException definition code for java.io.IOException - if an I/O exception occurs during reading of the stream
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the stream's audio format is not fully specified or invalid
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - if the line is already open
SecurityException sample code for java.lang.SecurityException definition code for java.lang.SecurityException - if the line cannot be opened due to security restrictions
See Also:
Line.close() sample code for javax.sound.sampled.Line.close() definition code for javax.sound.sampled.Line.close() , Line.isOpen() sample code for javax.sound.sampled.Line.isOpen() definition code for javax.sound.sampled.Line.isOpen() , LineListener sample code for javax.sound.sampled.LineListener definition code for javax.sound.sampled.LineListener

getFrameLength sample code for javax.sound.sampled.Clip.getFrameLength() definition code for javax.sound.sampled.Clip.getFrameLength()

int getFrameLength()
Obtains the media length in sample frames.

Returns:
the media length, expressed in sample frames, or AudioSystem.NOT_SPECIFIED if the line is not open.
See Also:
AudioSystem.NOT_SPECIFIED sample code for javax.sound.sampled.AudioSystem.NOT_SPECIFIED definition code for javax.sound.sampled.AudioSystem.NOT_SPECIFIED

getMicrosecondLength sample code for javax.sound.sampled.Clip.getMicrosecondLength() definition code for javax.sound.sampled.Clip.getMicrosecondLength()

long getMicrosecondLength()
Obtains the media duration in microseconds

Returns:
the media duration, expressed in microseconds, or AudioSystem.NOT_SPECIFIED if the line is not open.
See Also:
AudioSystem.NOT_SPECIFIED sample code for javax.sound.sampled.AudioSystem.NOT_SPECIFIED definition code for javax.sound.sampled.AudioSystem.NOT_SPECIFIED

setFramePosition sample code for javax.sound.sampled.Clip.setFramePosition(int) definition code for javax.sound.sampled.Clip.setFramePosition(int)

void setFramePosition(int frames)
Sets the media position in sample frames. The position is zero-based; the first frame is frame number zero. When the clip begins playing the next time, it will start by playing the frame at this position.

To obtain the current position in sample frames, use the getFramePosition sample code for javax.sound.sampled.DataLine.getFramePosition() definition code for javax.sound.sampled.DataLine.getFramePosition() method of DataLine.

Parameters:
frames - the desired new media position, expressed in sample frames

setMicrosecondPosition sample code for javax.sound.sampled.Clip.setMicrosecondPosition(long) definition code for javax.sound.sampled.Clip.setMicrosecondPosition(long)

void setMicrosecondPosition(long microseconds)
Sets the media position in microseconds. When the clip begins playing the next time, it will start at this position. The level of precision is not guaranteed. For example, an implementation might calculate the microsecond position from the current frame position and the audio sample frame rate. The precision in microseconds would then be limited to the number of microseconds per sample frame.

To obtain the current position in microseconds, use the getMicrosecondPosition sample code for javax.sound.sampled.DataLine.getMicrosecondPosition() definition code for javax.sound.sampled.DataLine.getMicrosecondPosition() method of DataLine.

Parameters:
microseconds - the desired new media position, expressed in microseconds

setLoopPoints sample code for javax.sound.sampled.Clip.setLoopPoints(int, int) definition code for javax.sound.sampled.Clip.setLoopPoints(int, int)

void setLoopPoints(int start,
                   int end)
Sets the first and last sample frames that will be played in the loop. The ending point must be greater than or equal to the starting point, and both must fall within the the size of the loaded media. A value of 0 for the starting point means the beginning of the loaded media. Similarly, a value of -1 for the ending point indicates the last frame of the media.

Parameters:
start - the loop's starting position, in sample frames (zero-based)
end - the loop's ending position, in sample frames (zero-based), or -1 to indicate the final frame
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - if the requested loop points cannot be set, usually because one or both falls outside the media's duration or because the ending point is before the starting point

loop sample code for javax.sound.sampled.Clip.loop(int) definition code for javax.sound.sampled.Clip.loop(int)

void loop(int count)
Starts looping playback from the current position. Playback will continue to the loop's end point, then loop back to the loop start point count times, and finally continue playback to the end of the clip.

If the current position when this method is invoked is greater than the loop end point, playback simply continues to the end of the clip without looping.

A count value of 0 indicates that any current looping should cease and playback should continue to the end of the clip. The behavior is undefined when this method is invoked with any other value during a loop operation.

If playback is stopped during looping, the current loop status is cleared; the behavior of subsequent loop and start requests is not affected by an interrupted loop operation.

Parameters:
count - the number of times playback should loop back from the loop's end position to the loop's start position, or LOOP_CONTINUOUSLY sample code for javax.sound.sampled.Clip.LOOP_CONTINUOUSLY definition code for javax.sound.sampled.Clip.LOOP_CONTINUOUSLY to indicate that looping should continue until interrupted