java中播放声音的问题(不是applet)

abcdefg007 2008-04-29 04:58:50
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,但运行并没有播放声音
是什么问题呢?

或者还有其他什么方法能实现?
谢谢!
...全文
1268 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunyujia 2008-05-02
  • 打赏
  • 举报
回复
上面那个简单的方法无法播放mp3转换成wav的文件,
处理不了复杂的波形
abcdefg007 2008-05-02
  • 打赏
  • 举报
回复
不知道为什么,我换了个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();
}
sunyujia 2008-05-01
  • 打赏
  • 举报
回复
这个你改的文件也是wav吗
我试过了
"C:\\TEMP\\a.wav"
abcdefg007 2008-05-01
  • 打赏
  • 举报
回复
to sunyujia:
异常?
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)
sunyujia 2008-05-01
  • 打赏
  • 举报
回复
在你的app中可以把
synchronized (this) {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
去掉
下面的也去掉
Thread td = new Thread() {
public void run() {
.....
td.setDaemon(false);
td.start();
}

这些信息我是为了调试加上的,你的程序如果有主线程的话,无需这些代码了
sunyujia 2008-05-01
  • 打赏
  • 举报
回复
我把线程wait();了你要根据实际环境唤醒,主要是我没找到播放wav文件的线程.
sunyujia 2008-05-01
  • 打赏
  • 举报
回复
我帮你把上面的程序改好了,但是在改的时候发现这段示例很bt,不建议楼主使用这套程序.

public static void main(String[] args) {
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();
}
abcdefg007 2008-05-01
  • 打赏
  • 举报
回复
哦~~~
异常信息如下:
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)

但是..为什么呢?
kerry_lulu 2008-04-29
  • 打赏
  • 举报
回复
有exception吗,你这样全部catch又不打印,怎么调试

62,634

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