java application中如何播放声音?我找到一段代码
try {
// From file
AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile"));
// From URL
stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
// Create the clip
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
// This method does not return until the audio file is completely loaded
clip.open(stream);
// Start playing
clip.start();
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (LineUnavailableException e) {
} catch (UnsupportedAudioFileException e) {
}
参见:http://www.exampledepot.com/egs/javax.sound.sampled/Load.html
把其中audiofile改成sound.wav,
http://hostname/audiofile改成file:\\E:\\sound.wav,但运行并没有播放声音
是什么问题呢?或者还有其他什么方法能实现?
谢谢!
try {
// From file
AudioInputStream stream = AudioSystem.getAudioInputStream(new File("audiofile"));
// From URL
stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits()*2,
format.getChannels(),
format.getFrameSize()*2,
format.getFrameRate(),
true); // big endian
stream = AudioSystem.getAudioInputStream(format, stream);
}
// Create the clip
DataLine.Info info = new DataLine.Info(
Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info);
// This method does not return until the audio file is completely loaded
clip.open(stream);
// Start playing
clip.start();
} catch (MalformedURLException e) {
} catch (IOException e) {
} catch (LineUnavailableException e) {
} catch (UnsupportedAudioFileException e) {
}
参见:http://www.exampledepot.com/egs/javax.sound.sampled/Load.html
把其中audiofile改成sound.wav,
http://hostname/audiofile改成file:\\E:\\sound.wav,但运行并没有播放声音
是什么问题呢?或者还有其他什么方法能实现?
谢谢!
解决方案 »
- ava的泛型,是编译时的泛型还是运行时的泛型?
- 如何在JFrame或JPane上面加PAINT方法。(急)
- 求助用poi操作Excel时合并行的时候设置的植被覆盖
- 布局问题,高手看看怎么回事。
- 请问int转char,有什么函数可以用
- [110分]求javascript实现的类似java.net.URLEncoder.encode(String s) 的方法
- JDB_ODBC连接SQL的问题,——————在线等回答
- 很急的一個問題,在線等
- 为什么命令行窗口那么快就消失了啊?我根本没法看我的程序啊!55555~~~~~
- Java集合是放对象的,那么如何放,他的原理是什么。最好有大婶能给我解释一下。求解
- java登录界面如何与主界面连接
- java 程序在网页中运行
异常信息如下:
java.io.FileNotFoundException: Global.wav (系统找不到指定的文件。)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at com.sun.media.sound.WaveFileReader.getAudioInputStream(WaveFileReader.java:205)
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:778)
at Test.main(Test.java:19)但是..为什么呢?
Thread td = new Thread() {
public void run() {
try {
// From URL
AudioInputStream stream = AudioSystem
.getAudioInputStream(new File("C:\\TEMP\\a.wav"));
// At present, ALAW and ULAW encodings must be converted
// to PCM_SIGNED before it can be played
AudioFormat format = stream.getFormat();
if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {
format = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, format
.getSampleRate(), format
.getSampleSizeInBits() * 2, format
.getChannels(),
format.getFrameSize() * 2, format
.getFrameRate(), true); // big
// endian
stream = AudioSystem
.getAudioInputStream(format, stream);
} // Create the clip
DataLine.Info info = new DataLine.Info(Clip.class, stream
.getFormat(),
((int) stream.getFrameLength() * format
.getFrameSize()));
Clip clip = (Clip) AudioSystem.getLine(info); // This method does not return until the audio file is
// completely
// loaded
clip.open(stream); // Start playing
clip.start();
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
};
td.setDaemon(false);
td.start();
}
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
去掉
下面的也去掉
Thread td = new Thread() {
public void run() {
.....
td.setDaemon(false);
td.start();
}这些信息我是为了调试加上的,你的程序如果有主线程的话,无需这些代码了
异常?
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:786)
at Test$1.run(Test.java:70)
我试过了
"C:\\TEMP\\a.wav"
谢谢你啊~另,找到了一个更简洁的办法..
try {
InputStream in = new FileInputStream("E:\\a.wav");//流文件
try {
AudioStream as = new AudioStream(in);//创建AudioStream 对象
AudioPlayer.player.start(as);//开始播放
//AudioPlayer.player.stop(as);//停止播放,本例没有设置播放时间,歌曲结束自动停止
} catch (IOException e){
e.printStackTrace();
} } catch (FileNotFoundException e) {
e.printStackTrace();
}
处理不了复杂的波形