编辑 - 我发现如果我使用“CircusSong”wav文件进行按钮点击,它就会开始播放。因此,我尝试访问的其他文件必须不兼容。如何确定哪种格式的.wav文件可以使用?
注意:我尝试过的每个音频文件都在我的机器上播放(Ubuntu)。他们只是没有得到IntelliJ的认可。
我有这个声音文件在程序运行时播放(直到我停止线程)。我能够成功地将它添加到我的“src”文件夹中的“Sounds”文件夹中。
注意:我刚刚学习如何使用声音文件。如果我转到IntelliJ中的文件,这就是它的样子:
嗯,我想在程序中多发一个声音,到目前为止我已经尝试了两个没有运气的声音。当我将鼠标悬停在错误上时,我会得到“预期的类或接口”:
以下是这两个文件的用途:
字段声明和声音剪辑方法:
public class SimManager implements SimulationEventListener, LineListener {
File mainMusicThemeSong = new File("src/Sounds/CircusSong.wav");
File selectionSound = new File("src/Sounds/SelectionSound.wav");
public static Clip mainClip;
public static Clip selectionClip;
public void playSelectionSound() {
try {
Line.Info linfo = new Line.Info(Clip.class);
Line line = AudioSystem.getLine(linfo);
selectionClip = (Clip) line;
selectionClip.addLineListener(this);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(selectionSound);
selectionClip.open(audioIn);
selectionClip.start();
} catch (Exception e) {
System.out.println("ERROR WHILE PLAYING SOUND CLIP");
}
}
public void playMainThemeSong() {
try {
Line.Info linfo = new Line.Info(Clip.class);
Line line = AudioSystem.getLine(linfo);
mainClip = (Clip) line;
mainClip.addLineListener(this);
AudioInputStream audioIn = AudioSystem.getAudioInputStream(mainMusicThemeSong);
mainClip.open(audioIn);
mainClip.start();
}catch(Exception e) {
System.out.println("ERROR PLAYING MUSIC CLIP");
}
}
然后,同样在该课程中,我确定播放哪种声音。单击“运行”后将播放主题。当选择所有其他按钮时,应该播放另一个声音,但我只显示“NORMAL_SETUP_EVENT”,因此没有太多不必要的代码。
public void simulationEventOccurred(SimulationEvent simulationEvent) {
if (simulationEvent.getEventType() == SimulationEvent.NORMAL_SETUP_EVENT) {
playSelectionSound(); // <-- THIS DOESN"T WORK
colonySim.initializeAntColony(); // INITIALIZE NORMALLY
antSimGUI.setTime(time);
}
if (simulationEvent.getEventType() == SimulationEvent.RUN_EVENT) {
isMoving = false;
thisThread = new Thread() {
public void run() {
playMainThemeSong(); //<-- THIS WORKS
mainClip.loop(-1); //<-- THIS LOOPS UNTIL THREAD STOPS
newTurn(); // START INCREMENTING THROUGH TURNS
}
};
thisThread.start();
}
那么,我是否只有不兼容的文件?因为我的代码没有看到任何错误,因为主题歌曲文件工作得很好。为什么我的.wav文件格式无法识别?
这是堆栈跟踪:
COULDN'T PLAY SOUND CLIP
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1189)
at SimManager.simulationEventOccurred(SimManager.java:131)
at AntSimGUI.fireSimulationEvent(AntSimGUI.java:193)
at AntSimGUI$ControlPanel$ButtonHandler.actionPerformed(AntSimGUI.java:407)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
at java.awt.Component.processMouseEvent(Component.java:6535)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Container.processEvent(Container.java:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
at java.awt.Window.dispatchEventImpl(Window.java:2750)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)