javax.sound.sampled.Line.Info#javax.sound.sampled.Clip源码实例Demo

下面列出了javax.sound.sampled.Line.Info#javax.sound.sampled.Clip 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: plugins   文件: MetronomePlugin.java
private Clip GetAudioClip(String path)
{
	File audioFile = new File(path);
	if (!audioFile.exists())
	{
		return null;
	}

	try (AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile))
	{
		Clip audioClip = AudioSystem.getClip();
		audioClip.open(audioStream);
		FloatControl gainControl = (FloatControl) audioClip.getControl(FloatControl.Type.MASTER_GAIN);
		float gainValue = (((float) config.volume()) * 40f / 100f) - 35f;
		gainControl.setValue(gainValue);

		return audioClip;
	}
	catch (IOException | LineUnavailableException | UnsupportedAudioFileException e)
	{
		log.warn("Error opening audiostream from " + audioFile, e);
		return null;
	}
}
 
源代码2 项目: javagame   文件: WaveEngine.java
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
源代码3 项目: Azzet   文件: TestAudio.java
private void doTest() throws AssetException, InterruptedException
{
	Clip clip = Assets.load("cowbell.wav");

	assertNotNull( clip );
	
	clip.start();

	Thread.sleep(1000);
	
	clip.stop();
	clip.close();
}
 
源代码4 项目: javagame   文件: WaveEngine.java
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
源代码5 项目: nullpomino   文件: WaveEngine.java
/**
 * Playback
 * @param name Registered name
 */
public void play(String name) {
	Clip clip = clipMap.get(name);

	if(clip != null) {
		// Stop
		clip.stop();
		// Playback position back to the beginning
		clip.setFramePosition(0);
		// Playback
		clip.start();
	}
}
 
源代码6 项目: dragonwell8_jdk   文件: JDK13Services.java
/** Obtain the value of a default provider property.
    @param typeClass The type of the default provider property. This
    should be one of Receiver.class, Transmitter.class, Sequencer.class,
    Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
    Clip.class or Port.class.
    @return The complete value of the property, if available.
    If the property is not set, null is returned.
 */
private static synchronized String getDefaultProvider(Class typeClass) {
    if (!SourceDataLine.class.equals(typeClass)
            && !TargetDataLine.class.equals(typeClass)
            && !Clip.class.equals(typeClass)
            && !Port.class.equals(typeClass)
            && !Receiver.class.equals(typeClass)
            && !Transmitter.class.equals(typeClass)
            && !Synthesizer.class.equals(typeClass)
            && !Sequencer.class.equals(typeClass)) {
        return null;
    }
    String name = typeClass.getName();
    String value = AccessController.doPrivileged(
            (PrivilegedAction<String>) () -> System.getProperty(name));
    if (value == null) {
        value = getProperties().getProperty(name);
    }
    if ("".equals(value)) {
        value = null;
    }
    return value;
}
 
源代码7 项目: TencentKona-8   文件: JDK13Services.java
/** Obtain the value of a default provider property.
    @param typeClass The type of the default provider property. This
    should be one of Receiver.class, Transmitter.class, Sequencer.class,
    Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
    Clip.class or Port.class.
    @return The complete value of the property, if available.
    If the property is not set, null is returned.
 */
private static synchronized String getDefaultProvider(Class typeClass) {
    if (!SourceDataLine.class.equals(typeClass)
            && !TargetDataLine.class.equals(typeClass)
            && !Clip.class.equals(typeClass)
            && !Port.class.equals(typeClass)
            && !Receiver.class.equals(typeClass)
            && !Transmitter.class.equals(typeClass)
            && !Synthesizer.class.equals(typeClass)
            && !Sequencer.class.equals(typeClass)) {
        return null;
    }
    String name = typeClass.getName();
    String value = AccessController.doPrivileged(
            (PrivilegedAction<String>) () -> System.getProperty(name));
    if (value == null) {
        value = getProperties().getProperty(name);
    }
    if ("".equals(value)) {
        value = null;
    }
    return value;
}
 
