/**
* Plays an audio file (in .wav, .mid, or .au format) in a background
* thread.
*
* @param filename
* the name of the audio file
* @throws IllegalArgumentException
* if unable to play {@code filename}
* @throws IllegalArgumentException
* if {@code filename} is {@code null}
*/
public static synchronized void play(final String filename) {
if (filename == null)
throw new IllegalArgumentException();
InputStream is = StdAudio.class.getResourceAsStream(filename);
if (is == null) {
throw new IllegalArgumentException("could not read '" + filename + "'");
}
// code adapted from:
// http://stackoverflow.com/questions/26305/how-can-i-play-sound-in-java
try {
// check if file format is supported
// (if not, will throw an UnsupportedAudioFileException)
@SuppressWarnings("unused")
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
new Thread(new Runnable() {
@Override
public void run() {
stream(filename);
}
}).start();
}
// let's try Applet.newAudioClip() instead
catch (UnsupportedAudioFileException e) {
playApplet(filename);
return;
}
// something else went wrong
catch (IOException ioe) {
throw new IllegalArgumentException("could not play '" + filename + "'", ioe);
}
}
StdAudio.java 文件源码
java
阅读 24
收藏 0
点赞 0
评论 0
项目:MusicToGraph
作者:
评论列表
文章目录