当前位置: 首页>>代码示例>>Java>>正文


Java Clip.addLineListener方法代码示例

本文整理汇总了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();
	}
 
开发者ID:thahn0720,项目名称:agui_framework,代码行数:25,代码来源:AchievementSound.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:24,代码来源:IsRunningHang.java

示例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();
}
 
开发者ID:yugecin,项目名称:opsu-dance,代码行数:26,代码来源:MultiClip.java

示例4: playSound

import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/**
 * <strong><em>playSound</em></strong><br /><br />
 * 
 * &emsp;Plays a .wav audio file located in /res/audio/.<br />
 * &emsp;<em>E.g.</em> <sub>audio</sub><br /><br />
 * &emsp;File location would be: <sub>/res/audio/audio.wav</sub><br />
 * &emsp;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();
	}
}
 
开发者ID:JediBurrell,项目名称:neo,代码行数:33,代码来源:Audio.java

示例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);
}
 
开发者ID:cmholton,项目名称:QwickSound,代码行数:39,代码来源:PreloadedPlayback.java

示例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;
}
 
开发者ID:WorldGrower,项目名称:WorldGrower,代码行数:19,代码来源:Sound.java

示例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();
	}
}
 
开发者ID:GoldenGnu,项目名称:jwarframe,代码行数:20,代码来源:AudioTool.java

示例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();
  }
}
 
开发者ID:Jahani,项目名称:EnchantedForest,代码行数:32,代码来源:Sound.java

示例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();
	}
}
 
开发者ID:ELTE-Soft,项目名称:txtUML,代码行数:23,代码来源:UI.java

示例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);
    }
}
 
开发者ID:RipMeApp,项目名称:ripme,代码行数:16,代码来源:Utils.java

示例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);
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:36,代码来源:ClipLinuxCrash2.java

示例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);
	}
}
 
开发者ID:andy-goryachev,项目名称:FxEditor,代码行数:29,代码来源:ClipPlayer.java

示例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();
}
 
开发者ID:mars-sim,项目名称:mars-sim,代码行数:24,代码来源:TelegraphSound.java

示例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);
}
 
开发者ID:wandora-team,项目名称:zmpp-wandora,代码行数:13,代码来源:DefaultSoundEffect.java

示例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.");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:48,代码来源:bug6251460.java


注:本文中的javax.sound.sampled.Clip.addLineListener方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。