How do I play an audio clip in a Java Application?

C

chump

I have been able to do it using the Applet.newAudioClip(URL)

Is there another (easy) way? Do I have to dive into the guts of Java's
sound packages?
 
K

Knute Johnson

chump said:
I have been able to do it using the Applet.newAudioClip(URL)

Is there another (easy) way? Do I have to dive into the guts of Java's
sound packages?

You can't get any simpler than Applet.newAudioClip(). The Clip
interface is much more powerful but a lot more complicated to use.
 
C

chump

I've written a simple class which plays audio in an app. Only problem
now is the thread won't die and the app stays up.

import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineEvent;
import javax.sound.sampled.LineListener;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;

/**
* This class provides simple functions for playing sounds
*
* @author ryan
*/
public class SoundPlayer implements LineListener, Runnable
{
private File soundFile;

private Thread thread;

private static SoundPlayer player;

/**
* Private because of the singleton
*/
private SoundPlayer()
{
}

/**
* Play a siren sound
*/
public static void playSiren()
{
SoundPlayer p = getPlayer();
p.playSirenFile();
}

/**
* Play the siren file
*/
private void playSirenFile()
{
this.soundFile = new File("./audio/threeHorn.wav");
thread = new Thread(this);
thread.setName("SoundPlayer");
thread.start();
}

/**
* Invoked when the thread kicks off
*/
public void run()
{
try
{
AudioInputStream stream = AudioSystem
.getAudioInputStream(this.soundFile);
AudioFormat format = stream.getFormat();

/**
* we can't yet open the device for ALAW/ULAW playback, convert
* ALAW/ULAW to PCM
*/
if ((format.getEncoding() == AudioFormat.Encoding.ULAW)
|| (format.getEncoding() == AudioFormat.Encoding.ALAW))
{
AudioFormat tmp = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED,
format.getSampleRate(),
format.getSampleSizeInBits() * 2, format.getChannels(),
format.getFrameSize() * 2, format.getFrameRate(), true);
stream = AudioSystem.getAudioInputStream(tmp, stream);
format = tmp;
}
DataLine.Info info = new DataLine.Info(Clip.class, stream
.getFormat(), ((int) stream.getFrameLength() * format
.getFrameSize()));

Clip clip = (Clip) AudioSystem.getLine(info);
clip.addLineListener(this);
clip.open(stream);
clip.start();
try
{
thread.sleep(99);
}
catch (Exception e)
{
}
while (clip.isActive() && thread != null)
{
try
{
thread.sleep(99);
}
catch (Exception e)
{
break;
}
}
clip.stop();
clip.close();
}
catch (UnsupportedAudioFileException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (LineUnavailableException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}

private static SoundPlayer getPlayer()
{
if (player == null)
{
player = new SoundPlayer();
}
return player;
}

public void update(LineEvent event)
{
}

public static void main(String[] args)
{
SoundPlayer.playSiren();
}
}
 
A

Andrew Thompson

chump said:
I've written a simple class which plays audio in an app.

I tried your exact code, but changed ..

this.soundFile = new File("./audio/threeHorn.wav");

...to..

this.soundFile = new File("./bottle-open.wav");

(a sound sample provided by Sun.)
..Only problem
now is the thread won't die and the app stays up.

The sound I tried played once, then the app. exited.
I did not see the same behaviour you describe, using
Sun's wav file.

Does the Sun sample work on your system?

Andrew T.
 
C

chump

Andrew.... where can I get my hands on a sun wav file? I'll give one a
try, but how weird would that be?
 
A

Andrew Thompson

chump said:
Andrew.... where can I get my hands on a sun wav file?

Sun Microsystems. That particular wav file is (AFAIR)
mentioned in the Java Tutorial section on Sound, and
distributed with many of the sound app. examples.
...I'll give one a
try, but how weird would that be?

It is just a standard wav file (provided by Sun -
as I mentioned)

But in any case, I have some URL's for two wav
files of my own, that, last time I checked them
(my bandwidth is too thin to be downloading them
at this instant to check) thay also worked just
fine in Java based sound applications.

You can can either of my samples here..
<http://www.javasaver.com/testjs/jmf/#media>

Andrew T.
 
A

Andrew Thompson

file seen to work with Java sound
...wav file? ....
You can can either of my samples here..
<http://www.javasaver.com/testjs/jmf/#media>

Just tested and comnfirmed that "101_0166.wav"
plays then exits, here.

I think you are trying to play a bung wav file.
How big is it? Do you have sound editing
tools that can load and save it? *

* that may correct the immediate problem

Andrew T.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,233
Latest member
AlyssaCrai

Latest Threads

Top