本文整理汇总了Java中javax.sound.sampled.Clip.addLineListener方法的典型用法代码示例。如果您正苦于以下问题:Java Clip.addLineListener方法的具体用法?Java Clip.addLineListener怎么用?Java Clip.addLineListener使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sound.sampled.Clip
的用法示例。
在下文中一共展示了Clip.addLineListener方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: play
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/**
* This method allows to actually play the sound provided from the
* {@link #audioInputStream}
*
* @throws LineUnavailableException
* if the {@link Clip} object can't be created
* @throws IOException
* if the audio file can't be find
*/
protected void play() throws LineUnavailableException, IOException {
// Clip clip = AudioSystem.getClip();
Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
clip.addLineListener(listener);
clip.open(audioInputStream);
try {
clip.start();
listener.waitUntilDone();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
clip.close();
}
audioInputStream.close();
}
示例2: test
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
private static void test(final AudioFormat format, final byte[] data)
throws Exception {
final Line.Info info = new DataLine.Info(Clip.class, format);
final Clip clip = (Clip) AudioSystem.getLine(info);
go = new CountDownLatch(1);
clip.addLineListener(event -> {
if (event.getType().equals(LineEvent.Type.START)) {
go.countDown();
}
});
clip.open(format, data, 0, data.length);
clip.start();
go.await();
while (clip.isRunning()) {
// This loop should not hang
}
while (clip.isActive()) {
// This loop should not hang
}
clip.close();
}
示例3: start
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/**
* Plays the clip with the specified volume.
* @param volume the volume the play at
* @param listener the line listener
* @throws LineUnavailableException if a clip object is not available or
* if the line cannot be opened due to resource restrictions
*/
public void start(float volume, LineListener listener) throws LineUnavailableException {
Clip clip = getClip();
if (clip == null)
return;
// PulseAudio does not support Master Gain
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
// set volume
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
float dB = (float) (Math.log(volume) / Math.log(10.0) * 20.0);
gainControl.setValue(dB);
}
if (listener != null)
clip.addLineListener(listener);
clip.setFramePosition(0);
clip.start();
}
示例4: playSound
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/**
* <strong><em>playSound</em></strong><br /><br />
*
*  Plays a .wav audio file located in /res/audio/.<br />
*  <em>E.g.</em> <sub>audio</sub><br /><br />
*  File location would be: <sub>/res/audio/audio.wav</sub><br />
*  and would be played automatically.
*
* @param audio - File name.
*/
public void playSound(String audio){
try{
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(
getClass().getResource("/audio/"+audio+".wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent arg0) {
if(arg0.getFramePosition()==clip.getFrameLength()){
clip.close();
}
}
});
clips.put(audio, clip);
}catch(Exception e){
e.printStackTrace();
}
}
示例5: PreloadedPlayback
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/**
* Creates a new {@code PreloadedPlayback}. PreloadedPlayback objects will
* always be created by their associated PreloadedAudio object.
* <p>
* IMPLEMENTATION NOTE: Originally, the fetching of a new {@code Line} was
* done in the {@code run} method, however testing revealed that latency is
* decreased if a {@code Line} is acquired ahead of time, here in the
* constructor.
*
* @param audio
* The {@code Audio} that created this {@code PreloadedPlayback}.
* @param audioFormat
* Specifies the particular arrangement of audio data.
* @param audioBytes
* Holds the audio data from which a {@code Clip} will be
* created.
* @param instanceID
* The {@code instanceID} of this {@code PreloadedPlayback}.
*/
protected PreloadedPlayback(Audio audio, AudioFormat audioFormat,
byte[] audioBytes, long instanceID) {
super(audio, instanceID);
DataLine.Info info = new DataLine.Info(Clip.class, audioFormat);
try {
clip = (Clip) AudioSystem.getLine(info);
clip.open(audioFormat, audioBytes, 0, audioBytes.length);
if (clip.isControlSupported(FloatControl.Type.MASTER_GAIN)) {
volCtrl = (FloatControl) clip
.getControl(FloatControl.Type.MASTER_GAIN);
} else {
logger.warning("Master-Gain control is not supported."
+ " Volume will be fixed at the default level.");
}
} catch (LineUnavailableException ex) {
ex.printStackTrace();
}
clip.addLineListener(this);
}
示例6: openClip
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
private Clip openClip(boolean closeAfterPlaying) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
AudioInputStream audioStream = AudioSystem.getAudioInputStream(new ByteArrayInputStream(audioFilePath));
DataLine.Info info = getLineInfo(audioStream);
Clip audioClip = (Clip) AudioSystem.getLine(info);
if (closeAfterPlaying) {
audioClip.addLineListener(new LineListener() {
@Override
public void update(LineEvent myLineEvent) {
if (myLineEvent.getType() == LineEvent.Type.STOP)
audioClip.close();
}
});
}
audioClip.open(audioStream);
return audioClip;
}
示例7: playIt
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
private void playIt(InputStream inputStream) throws IOException, UnsupportedAudioFileException, LineUnavailableException, InterruptedException, IllegalArgumentException {
AudioListener listener = new AudioListener();
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new BufferedInputStream(inputStream));
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(listener);
clip.open(audioInputStream);
try {
clip.start();
listener.waitUntilDone();
} finally {
clip.close();
}
} finally {
inputStream.close();
}
}
示例8: playClip
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
public static void playClip(File clipFile) throws IOException,
UnsupportedAudioFileException, LineUnavailableException, InterruptedException {
class AudioListener implements LineListener {
private boolean done = false;
@Override public synchronized void update(LineEvent event) {
Type eventType = event.getType();
if (eventType == Type.STOP || eventType == Type.CLOSE) {
done = true;
notifyAll();
}
}
public synchronized void waitUntilDone() throws InterruptedException {
while (!done) { wait(); }
}
}
AudioListener listener = new AudioListener();
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(clipFile);
try {
Clip clip = AudioSystem.getClip();
clip.addLineListener(listener);
clip.open(audioInputStream);
try {
clip.start();
listener.waitUntilDone();
} finally {
clip.close();
}
} finally {
audioInputStream.close();
}
}
示例9: playSirenSound
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
private void playSirenSound() {
try {
File soundFile = new File(sirenFile);
AudioInputStream soundIn = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = soundIn.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
soundOn = false;
}
}
});
clip.open(soundIn);
clip.start();
soundOn = true;
} catch (Exception e) {
e.printStackTrace();
}
}
示例10: playSound
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
public static void playSound(String filename) {
URL resource = ClassLoader.getSystemClassLoader().getResource(filename);
try {
final Clip clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
clip.addLineListener(event -> {
if (event.getType() == LineEvent.Type.STOP) {
clip.close();
}
});
clip.open(AudioSystem.getAudioInputStream(resource));
clip.start();
} catch (Exception e) {
logger.error("Failed to play sound " + filename, e);
}
}
示例11: start
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
public long start() throws Exception {
AudioFormat format = new AudioFormat(44100, 16, 2, true, false);
if (addLen) {
staticLen+=(int) (staticLen/5)+1000;
} else {
staticLen-=(int) (staticLen/5)+1000;
}
if (staticLen>8*44100*4) {
staticLen = 8*44100*4;
addLen=!addLen;
}
if (staticLen<1000) {
staticLen = 1000;
addLen=!addLen;
}
int len = staticLen;
len -= (len % 4);
out(" Test program: preparing to play back "+len+" bytes == "+bytes2Ms(len, format)+"ms audio...");
byte[] fakedata=new byte[len];
InputStream is = new ByteArrayInputStream(fakedata);
AudioInputStream ais = new AudioInputStream(is, format, fakedata.length/format.getFrameSize());
DataLine.Info info = new DataLine.Info(Clip.class, ais.getFormat());
clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(this);
out(" Test program: opening clip="+((clip==null)?"null":clip.toString()));
clip.open(ais);
ais.close();
out(" Test program: starting clip="+((clip==null)?"null":clip.toString()));
clip.start();
return bytes2Ms(fakedata.length, format);
}
示例12: play
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
public static void play(InputStream in)
{
try
{
AudioInputStream stream = AudioSystem.getAudioInputStream(in);
AudioFormat format = stream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
final Clip clip = (Clip)AudioSystem.getLine(info);
clip.addLineListener(new LineListener()
{
public void update(LineEvent ev)
{
LineEvent.Type t = ev.getType();
if(t == LineEvent.Type.STOP)
{
clip.close();
}
}
});
clip.open(stream);
clip.start();
}
catch(Exception e)
{
Log.ex(e);
}
}
示例13: play
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/**
* This method allows to actually play the sound provided from the
* {@link #audioInputStream}
*
* @throws LineUnavailableException
* if the {@link Clip} object can't be created
* @throws IOException
* if the audio file can't be find
*/
protected void play() throws LineUnavailableException, IOException {
final Clip clip = AudioSystem.getClip();
clip.addLineListener(listener);
clip.open(audioInputStream);
try {
clip.start();
listener.waitUntilDone();
} catch (final InterruptedException e) {
e.printStackTrace();
} finally {
clip.close();
}
audioInputStream.close();
}
示例14: DefaultSoundEffect
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/**
* Constructor.
*
* @param clip an audio clip
*/
public DefaultSoundEffect(final Clip clip) {
super();
this.clip = clip;
listeners = new ArrayList<SoundStopListener>();
clip.addLineListener(this);
}
示例15: test
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
static protected void test()
throws LineUnavailableException, InterruptedException {
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip)AudioSystem.getLine(info);
final MutableBoolean clipStoppedEvent = new MutableBoolean(false);
clip.addLineListener(new LineListener() {
@Override
public void update(LineEvent event) {
if (event.getType() == LineEvent.Type.STOP) {
synchronized (clipStoppedEvent) {
clipStoppedEvent.value = true;
clipStoppedEvent.notifyAll();
}
}
}
});
clip.open(format, soundData, 0, soundData.length);
long lengthClip = clip.getMicrosecondLength() / 1000;
log("Clip length " + lengthClip + " ms");
log("Playing...");
for (int i=1; i<=LOOP_COUNT; i++) {
long startTime = currentTimeMillis();
log(" Loop " + i);
clip.start();
synchronized (clipStoppedEvent) {
while (!clipStoppedEvent.value) {
clipStoppedEvent.wait();
}
clipStoppedEvent.value = false;
}
long endTime = currentTimeMillis();
long lengthPlayed = endTime - startTime;
if (lengthClip > lengthPlayed + 20) {
log(" ERR: Looks like sound didn't play: played " + lengthPlayed + " ms instead " + lengthClip);
countErrors++;
} else {
log(" OK: played " + lengthPlayed + " ms");
}
clip.setFramePosition(0);
}
log("Played " + LOOP_COUNT + " times, " + countErrors + " errors detected.");
}