2

I've been rampaging about for a couple of hours now looking for sample code that can play simple wav files in Java. However, none of the ones I've received are working for me. Maybe it's just me that doesn't understand how to operate the sample code but could anyone provide me with sample code and "instructions" on how to get it working correctly. Any help would be much appreciated.

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
john nguyen
  • 45
  • 1
  • 8

3 Answers3

3

This code will create a clip and play it continuously once started:

import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;


Clip clip = AudioSystem.getClip();
clip.open(AudioSystem.getAudioInputStream(new URL(filename)));
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
Tot Zam
  • 8,406
  • 10
  • 51
  • 76
0

There are more modern ways to do this, but the applet class AudioClip might satisfy your needs:

import java.applet.Applet;
import java.applet.AudioClip;

final AudioClip clip = Applet.newAudioClip(resourceUrl);

To play it once:

clip.play();

To loop:

clip.loop();
clip.stop();

See Javadocs for Applet and AudioClip

getAudioClip

public AudioClip getAudioClip(URL url)

Returns the AudioClip object specified by the URL argument. This method always returns immediately, whether or not the audio clip exists. When this applet attempts to play the audio clip, the data will be loaded.

Parameters:

url - an absolute URL giving the location of the audio clip.

Returns:

the audio clip at the specified URL.

You don't need to actually be doing anything with applets for this to work. It will work fine in a regular Java application.

Greg Kopff
  • 15,945
  • 12
  • 55
  • 78
  • It is recommended to use the javax Clip over AudioClip. AudioClip is just older and presents limited functionalities. This idea is explained [here](http://stackoverflow.com/questions/26695530/what-is-the-difference-between-audioclip-and-clip-in-java). – Tot Zam Jun 02 '15 at 04:59
  • @TotZam: I said as much in the first sentence. :) But from what OP describes, this covers the functionality he requires. – Greg Kopff Jun 02 '15 at 05:00
  • @GregKopff when it says resourceUrl, is that where i'm suppose to put the file path? – john nguyen Jun 02 '15 at 05:14
  • i tried putting in my file path C:\Users\John\Desktop\pokemon.wav but it didn't work and the error it gave me was ')' expected. And if i put the entire thing in quotes, it gives me an error that says illegal escape character. – john nguyen Jun 02 '15 at 05:14
  • @johnnguyen: you need to make a [`URL`](https://docs.oracle.com/javase/8/docs/api/java/net/URL.html#URL-java.lang.String-) object. A URL can be a `file:///` URL. – Greg Kopff Jun 02 '15 at 05:57
  • @GregKopff so Url urlname = new Url(filepath); ? Could you give me an example of a new URL object, i'm still a beginner at Java. – john nguyen Jun 02 '15 at 17:07
  • @johnnguyen: `URL url = new URL("file:///c:/Users/John/Desktop/pokemon.wav")` (I think - I don't use Windows so I can't test the drive letter bit) - More info see [here](http://stackoverflow.com/questions/8406025/local-file-protocol-for-java-net-url) – Greg Kopff Jun 02 '15 at 22:07
  • @GreGKopff, it give me an error when i try compiling it and it says cannot find symbol class URL. – john nguyen Jun 03 '15 at 17:11
  • @johnnguyen: you need to `import java.net.URL`. – Greg Kopff Jun 03 '15 at 22:19
0
import java.io.File;
import javax.sound.sampled.*;

public void play(File file) 
{
    try
{
    final Clip clip = (Clip)AudioSystem.getLine(new Line.Info(Clip.class));

        clip.addLineListener(new LineListener()
        {
        @Override
        public void update(LineEvent event)
            {
            if (event.getType() == LineEvent.Type.STOP)
                clip.close();
            }
        });

        clip.open(AudioSystem.getAudioInputStream(file));
        clip.start();
    }
        catch (Exception exc)
    {
        exc.printStackTrace(System.out);
    }
}
Victor Juliet
  • 1,052
  • 11
  • 21