下面列出了javax.sound.sampled.Line.Info#javax.sound.sampled.Clip 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
}
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���ŏ��ɖ߂�
}
}
private void doTest() throws AssetException, InterruptedException
{
Clip clip = Assets.load("cowbell.wav");
assertNotNull( clip );
clip.start();
Thread.sleep(1000);
clip.stop();
clip.close();
}
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���ŏ��ɖ߂�
}
}
/**
* 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();
}
}
/** 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;
}
/** 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;
}
@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);
}
/** 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;
}
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();
}
}
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;
}
}
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();
}
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();
}
/**
* {@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;
}
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());
}
}
/** 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;
}
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();
}
}
/**
* 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);
}
}
/**
* Stops the clip, if active.
*/
public void stop() {
try {
Clip clip = getClip();
if (clip == null)
return;
if (clip.isActive())
clip.stop();
} catch (LineUnavailableException e) {}
}
/**
* 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();
}
/** 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;
}
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);
}
}
}
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();
}
/** 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;
}
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���ŏ��ɖ߂�
}
}
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���ŏ��ɖ߂�
}
}
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���ŏ��ɖ߂�
}
}
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();
}
}
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;
}
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);
}