所以,我现在正在为我的游戏开发新的声音引擎,因为旧的声音引擎不好。为此,我尝试使用SourceDataLine,但运行时它返回:
java.lang.ClassCastException: com.sun.media.sound.DirectAudioDevice$DirectClip cannot be cast to javax.sound.sampled.SourceDataLine
at com.glowiak.aws.SoundLibrary.play(Main.java:1360)
at com.glowiak.aws.UIButton.loop(Main.java:475)
at com.glowiak.aws.Renderer.paintComponent(Main.java:174)
at javax.swing.JComponent.paint(JComponent.java:1056)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5210)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:290)
at javax.swing.RepaintManager.paint(RepaintManager.java:1272)
at javax.swing.JComponent._paintImmediately(JComponent.java:5158)
at javax.swing.JComponent.paintImmediately(JComponent.java:4969)
at javax.swing.RepaintManager$4.run(RepaintManager.java:831)
at javax.swing.RepaintManager$4.run(RepaintManager.java:814)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:789)
at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:738)
at javax.swing.RepaintManager.access$1200(RepaintManager.java:64)
at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1732)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
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:74)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
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)
发动机代码为:
class SoundLibrary // the second option from https://www.baeldung.com/java-play-sound modified to fit my engine idea
{
public static final int BUFFER_SIZE = 4096;
public ArrayList<String> urls;
public void registerSound(String fp)
{
urls.add(fp);
}
public boolean play(int i)
{
try {
InputStream is = getClass().getResourceAsStream(urls.get(urls.size() - 1));
AudioInputStream ais = AudioSystem.getAudioInputStream(is);
SourceDataLine.Info dli = new DataLine.Info(Clip.class, ais.getFormat());
SourceDataLine sdl = (SourceDataLine)AudioSystem.getLine(dli);
sdl.open(ais.getFormat());
sdl.start();
byte[] buffer = new byte[BUFFER_SIZE];
int rb = -1;
while ((rb = ais.read(buffer)) != -1)
{
sdl.write(buffer, 0, rb);
}
sdl.drain();
sdl.close();
ais.close();
return true;
} catch (Exception e)
{
e.printStackTrace();
return false;
}
}
public static void cleanup()
{
}
public SoundLibrary()
{
this.urls = new ArrayList<String>();
this.registerSound("/resources/sound/bow.wav");
this.registerSound("NULL");
this.registerSound("/resources/sound/buttonpress.wav");
}
}
就在上面。
在这里我写的东西,没有人关心只是StackOverflowException告诉我,我必须在这里写的东西,否则它不会允许我张贴,只是忽略这两行。
有人能解释一下我做错了什么吗?
1条答案
按热度按时间bq3bfh9z1#
下面的方法对你有效吗?改变你获取URL的方式来适应你的情况。