3

尝试在java中播放和音频剪辑,但每次都会弹出此错误。我导入了我需要的所有东西,所以我不确定问题是什么。

AudioInputStream audioInputStream = AudioSystem.getAudioInputStream (this.getClass ().getResource ("hopes_and_dreams.wav"));
                Clip clip = AudioSystem.getClip ();
                clip.open (audioInputStream);
                clip.start ();
javax.sound.sampled.LineUnavailableException: Failed to allocate clip data: Requested buffer too large.
    at com.sun.media.sound.MixerClip.implOpen(Unknown Source)
    at com.sun.media.sound.MixerClip.open(Unknown Source)
    at com.sun.media.sound.MixerClip.open(Unknown Source)
    at CA_PeterLang.paint(CA_PeterLang.java:828)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at javax.swing.JLayeredPane.paint(Unknown Source)
    at javax.swing.JComponent.paintChildren(Unknown Source)
    at javax.swing.JComponent.paintWithOffscreenBuffer(Unknown Source)
    at javax.swing.JComponent.paintDoubleBuffered(Unknown Source)
    at javax.swing.JComponent.paint(Unknown Source)
    at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
    at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
    at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
    at java.awt.Container.paint(Unknown Source)
    at sun.awt.RepaintArea.paint(Unknown Source)
    at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(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.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForHierarchy(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)
4

2 回答 2

1

我从未使用过它,但似乎你必须这样做:

Clip clip = new Clip(); // Think that you can pass the stream as parameter for the builder
clip.open(audioInputStream);

参考这里:https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html#open(javax.sound.sampled.AudioInputStream)

于 2017-06-01T00:20:14.980 回答
1

OP 的问题和无法找到该方法与Ready to Program显然运行 Java 1.4 的 IDE 有关。问题中的.getClip()方法是根据JavaDocs for AudioSystem在 Java 1.5 中添加的

但是,我过去曾遇到过系统无法找到我的特定发言人的问题,因此以下方法对我有用。请注意,我使用的是 URL,但它应该适用于某种getResource()方法。

private Mixer.Info getSpeakers()
{
    Mixer.Info speakers = null;
    Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
    for (Mixer.Info mi : mixerInfo) {
        // System.out.println(mi.getName() + "\t" +
        // mi.getDescription());

        if (mi.getName().startsWith("Speakers")) {
            speakers = mi;
        }
    }

    System.out.println(
            (speakers != null ? speakers.getName() : "<no speakers>"));

    return speakers;
}    

public void playSound(String soundFile)
{
    AudioInputStream ais = null;        
    try {
        URL url = new File(soundFile).toURI().toURL();

        ais = AudioSystem.getAudioInputStream(url);

        Mixer mixer = AudioSystem.getMixer(getSpeakers());

        DataLine.Info dataInfo = new DataLine.Info(Clip.class, null);

        Clip clip = (Clip)mixer.getLine(dataInfo);

        clip.open(ais);
        clip.start();

        do {
            try {
                Thread.sleep(50);
            }
            catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        while (clip.isActive());            
    }
    catch (UnsupportedAudioFileException | IOException |
            LineUnavailableException e) 
    {
        e.printStackTrace();
    }        
}

当用 调用时playSound("Alarm01.wav"),它会正确执行。我认为这种方法使用稍旧的方法。

编辑:请不要在这里跟随我的名字——他们被黑客入侵以进行测试。

编辑2:foreach循环可以更改为:

for (int i = 0; i < mixerInfo.length; ++i) {
    Mixer.Info mi = mixerInfo[i];
    ...

编辑 3:用作 aInputStream而不是 a URL,使用

InputStream is = this.getClass().getClassLoader().getResourceAsStream(soundName);
// add a check for null
ais = AudioSystem.getAudioInputStream(is);

编辑 4:此方法适用于 Java 1.4(据我所知)。我不得不修改我的本地机器设置来获得声音,但这是一个不同的问题。

public void playSoundOldJava(String soundFile)
{
    try {
        InputStream is = this.getClass().getClassLoader().getResourceAsStream(soundFile);

        // TODO: add check for null inputsteam
        if (is == null) {
            throw new IOException("did not find " + soundFile);
        }

        AudioInputStream ais = AudioSystem.getAudioInputStream(is);

        DataLine.Info dataInfo = new DataLine.Info(Clip.class, ais.getFormat());

        if (AudioSystem.isLineSupported(dataInfo)) {
            Clip clip = (Clip)AudioSystem.getLine(dataInfo);
            System.out.println("open");
            clip.open(ais);

            clip.start();
            do {
                try {
                    Thread.sleep(50);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            while (clip.isActive());                  

        }
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}
于 2017-06-01T00:47:17.863 回答