No method named getClip was found in method "javax.sound.sampled.AudioSystem"

Trying to play and audio clip in java but this error appears every time. I've imported everything I need, so I'm not sure what the problem is.

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)

      

+3


source to share


2 answers


The OP's problem and not being able to find a method has to do with the Ready to Program

IDE, which obviously works since Java 1.4. The method .getClip()

in the question was added in Java 1.5 as per JavaDocs for AudioSystem

However, I've had issues in the past where the system couldn't find my specific presenters, so the following approach worked for me. Please note that I am using a URL, but it has to be adapted to the approach 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();
    }        
}

      

When called with, playSound("Alarm01.wav")

it executes correctly. I think this approach uses slightly older methods.

Edit: Please don't follow my names here - they are hacked for testing purposes.

Edit 2: the loop foreach

can be changed to:



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

      

Edit 3: use like InputStream

, not URL

, use

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

      

Edit 4: This method has been working since Java 1.4 (as far as I know). I had to hack my computer's local settings to get sound, but that's a different problem.

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();
    }
}

      

+1


source


I've never used it, but it looks like you should do this:

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

      



Link here: https://docs.oracle.com/javase/7/docs/api/javax/sound/sampled/Clip.html#open(javax.sound.sampled.AudioInputStream)

+1


source







All Articles