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