gpt4 book ai didi

java - 卡在java中播放wav文件上......需要一些指导

转载 作者:行者123 更新时间:2023-12-01 12:12:14 24 4
gpt4 key购买 nike

我试图在按下按钮时在java中播放声音。所以我有类似以下内容:

public void playSound(File soundFile) {
try {
AudioInputStream stream = AudioSystem.getAudioInputStream(soundFile);
AudioFormat format = stream.getFormat();
DataLine.Info info = new DataLine.Info(Clip.class, format);
Clip clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
}
catch (Exception e) {e.printStackTrace();}
}

然后我在buttonListener中调用actionPerformed中的方法:

playSound(new File("woow_x.wav"));

但它抛出 UnsupportedAudioFileException。这是说不支持 .wav 文件吗?我确认 wav 文件可以工作,所以我不知道问题是什么。我正在尝试在不使用阳光的情况下做到这一点。请让我知道这是如何正确完成的。谢谢。

堆栈跟踪:

    javax.sound.sampled.UnsupportedAudioFileException: could not get audio input 
stream from input file
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at robotMaze.SystemGUI.playSound(SystemGUI.java:183)
at robotMaze.SystemGUI$SendButtonListener.actionPerformed(SystemGUI.java:229)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

最佳答案

文件的编码在其 header 中指定为0x55。我不确定 0x55 到底是什么,但我怀疑它是 mp3。不管怎样,它都不是 Java 支持的编码之一。

为了您的目的(并且如果许可证允许),您可以使用音频编辑器将文件转换为支持的格式。 (这些由 AudioFormat.Encoding 中的静态字段枚举。)

关于java - 卡在java中播放wav文件上......需要一些指导,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27205306/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com