源代码8 项目: salty-engine   文件: InnerResource.java
@Override
public Clip getAudioResource(final String relativePath) {

    AudioInputStream audioInput = null;

    final String arrangedPath = arrangePath(relativePath);

    try {

        final InputStream inputStream = classLoader.getResourceAsStream(arrangedPath);
        final InputStream bufferedIn = new BufferedInputStream(inputStream);
        audioInput = AudioSystem.getAudioInputStream(bufferedIn);
    } catch (final IOException | UnsupportedAudioFileException e) {
        e.printStackTrace();
    }

    return Resource.createClip(audioInput);
}
 
源代码9 项目: hottub   文件: JDK13Services.java
/** Obtain the value of a default provider property.
    @param typeClass The type of the default provider property. This
    should be one of Receiver.class, Transmitter.class, Sequencer.class,
    Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
    Clip.class or Port.class.
    @return The complete value of the property, if available.
    If the property is not set, null is returned.
 */
private static synchronized String getDefaultProvider(Class typeClass) {
    if (!SourceDataLine.class.equals(typeClass)
            && !TargetDataLine.class.equals(typeClass)
            && !Clip.class.equals(typeClass)
            && !Port.class.equals(typeClass)
            && !Receiver.class.equals(typeClass)
            && !Transmitter.class.equals(typeClass)
            && !Synthesizer.class.equals(typeClass)
            && !Sequencer.class.equals(typeClass)) {
        return null;
    }
    String name = typeClass.getName();
    String value = AccessController.doPrivileged(
            (PrivilegedAction<String>) () -> System.getProperty(name));
    if (value == null) {
        value = getProperties().getProperty(name);
    }
    if ("".equals(value)) {
        value = null;
    }
    return value;
}
 
源代码10 项目: txtUML   文件: UI.java
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 项目: MyBox   文件: SoundTools.java
public static FloatControl getControl(AudioInputStream in) {
    try {

        AudioFormat baseFormat = in.getFormat();
        AudioFormat decodedFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(),
                16,
                baseFormat.getChannels(),
                baseFormat.getChannels() * 2,
                baseFormat.getSampleRate(),
                false);
        AudioInputStream ain = AudioSystem.getAudioInputStream(decodedFormat, in);
        DataLine.Info info = new DataLine.Info(Clip.class, decodedFormat);
        try ( Clip clip = (Clip) AudioSystem.getLine(info)) {
            clip.open(ain);
            FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
            return gainControl;
        }
    } catch (Exception e) {
        logger.debug(e.toString());
        return null;
    }
}
 
源代码12 项目: MyBox   文件: FxmlControl.java
public static void playClip(final String file, final String userFile) {
    Task miaoTask = new Task<Void>() {
        @Override
        protected Void call() {
            try {
                File sound = FxmlControl.getInternalFile(file, "sound", userFile);
                FloatControl control = SoundTools.getControl(sound);
                Clip player = SoundTools.playback(sound, control.getMaximum() * 0.6f);
                player.start();
            } catch (Exception e) {
            }
            return null;
        }
    };
    Thread thread = new Thread(miaoTask);
    thread.setDaemon(true);
    thread.start();
}
 
源代码13 项目: MyBox   文件: FxmlControl.java
public static void playClip(final File file) {
    Task miaoTask = new Task<Void>() {
        @Override
        protected Void call() {
            try {
                FloatControl control = SoundTools.getControl(file);
                Clip player = SoundTools.playback(file, control.getMaximum() * 0.6f);
                player.start();
            } catch (Exception e) {
            }
            return null;
        }
    };
    Thread thread = new Thread(miaoTask);
    thread.setDaemon(true);
    thread.start();
}
 
源代码14 项目: petscii-bbs   文件: BlorbSounds.java
/**
 * {@inheritDoc}
 */
