2

I attempted to mimic the code found here https://stackoverflow.com/tags/javasound/info but I cannot make it play either through loop() or start(). I've looked for answers but it seems like mine is just a fluke or a stupid mistake that everyone else is good enough to recognize.

import javax.sound.sampled.*;
import java.net.URL;

public class AudioTest
{
    public static void main(String[] args) throws Exception
    {
        URL url = new URL("http://www.public.asu.edu/~wnjones1/leftright.wav");
        Clip clip = AudioSystem.getClip();
        AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
        clip.open(audioIn);
        clip.start();
    }
}

It has everything that the example has short of the GUI but that shouldn't matter, should it? It should still be able to play at least once right?

Any help would be much appreciated. Thank you!

--EDIT-- It is a simple two second .wav file that I am pulling from my website. I am using Java7u21.

--EDIT v2.0-- So basically what I learned is... Keep the GUI. Or use an Applet so you don't have to worry about the main() ending.

import javax.swing.*;

public class Assignment6me extends JApplet
{
    private int APPLET_WIDTH = 400, APPLET_HEIGHT = 160;

    private AudioPanel ap;

    //The method init initializes the Applet with a Pane with two tabs
    public void init()
    {
        try
        {
            ap = new AudioPanel();
        }
        catch(Exception e)
        {}
        getContentPane().add(ap);
        setSize (APPLET_WIDTH, APPLET_HEIGHT); //set Applet size
    }
}



import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
import java.io.File;


public class AudioPanel extends JPanel
{
    public AudioPanel() throws Exception
    {
        File file = new File("Don't Stop Believin'.wav");
        Clip clip = AudioSystem.getClip();
         // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.getAudioInputStream( file );
        clip.open(ais);
        clip.start();
    }
}
Community
  • 1
  • 1
no-one
  • 55
  • 5

1 Answers1

1

The (working) source seen on the Java Sound info. page is precisely.

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;

public class LoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");
            }
        });
    }
}

I draw your attention to:

                // A GUI element to prevent the Clip's daemon Thread
                // from terminating at the end of the main()
                JOptionPane.showMessageDialog(null, "Close to exit!");

Add that part and it should be fine.


So I can't play any files without a GUI?

I can't recall a command line based app. that does play sound, but it is possible.

import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
import java.util.Scanner;

public class LoopSound {

    public static void main(String[] args) throws Exception {
        URL url = new URL(
            "http://pscode.org/media/leftright.wav");
        Clip clip = AudioSystem.getClip();
        // getAudioInputStream() also accepts a File or InputStream
        AudioInputStream ais = AudioSystem.
            getAudioInputStream( url );
        clip.open(ais);
        clip.loop(Clip.LOOP_CONTINUOUSLY);
        Scanner scanner = new Scanner (System.in);
        scanner.nextInt();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • I purposely left out the GUI. I assumed I could do it without one. So I can't play any files without a GUI? – no-one Apr 28 '13 at 15:03
  • No I didn't say that. See the edit. All that is needed is something to prevent the `main(String)` from ending. And if you come back with *"So I can't play songs without a `Scanner`?"* Be warned that I will have to smack you upside the head (it's a rule). – Andrew Thompson Apr 29 '13 at 02:30
  • "So... I can't play music without a Scanner?" I kid. But thanks. It does make more sense why the GUI is there. – no-one Apr 29 '13 at 14:28