Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/javax/sound/sampled/Clip.java
38918 views
/*1* Copyright (c) 1999, 2003, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.sound.sampled;2627import java.io.InputStream;28import java.io.IOException;2930/**31* The <code>Clip</code> interface represents a special kind of data line whose32* audio data can be loaded prior to playback, instead of being streamed in33* real time.34* <p>35* Because the data is pre-loaded and has a known length, you can set a clip36* to start playing at any position in its audio data. You can also create a37* loop, so that when the clip is played it will cycle repeatedly. Loops are38* specified with a starting and ending sample frame, along with the number of39* times that the loop should be played.40* <p>41* Clips may be obtained from a <code>{@link Mixer}</code> that supports lines42* of this type. Data is loaded into a clip when it is opened.43* <p>44* Playback of an audio clip may be started and stopped using the <code>start</code>45* and <code>stop</code> methods. These methods do not reset the media position;46* <code>start</code> causes playback to continue from the position where playback47* was last stopped. To restart playback from the beginning of the clip's audio48* data, simply follow the invocation of <code>{@link DataLine#stop stop}</code>49* with setFramePosition(0), which rewinds the media to the beginning50* of the clip.51*52* @author Kara Kytle53* @since 1.354*/55public interface Clip extends DataLine {565758/**59* A value indicating that looping should continue indefinitely rather than60* complete after a specific number of loops.61* @see #loop62*/63public static final int LOOP_CONTINUOUSLY = -1;6465/**66* Opens the clip, meaning that it should acquire any required67* system resources and become operational. The clip is opened68* with the format and audio data indicated.69* If this operation succeeds, the line is marked as open and an70* <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched71* to the line's listeners.72* <p>73* Invoking this method on a line which is already open is illegal74* and may result in an IllegalStateException.75* <p>76* Note that some lines, once closed, cannot be reopened. Attempts77* to reopen such a line will always result in a78* <code>{@link LineUnavailableException}</code>.79*80* @param format the format of the supplied audio data81* @param data a byte array containing audio data to load into the clip82* @param offset the point at which to start copying, expressed in83* <em>bytes</em> from the beginning of the array84* @param bufferSize the number of <em>bytes</em>85* of data to load into the clip from the array.86* @throws LineUnavailableException if the line cannot be87* opened due to resource restrictions88* @throws IllegalArgumentException if the buffer size does not represent89* an integral number of sample frames,90* or if <code>format</code> is not fully specified or invalid91* @throws IllegalStateException if the line is already open92* @throws SecurityException if the line cannot be93* opened due to security restrictions94*95* @see #close96* @see #isOpen97* @see LineListener98*/99public void open(AudioFormat format, byte[] data, int offset, int bufferSize) throws LineUnavailableException;100101/**102* Opens the clip with the format and audio data present in the provided audio103* input stream. Opening a clip means that it should acquire any required104* system resources and become operational. If this operation105* input stream. If this operation106* succeeds, the line is marked open and an107* <code>{@link LineEvent.Type#OPEN OPEN}</code> event is dispatched108* to the line's listeners.109* <p>110* Invoking this method on a line which is already open is illegal111* and may result in an IllegalStateException.112* <p>113* Note that some lines, once closed, cannot be reopened. Attempts114* to reopen such a line will always result in a115* <code>{@link LineUnavailableException}</code>.116*117* @param stream an audio input stream from which audio data will be read into118* the clip119* @throws LineUnavailableException if the line cannot be120* opened due to resource restrictions121* @throws IOException if an I/O exception occurs during reading of122* the stream123* @throws IllegalArgumentException if the stream's audio format124* is not fully specified or invalid125* @throws IllegalStateException if the line is already open126* @throws SecurityException if the line cannot be127* opened due to security restrictions128*129* @see #close130* @see #isOpen131* @see LineListener132*/133public void open(AudioInputStream stream) throws LineUnavailableException, IOException;134135/**136* Obtains the media length in sample frames.137* @return the media length, expressed in sample frames,138* or <code>AudioSystem.NOT_SPECIFIED</code> if the line is not open.139* @see AudioSystem#NOT_SPECIFIED140*/141public int getFrameLength();142143/**144* Obtains the media duration in microseconds145* @return the media duration, expressed in microseconds,146* or <code>AudioSystem.NOT_SPECIFIED</code> if the line is not open.147* @see AudioSystem#NOT_SPECIFIED148*/149public long getMicrosecondLength();150151/**152* Sets the media position in sample frames. The position is zero-based;153* the first frame is frame number zero. When the clip begins playing the154* next time, it will start by playing the frame at this position.155* <p>156* To obtain the current position in sample frames, use the157* <code>{@link DataLine#getFramePosition getFramePosition}</code>158* method of <code>DataLine</code>.159*160* @param frames the desired new media position, expressed in sample frames161*/162public void setFramePosition(int frames);163164/**165* Sets the media position in microseconds. When the clip begins playing the166* next time, it will start at this position.167* The level of precision is not guaranteed. For example, an implementation168* might calculate the microsecond position from the current frame position169* and the audio sample frame rate. The precision in microseconds would170* then be limited to the number of microseconds per sample frame.171* <p>172* To obtain the current position in microseconds, use the173* <code>{@link DataLine#getMicrosecondPosition getMicrosecondPosition}</code>174* method of <code>DataLine</code>.175*176* @param microseconds the desired new media position, expressed in microseconds177*/178public void setMicrosecondPosition(long microseconds);179180/**181* Sets the first and last sample frames that will be played in182* the loop. The ending point must be greater than183* or equal to the starting point, and both must fall within the184* the size of the loaded media. A value of 0 for the starting185* point means the beginning of the loaded media. Similarly, a value of -1186* for the ending point indicates the last frame of the media.187* @param start the loop's starting position, in sample frames (zero-based)188* @param end the loop's ending position, in sample frames (zero-based), or189* -1 to indicate the final frame190* @throws IllegalArgumentException if the requested191* loop points cannot be set, usually because one or both falls outside192* the media's duration or because the ending point is193* before the starting point194*/195public void setLoopPoints(int start, int end);196197/**198* Starts looping playback from the current position. Playback will199* continue to the loop's end point, then loop back to the loop start point200* <code>count</code> times, and finally continue playback to the end of201* the clip.202* <p>203* If the current position when this method is invoked is greater than the204* loop end point, playback simply continues to the205* end of the clip without looping.206* <p>207* A <code>count</code> value of 0 indicates that any current looping should208* cease and playback should continue to the end of the clip. The behavior209* is undefined when this method is invoked with any other value during a210* loop operation.211* <p>212* If playback is stopped during looping, the current loop status is213* cleared; the behavior of subsequent loop and start requests is not214* affected by an interrupted loop operation.215*216* @param count the number of times playback should loop back from the217* loop's end position to the loop's start position, or218* <code>{@link #LOOP_CONTINUOUSLY}</code> to indicate that looping should219* continue until interrupted220*/221public void loop(int count);222}223224225