protected boolean putToDatabase(final Chunk chunk, final int resnum) {

  final InputStream aiffStream =
    new  MemoryAccessInputStream(chunk.getMemoryAccess(), 0,
        chunk.getSize() + Chunk.CHUNK_HEADER_LENGTH);
  try {

    final AudioFileFormat aiffFormat =
      AudioSystem.getAudioFileFormat(aiffStream);
    final AudioInputStream stream = new AudioInputStream(aiffStream,
      aiffFormat.getFormat(), (long) chunk.getSize());
    final Clip clip = AudioSystem.getClip();
    clip.open(stream);      
    sounds.put(resnum, new DefaultSoundEffect(clip));
    return true;

  } catch (Exception ex) {

    ex.printStackTrace();
  }
  return false;
}
 
源代码15 项目: Robot-Overlord-App   文件: SoundSystem.java
static public void playSound(String url) {
	if (url.isEmpty()) return;

	try {
		Clip clip = AudioSystem.getClip();
		BufferedInputStream x = new BufferedInputStream(new FileInputStream(url));
		AudioInputStream inputStream = AudioSystem.getAudioInputStream(x);
		clip.open(inputStream);
		clip.start();
	} catch (Exception e) {
		e.printStackTrace();
		System.err.println(e.getMessage());
	}
}
 
源代码16 项目: openjdk-jdk8u   文件: JDK13Services.java
/** Obtain the value of a default provider property.
    @param typeClass The type of the default provider property. This
    should be one of Receiver.class, Transmitter.class, Sequencer.class,
    Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
    Clip.class or Port.class.
    @return The complete value of the property, if available.
    If the property is not set, null is returned.
 */
private static synchronized String getDefaultProvider(Class typeClass) {
    if (!SourceDataLine.class.equals(typeClass)
            && !TargetDataLine.class.equals(typeClass)
            && !Clip.class.equals(typeClass)
            && !Port.class.equals(typeClass)
            && !Receiver.class.equals(typeClass)
            && !Transmitter.class.equals(typeClass)
            && !Synthesizer.class.equals(typeClass)
            && !Sequencer.class.equals(typeClass)) {
        return null;
    }
    String name = typeClass.getName();
    String value = AccessController.doPrivileged(
            (PrivilegedAction<String>) () -> System.getProperty(name));
    if (value == null) {
        value = getProperties().getProperty(name);
    }
    if ("".equals(value)) {
        value = null;
    }
    return value;
}
 
源代码17 项目: java-swing-tips   文件: MainPanel.java
protected void loadAndPlayAudio(String path) {
  try (AudioInputStream sound = AudioSystem.getAudioInputStream(getClass().getResource(path));
       Clip clip = (Clip) AudioSystem.getLine(new DataLine.Info(Clip.class, sound.getFormat()))) {
    SecondaryLoop loop = Toolkit.getDefaultToolkit().getSystemEventQueue().createSecondaryLoop();
    clip.addLineListener(e -> {
      LineEvent.Type t = e.getType();
      if (Objects.equals(t, LineEvent.Type.STOP) || Objects.equals(t, LineEvent.Type.CLOSE)) {
        loop.exit();
      }
    });
    clip.open(sound);
    clip.start();
    loop.enter();
  } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
    ex.printStackTrace();
    Toolkit.getDefaultToolkit().beep();
  }
}
 
源代码18 项目: ripme   文件: Utils.java
/**
 * Plays a sound from a file.
 *
 * @param filename Path to the sound file
 */
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);
    }
}
 
源代码19 项目: opsu   文件: MultiClip.java
/**
 * Stops the clip, if active.
 */
public void stop() {
	try {
		Clip clip = getClip();
		if (clip == null)
			return;

		if (clip.isActive())
			clip.stop();
	} catch (LineUnavailableException e) {}
}
 
源代码20 项目: opsu-dance   文件: MultiClip.java
/**
 * 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();
}
 
源代码21 项目: openjdk-jdk8u-backup   文件: JDK13Services.java
/** Obtain the value of a default provider property.
    @param typeClass The type of the default provider property. This
    should be one of Receiver.class, Transmitter.class, Sequencer.class,
    Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
    Clip.class or Port.class.
    @return The complete value of the property, if available.
    If the property is not set, null is returned.
 */
