本文整理汇总了Java中javax.sound.sampled.Clip.start方法的典型用法代码示例。如果您正苦于以下问题:Java Clip.start方法的具体用法?Java Clip.start怎么用?Java Clip.start使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.sound.sampled.Clip
的用法示例。
在下文中一共展示了Clip.start方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: run
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/**
* The sound is load and play in a thread no slow down the engine.
* */
@Override
public void run() {
try {
InputStream in = new BufferedInputStream(this.getClass().getResourceAsStream(this.filename + ".wav"));
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(in));
if (this.loop){
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
clip.start();
}catch (Exception e){
System.err.println(e);
}
}
示例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: playSound
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
private synchronized void playSound(final String audioFileName) {
if(isSoundEnabled) {
try {
Clip clip = AudioSystem.getClip();
InputStream inputStream = MainWindow.class.getResourceAsStream(audioFileName);
if(inputStream != null) {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(inputStream);
clip.open(audioInputStream);
clip.start();
}
else {
System.out.println("Input stream not valid");
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
示例4: 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();
}
示例5: playButtonClickNormalSound
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
/** Play the audio clip when regular button is clicked. */
public void playButtonClickNormalSound()
{
try
{
String soundName = "regular_button_click_sound.wav";
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(soundName).getAbsoluteFile());
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.setFramePosition(0);
clip.start();
}
catch (Exception e)
{
}
}
示例6: play
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
private void play(String wavPath, float db) {
if (!dnd && db > -40) {
ClassLoader classLoader = getClass().getClassLoader();
try (AudioInputStream stream = AudioSystem.getAudioInputStream(classLoader.getResource(wavPath))) {
Clip clip = AudioSystem.getClip();
clip.open(stream);
if (db != 0.0) {
FloatControl gainControl =
(FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(db);
}
clip.start();
} catch (Exception e) {
logger.error("Cannot start playing wav file: ", e);
}
}
}
示例7: 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();
}
}
示例8: main
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
public static void main(String[] av) {
if (av.length == 0)
main(defSounds);
else for (String a : av) {
System.out.println("Playing " + a);
try {
URL snd = AudioPlay.class.getResource(a);
if (snd == null) {
System.err.println("Cannot getResource " + a);
continue;
}
AudioInputStream audioInputStream =
AudioSystem.getAudioInputStream(snd);
final Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start();
} catch (Exception e) {
System.err.println(e);
}
}
}
示例9: play
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
private void play()
{
byte[] out = new byte[HEADER_SIZE + 2 * samples.length];
for (int i = 0; i < HEADER_SIZE; i++)
{
out[i] = header[i];
}
for (int i = 0; i < samples.length; i++)
{
int value = samples[i];
if (value < 0) { value = value + 65536; }
out[HEADER_SIZE + 2 * i] = (byte)(value % 256);
out[HEADER_SIZE + 2 * i + 1] = (byte)(value / 256);
}
try
{
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new ByteArrayInputStream(out)));
clip.start();
}
catch (Exception ex)
{
error(ex.getMessage());
}
}
示例10: 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();
}
}
示例11: run
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
@Override
public void run() {
AudioInputStream audioInputStream;
try {
audioInputStream = AudioSystem.getAudioInputStream(clickUrl);
DataLine.Info info = new DataLine.Info(Clip.class, audioInputStream.getFormat());
Clip clip = (Clip) AudioSystem.getLine(info);
if (clip.isRunning()) {
clip.close();
}
clip.open(audioInputStream);
clip.start();
} catch (Exception e) {
e.printStackTrace();
Main.myErr(Arrays.toString(e.getStackTrace()).replace(",", "\n"));
}
}
示例12: playMainMusic
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
public void playMainMusic() {
try {
clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class));
clip.open(AudioSystem.getAudioInputStream(new File("sounds/main.wav")));
FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
gainControl.setValue(-20.0f); // Muziek moet niet boven soundeffects uitkomen, dus 20dB zachter
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
catch (Exception exc) {
exc.printStackTrace(System.out);
}
}
示例13: playAudioFile
import javax.sound.sampled.Clip; //导入方法依赖的package包/类
private Clip playAudioFile(URL soundURL) throws Exception
{
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(soundURL);
BufferedInputStream bufferedInputStream = new BufferedInputStream(audioInputStream);
AudioFormat af = audioInputStream.getFormat();
int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
byte[] audio = new byte[size];
DataLine.Info info = new DataLine.Info(Clip.class, af, size);
bufferedInputStream.read(audio, 0, size);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(af, audio, 0, size);
clip.start();
bufferedInputStream.close();
return clip;
}
示例14: 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();
}
}
示例15: 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();
}
}