在Java中使用AudioPlayer [英] Working with AudioPlayer in Java
问题描述
在我的swing应用程序中,我使用一个类来点击鼠标时发出声音。我遇到的问题是,当我打电话给班级时,声音会被播放一次,当突然看到另一个按钮时,它就不会播放声音。我尝试延迟我的代码,但仍然无法正常工作。它与线程有关吗?我不擅长线程所以请告诉我如何做到这一点。我使用的代码如下,
In my swing application i am using a class to play a sound when the mouse is clicked. The problem i have is when i call the class the sound is played onetime and when suddenly another button is clicked it doesn't play the sound. I tried giving a delay in my code but stilll id doesn't work as i expected. Is it something to do with threads ?? I am not good at threads so please tell me how to do that. The code i am using is as below,
package utilities;
import java.applet.AudioClip;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
import sun.audio.ContinuousAudioDataStream;
public class Tone {`
File wavFile = new File("sounds/Windows Default.wav");
URL urlClick = Tone.class.getResource("/sounds/WindowsDefault.wav");
AudioClip sound;
public void sound() {
AudioStream as = null;
try {
InputStream in = this.getClass().getResourceAsStream("/sounds/WindowsDefault.wav");
as = new AudioStream(in);
AudioData data = as.getData();
// Create ContinuousAudioDataStream.
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);
//System.out.println(as.getLength());
AudioPlayer.player.start(cas);
//System.out.println(urlClick);
//sound = Applet.newAudioClip(urlClick);
//this.wait(1000);
for(int i =0;i<100000;i++){
double k = Math.pow(i, 5);
if(i==99999){
AudioPlayer.player.stop(cas);
return;
}
}
// sound.play();
String relativeDirectory = System.getProperty("user.dir");
System.out.println(relativeDirectory);
} catch (IOException ex) {
Logger.getLogger(Tone.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
as.close();
} catch (IOException ex) {
Logger.getLogger(Tone.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String[] args) {
Tone tone = new Tone();
tone.sound();
System.out.println("done");
}
}
- 编辑 -
我使用continuousaudiostream的原因是因为当我使用此aleast听到一些声音时声音根本没有发生。即使我使用AudioStream问题就在那里..
--EDIT-- The reason why i used the continuousaudiostream is because the sound was not happening at all when i use this aleast hear some sound. Even when i used AudioStream the problem was there..
推荐答案
1)最好不要使用隐藏的类/ api - 因为你已经有了阅读你在其他问题中得到的链接: - )
1) Better not use hidden classes/api - as you already have read in the link you got in your other question :-)
2)在开始/重新打开之前停止/关闭已经运行的声音,在公共场合像:
2) stop/close an already running sound before starting/re-open again, in public api something like:
Clip clip;
private void doPlay(final String url) {
try {
stopPlay();
AudioInputStream inputStream = AudioSystem
.getAudioInputStream(getClass().getResourceAsStream(url));
clip = AudioSystem.getClip();
clip.open(inputStream);
clip.start();
} catch (Exception e) {
stopPlay();
System.err.println(e.getMessage());
}
}
private void stopPlay() {
if (clip != null) {
clip.stop();
clip.close();
clip = null;
}
}
(注意:完全关闭仅供参考,请完善加载一次然后再次停止/启动的逻辑)
(Note: completely closed for illustration only, refine the logic to load once and then stop/start again)
这篇关于在Java中使用AudioPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!