private static synchronized String getDefaultProvider(Class typeClass) {
    if (!SourceDataLine.class.equals(typeClass)
            && !TargetDataLine.class.equals(typeClass)
            && !Clip.class.equals(typeClass)
            && !Port.class.equals(typeClass)
            && !Receiver.class.equals(typeClass)
            && !Transmitter.class.equals(typeClass)
            && !Synthesizer.class.equals(typeClass)
            && !Sequencer.class.equals(typeClass)) {
        return null;
    }
    String name = typeClass.getName();
    String value = AccessController.doPrivileged(
            (PrivilegedAction<String>) () -> System.getProperty(name));
    if (value == null) {
        value = getProperties().getProperty(name);
    }
    if ("".equals(value)) {
        value = null;
    }
    return value;
}
 
源代码22 项目: MercuryTrade   文件: SoundNotifier.java
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);
        }
    }
}
 
源代码23 项目: openjdk-jdk9   文件: IsRunningHang.java
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();
}
 
源代码24 项目: jdk8u-dev-jdk   文件: JDK13Services.java
/** Obtain the value of a default provider property.
    @param typeClass The type of the default provider property. This
    should be one of Receiver.class, Transmitter.class, Sequencer.class,
    Synthesizer.class, SourceDataLine.class, TargetDataLine.class,
    Clip.class or Port.class.
    @return The complete value of the property, if available.
    If the property is not set, null is returned.
 */
private static synchronized String getDefaultProvider(Class typeClass) {
    if (!SourceDataLine.class.equals(typeClass)
            && !TargetDataLine.class.equals(typeClass)
            && !Clip.class.equals(typeClass)
            && !Port.class.equals(typeClass)
            && !Receiver.class.equals(typeClass)
            && !Transmitter.class.equals(typeClass)
            && !Synthesizer.class.equals(typeClass)
            && !Sequencer.class.equals(typeClass)) {
        return null;
    }
    String name = typeClass.getName();
    String value = AccessController.doPrivileged(
            (PrivilegedAction<String>) () -> System.getProperty(name));
    if (value == null) {
        value = getProperties().getProperty(name);
    }
    if ("".equals(value)) {
        value = null;
    }
    return value;
}
 
源代码25 项目: javagame   文件: WaveEngine.java
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
源代码26 项目: javagame   文件: WaveEngine.java
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
源代码27 项目: javagame   文件: WaveEngine.java
public void update(LineEvent event) {
    // �X�g�b�v���Ō�܂ōĐ����ꂽ�ꍇ
    if (event.getType() == LineEvent.Type.STOP) {
        Clip clip = (Clip) event.getSource();
        clip.stop();
        clip.setFramePosition(0); // �Đ��ʒu���ŏ��ɖ߂�
    }
}
 
源代码28 项目: The-5zig-Mod   文件: AudioChatLine.java
private void loadClip() {
	clipLoaded = true;
	try {
		AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(new File(The5zigMod.getModDirectory(),
				"media/" + The5zigMod.getDataManager().getUniqueId().toString() + "/" + ((ConversationChat) getMessage().getConversation()).getFriendUUID().toString() + "/" +
						getAudioData().getHash()));
		clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
		clip.open(audioInputStream);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
源代码29 项目: dragonwell8_jdk   文件: SoftMixingMixer.java
public int getMaxLines(Line.Info info) {
    if (info.getLineClass() == SourceDataLine.class)
        return AudioSystem.NOT_SPECIFIED;
    if (info.getLineClass() == Clip.class)
        return AudioSystem.NOT_SPECIFIED;
    return 0;
}
 
源代码30 项目: jdk8u_jdk   文件: SoftMixingMixer.java
public Line getLine(Line.Info info) throws LineUnavailableException {

        if (!isLineSupported(info))
            throw new IllegalArgumentException("Line unsupported: " + info);

        if ((info.getLineClass() == SourceDataLine.class)) {
            return new SoftMixingSourceDataLine(this, (DataLine.Info) info);
        }
        if ((info.getLineClass() == Clip.class)) {
            return new SoftMixingClip(this, (DataLine.Info) info);
        }

        throw new IllegalArgumentException("Line unsupported: " + info);
    }