本文整理了Java中javax.sound.sampled.Clip
类的一些代码示例,展示了Clip
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Clip
类的具体详情如下:
包路径:javax.sound.sampled.Clip
类名称:Clip
暂无
代码示例来源:origin: kevin-wayne/algs4
/**
* Loops an audio file (in .wav, .mid, or .au format) in a background thread.
*
* @param filename the name of the audio file
* @throws IllegalArgumentException if {@code filename} is {@code null}
*/
public static synchronized void loop(String filename) {
if (filename == null) throw new IllegalArgumentException();
// code adapted from: http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java
try {
Clip clip = AudioSystem.getClip();
InputStream is = StdAudio.class.getResourceAsStream(filename);
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
}
catch (UnsupportedAudioFileException e) {
throw new IllegalArgumentException("unsupported audio format: '" + filename + "'", e);
}
catch (LineUnavailableException e) {
throw new IllegalArgumentException("could not play '" + filename + "'", e);
}
catch (IOException e) {
throw new IllegalArgumentException("could not play '" + filename + "'", e);
}
}
代码示例来源:origin: RipMeApp/ripme
/**
* 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);
}
}
代码示例来源:origin: marytts/marytts
public void actionPerformed(ActionEvent e) {
synchronized (clip) {
if (clip.isActive()) {
System.err.println("Stopping clip.");
clip.stop();
} else {
System.err.println("Rewinding clip.");
if (Double.isNaN(positionCursor.x)) { // no cursor, play from start
clip.setFramePosition(0);
} else { // play from cursor position
clip.setFramePosition(X2indexX(positionCursor.x));
}
if (!Double.isNaN(rangeCursor.x)) { // range set?
System.err.println("Setting timer task");
int endFrame = X2indexX(rangeCursor.x);
timer.schedule(new ClipObserver(clip, endFrame), 50, 50);
}
System.err.println("Starting clip.");
clip.start();
}
}
}
});
代码示例来源:origin: marytts/marytts
/**
* Stop wav play back
*
*/
public static void stopWavFile() {
m_clip.stop();
m_clip.flush();
m_clip.close();
}
代码示例来源:origin: marytts/marytts
public void run() {
System.err.println("Timer task running");
if (!clip.isActive() // already stopped?
|| clip.getFramePosition() >= endFrame) {
System.err.println("Timer task stopping clip.");
clip.stop();
this.cancel();
}
}
}
代码示例来源:origin: stackoverflow.com
public static synchronized void playSound(final String url) {
new Thread(new Runnable() {
// The wrapper thread is unnecessary, unless it blocks on the
// Clip finishing; see comments.
public void run() {
try {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(
Main.class.getResourceAsStream("/path/to/sounds/" + url));
clip.open(inputStream);
clip.start();
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
}).start();
}
代码示例来源:origin: stackoverflow.com
URL url2 = new URL("http://www.villagegeek.com/downloads/webwavs/Austin_Powers_death.wav");
Clip clip2 = AudioSystem.getClip();
AudioInputStream ais2 = AudioSystem.getAudioInputStream(url2);
clip2.open(ais2);
clip2.start();
代码示例来源:origin: stackoverflow.com
URL url = new URL("foo.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(url);
clip.open(ais);
System.out.println(clip.getMicrosecondLength());
代码示例来源:origin: org.jvnet.hudson/netx
/**
* Creates new AudioClip. If the clip cannot be opened no
* exception is thrown, instead the methods of the AudioClip
* return without performing any operations.
*
* @param location the clip location
*/
public AppletAudioClip(URL location) {
try {
AudioInputStream stream = AudioSystem.getAudioInputStream(location);
clip = (Clip) AudioSystem.getLine(new Line.Info(Clip.class));
clip.open(stream);
}
catch (Exception ex) {
System.err.println("Error loading sound:"+location.toString());
clip = null;
}
}
代码示例来源:origin: stackoverflow.com
File file = new File(JavaApplication.class.getResource("1.wav").getPath());
AudioInputStream inputStream1 = AudioSystem.getAudioInputStream(file);
AudioInputStream inputStream2 = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(inputStream1);
clip.setFramePosition(0);
clip.start();
Clip clip2 = AudioSystem.getClip();
clip2.open(inputStream2);
clip2.setFramePosition(0);
clip2.start();
代码示例来源:origin: stackoverflow.com
URL defaultSound = getClass().getResource("/folder/folder/audio.wav");
File soundFile = new File(defaultSound.toURI());
System.out.println("defaultSound " + defaultSound); // check the URL!
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(defaultSound);
Clip clip = AudioSystem.getClip();
clip.open(audioInputStream);
clip.start( );
代码示例来源:origin: stackoverflow.com
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
//...
public static void main(String[] args) throws Throwable {
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(SoundManager.class.getResourceAsStream("C://temp/my.mp3"));
clip.open(inputStream);
clip.start();
}
代码示例来源:origin: stackoverflow.com
try {
File file = new File("audio.wav");
Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(file));
clip.loop(Clip.LOOP_CONTINUOUSLY);
clip.start();
} catch (Exception e) {
System.err.println("Put the music.wav file in the sound folder if you want to play background music, only optional!");
JFrame f = new JFrame();
JPanel p = new JPanel();
JLabel l = new JLabel();
l.setIcon(icon);
p.add(l);
f.getContentPane().add(p);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
audio();
代码示例来源:origin: stackoverflow.com
clip = AudioSystem.getClip();
clip.getControl( FloatControl.Type.MASTER_GAIN );
clip.loop( loopcount );
clip.setFramePosition(0);
clip.loop( Clip.LOOP_CONTINUOUSLY );
} else {
clip.stop();
throws LineUnavailableException {
if ( clip!=null ) {
clip.stop();
clip.close();
} else {
clip = AudioSystem.getClip();
AudioInputStream ais = new AudioInputStream(
new ByteArrayInputStream(b),
af,
buf.length/2 );
clip.open( ais );
} catch(Exception e) {
e.printStackTrace();
代码示例来源:origin: stackoverflow.com
JFrame f = new JFrame();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Test Sound");
f.setSize(300, 200);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(soundFile);
Clip clip = AudioSystem.getClip();
clip.open(audioIn);
clip.start();
clips.add(clip);
} catch (UnsupportedAudioFileException e) {
clips.get(id-1).stop();
return true;
代码示例来源:origin: stackoverflow.com
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(url);
clip = AudioSystem.getClip();
clip.open(audioInputStream);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
if (clip.isRunning())
clip.stop(); // Stop the player if it is still running
clip.setFramePosition(0); // rewind to the beginning
clip.start(); // Start playing
代码示例来源:origin: tomighty/tomighty
private SoundChain play(final Sound sound, boolean repeatedly, final SoundChain chain) {
stop(sound);
if(sound.disabled()) {
chain.takeOver();
return chain;
}
try {
InputStream stream = sound.inputStream();
//wrap the stream in another one that supports mark/reset (see issue #31)
stream = new BufferedInputStream(stream);
AudioInputStream input = AudioSystem.getAudioInputStream(stream);
Clip clip = AudioSystem.getClip();
clip.addLineListener(new ClipHandler(sound, chain));
clip.open(input);
if(repeatedly) {
clip.loop(Clip.LOOP_CONTINUOUSLY);
} else {
clip.start();
}
activeClips.put(sound, clip);
} catch (Exception e) {
logger.error("Error while playing sound: " + sound, e);
}
return chain;
}
代码示例来源:origin: br.com.jarch/jarch-utils
private static boolean play(final InputStream arquivo, int count) {
try (final AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(arquivo)) {
// Carrega o formato do audio e cria uma linha
final AudioFormat audioFormat = audioInputStream.getFormat();
final DataLine.Info dataLineInfo = new DataLine.Info(Clip.class,
audioInputStream.getFormat(),
(int) audioInputStream.getFrameLength() * audioFormat.getFrameSize());
// Carrega o som para o dispositivo
try (final Clip clip = (Clip) AudioSystem.getLine(dataLineInfo)) {
// Evento do LineListener
clip.addLineListener(e -> {
if (LineEvent.Type.STOP.equals(e.getType())) {
e.getLine().close();
}
});
clip.open(audioInputStream);
clip.loop(count);
}
return true;
} catch (LineUnavailableException | UnsupportedAudioFileException | IOException e) {
LogUtils.generate(e);
return false;
}
}
}
代码示例来源:origin: google/sagetv
/** constructor */
public WaveFile(String path)
{
try
{
File file=new File(path);
javax.sound.sampled.AudioFileFormat audioFileFormat=javax.sound.sampled.AudioSystem.getAudioFileFormat(file);
m_stream=javax.sound.sampled.AudioSystem.getAudioInputStream(file);
javax.sound.sampled.AudioFormat format=m_stream.getFormat();
javax.sound.sampled.DataLine.Info info=new javax.sound.sampled.DataLine.Info(javax.sound.sampled.Clip.class,format,((int)m_stream.getFrameLength()*format.getFrameSize()));
m_clip=(javax.sound.sampled.Clip)javax.sound.sampled.AudioSystem.getLine(info);
m_clip.open(m_stream);
}
catch(Exception e)
{
e.printStackTrace();
}
}
代码示例来源:origin: marytts/marytts
audioInputStream = AudioSystem.getAudioInputStream(clipFile);
} catch (Exception e) {
e.printStackTrace();
AudioFormat format = audioInputStream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
try {
m_clip = (Clip) AudioSystem.getLine(info);
m_clip.open(audioInputStream);
} catch (LineUnavailableException e) {
e.printStackTrace();
e.printStackTrace();
m_clip.loop(loop);
if (waitUntilCompleted)
m_clip.drain();
} else {
System.out.println("playWavFile<init>(): can't get data from file " + clipFile.getName());
内容来源于网络,如有侵权,请联系作者删除!