Path: blob/master/src/java.desktop/share/classes/javax/sound/sampled/Clip.java
41159 views
/*1* Copyright (c) 1999, 2017, 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.IOException;2829/**30* The {@code Clip} interface represents a special kind of data line whose audio31* data can be loaded prior to playback, instead of being streamed in real time.32* <p>33* Because the data is pre-loaded and has a known length, you can set a clip to34* start playing at any position in its audio data. You can also create a loop,35* so that when the clip is played it will cycle repeatedly. Loops are specified36* with a starting and ending sample frame, along with the number of times that37* the loop should be played.38* <p>39* Clips may be obtained from a {@link Mixer} that supports lines of this type.40* Data is loaded into a clip when it is opened.41* <p>42* Playback of an audio clip may be started and stopped using the43* {@link #start start} and {@link #stop stop} methods. These methods do not44* reset the media position; {@code start} causes playback to continue from the45* position where playback was last stopped. To restart playback from the46* beginning of the clip's audio data, simply follow the invocation of47* {@code stop} with {@code setFramePosition(0)}, which rewinds the media to the48* beginning of the clip.49*50* @author Kara Kytle51* @since 1.352*/53public interface Clip extends DataLine {5455/**56* A value indicating that looping should continue indefinitely rather than57* complete after a specific number of loops.58*59* @see #loop60*/61int LOOP_CONTINUOUSLY = -1;6263/**64* Opens the clip, meaning that it should acquire any required system65* resources and become operational. The clip is opened with the format and66* audio data indicated. If this operation succeeds, the line is marked as67* open and an {@link LineEvent.Type#OPEN OPEN} event is dispatched to the68* line's listeners.69* <p>70* Invoking this method on a line which is already open is illegal and may71* result in an {@code IllegalStateException}.72* <p>73* Note that some lines, once closed, cannot be reopened. Attempts to reopen74* such a line will always result in a {@code LineUnavailableException}.75*76* @param format the format of the supplied audio data77* @param data a byte array containing audio data to load into the clip78* @param offset the point at which to start copying, expressed in79* <em>bytes</em> from the beginning of the array80* @param bufferSize the number of <em>bytes</em> of data to load into the81* clip from the array82* @throws LineUnavailableException if the line cannot be opened due to83* resource restrictions84* @throws IllegalArgumentException if the buffer size does not represent an85* integral number of sample frames, or if {@code format} is not86* fully specified or invalid87* @throws IllegalStateException if the line is already open88* @throws SecurityException if the line cannot be opened due to security89* restrictions90* @see #close91* @see #isOpen92* @see LineListener93*/94void open(AudioFormat format, byte[] data, int offset, int bufferSize)95throws LineUnavailableException;9697/**98* Opens the clip with the format and audio data present in the provided99* audio input stream. Opening a clip means that it should acquire any100* required system resources and become operational. If this operation input101* stream. If this operation succeeds, the line is marked open and an102* {@link LineEvent.Type#OPEN OPEN} event is dispatched to the line's103* listeners.104* <p>105* Invoking this method on a line which is already open is illegal and may106* result in an {@code IllegalStateException}.107* <p>108* Note that some lines, once closed, cannot be reopened. Attempts to reopen109* such a line will always result in a {@code LineUnavailableException}.110*111* @param stream an audio input stream from which audio data will be read112* into the clip113* @throws LineUnavailableException if the line cannot be opened due to114* resource restrictions115* @throws IOException if an I/O exception occurs during reading of the116* stream117* @throws IllegalArgumentException if the stream's audio format is not118* fully specified or invalid119* @throws IllegalStateException if the line is already open120* @throws SecurityException if the line cannot be opened due to security121* restrictions122* @see #close123* @see #isOpen124* @see LineListener125*/126void open(AudioInputStream stream)127throws LineUnavailableException, IOException;128129/**130* Obtains the media length in sample frames.131*132* @return the media length, expressed in sample frames, or133* {@code AudioSystem.NOT_SPECIFIED} if the line is not open134* @see AudioSystem#NOT_SPECIFIED135*/136int getFrameLength();137138/**139* Obtains the media duration in microseconds.140*141* @return the media duration, expressed in microseconds, or142* {@code AudioSystem.NOT_SPECIFIED} if the line is not open143* @see AudioSystem#NOT_SPECIFIED144*/145long getMicrosecondLength();146147/**148* Sets the media position in sample frames. The position is zero-based; the149* first frame is frame number zero. When the clip begins playing the next150* time, it will start by playing the frame at this position.151* <p>152* To obtain the current position in sample frames, use the153* {@link DataLine#getFramePosition getFramePosition} method of154* {@code DataLine}.155*156* @param frames the desired new media position, expressed in sample frames157*/158void setFramePosition(int frames);159160/**161* Sets the media position in microseconds. When the clip begins playing the162* next time, it will start at this position. The level of precision is not163* guaranteed. For example, an implementation might calculate the164* microsecond position from the current frame position and the audio sample165* frame rate. The precision in microseconds would then be limited to the166* number of microseconds per sample frame.167* <p>168* To obtain the current position in microseconds, use the169* {@link DataLine#getMicrosecondPosition getMicrosecondPosition} method of170* {@code DataLine}.171*172* @param microseconds the desired new media position, expressed in173* microseconds174*/175void setMicrosecondPosition(long microseconds);176177/**178* Sets the first and last sample frames that will be played in the loop.179* The ending point must be greater than or equal to the starting point, and180* both must fall within the size of the loaded media. A value of 0 for the181* starting point means the beginning of the loaded media. Similarly, a182* value of -1 for the ending point indicates the last frame of the media.183*184* @param start the loop's starting position, in sample frames (zero-based)185* @param end the loop's ending position, in sample frames (zero-based), or186* -1 to indicate the final frame187* @throws IllegalArgumentException if the requested loop points cannot be188* set, usually because one or both falls outside the media's189* duration or because the ending point is before the starting point190*/191void setLoopPoints(int start, int end);192193/**194* Starts looping playback from the current position. Playback will continue195* to the loop's end point, then loop back to the loop start point196* {@code count} times, and finally continue playback to the end of the197* clip.198* <p>199* If the current position when this method is invoked is greater than the200* loop end point, playback simply continues to the end of the clip without201* looping.202* <p>203* A {@code count} value of 0 indicates that any current looping should204* cease and playback should continue to the end of the clip. The behavior205* is undefined when this method is invoked with any other value during a206* loop operation.207* <p>208* If playback is stopped during looping, the current loop status is209* cleared; the behavior of subsequent loop and start requests is not210* affected by an interrupted loop operation.211*212* @param count the number of times playback should loop back from the213* loop's end position to the loop's start position, or214* {@link #LOOP_CONTINUOUSLY} to indicate that looping should215* continue until interrupted216*/217void loop(int count);218}219220221