欢迎您到 IT09数码网首页!
您当前的位置:it09数码网资讯正文

求助:关于JAVA应用程序中引入声音的问题

放大字体  缩小字体 2018-04-09 13:02:19  阅读:8611 来源:本站原创 作者:王姬
思想者蹲着提问: 为什么用JAVA引入声音后,用按钮触发,第一次正常,再次按按钮就没有声音了? 引入声音的源代码如下:
import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Voice {
AudioStream start;

AudioStream end;

AudioStream wrong;

AudioStream play;

AudioStream timer;

AudioStream clock;

AudioStream select;

public Voice() {
try {
start = new AudioStream(new FileInputStream("voice/start.wav"));
end = new AudioStream(new FileInputStream("voice/end.wav"));
wrong = new AudioStream(new FileInputStream("voice/wrong.wav"));
play = new AudioStream(new FileInputStream("voice/play.wav"));
timer = new AudioStream(new FileInputStream("voice/timer.wav"));

} catch (Exception e) {
e.printStackTrace();
}
}

public void play(AudioStream as) {
AudioPlayer.player.start(as);
}
}

触发事件的时候调用的play方法。
请问这是怎么回事啊?谢谢指教小编推荐答案:

楼上这个流可没有被close,很简单的方式就能够证明

import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Voice {

private class MyFis extends FileInputStream{
public MyFis(String file) throws FileNotFoundException{
super(file);
}
public void close() throws IOException{
System.out.println("关闭");
super.close();

}
}

AudioStream start;

AudioStream end;

AudioStream wrong;

AudioStream play;

AudioStream timer;

AudioStream clock;

AudioStream select;


public Voice() {
try {
start = new AudioStream(new MyFis("voice/start.wav"));
end = new AudioStream(new MyFis("voice/end.wav"));
wrong = new AudioStream(new MyFis("voice/wrong.wav"));
play = new AudioStream(new MyFis("voice/play.wav"));
timer = new AudioStream(new MyFis("voice/timer.wav"));

} catch (Exception e) {
e.printStackTrace();
}
}

public void play(AudioStream as) {
AudioPlayer.player.start(as);
}
}
你无法看到"关闭"字样,所以close没有被调用!

很遗憾,这个流并不支持mark,所以是个单向流,用过之后只能丢弃,从新载入!所以楼上的方法是可行的。


======================================
回答人的补充2010-04-15 16:57

注意到AudioPlayer、AudioStream来自同一个包,我觉得产生这个问题的原因是有些包内的属性或者方法能够控制AudioPlayer不去重复播放同一个对象!

我原来以为是由于这个流不支持Mark的原因,因此我试着使用ByteArrayInputStream来代替FileInputStream,然后我利用这些标记来“重置流”,这样做我成功了。

import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;

public class Voice {
A

“如果发现本网站发布的资讯影响到您的版权,可以联系本站!同时欢迎来本站投稿!