Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Play audio clip in an Application

Reply
Thread Tools

Play audio clip in an Application

 
 
Knute Johnson
Guest
Posts: n/a
 
      01-17-2007
chump wrote:
> I've been able to play an audio clip with the code below. Only problem
> now is the thread won't die and the app stays running.
>
> Does the thread need to be set as Deamon?
>
> 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();
> }
> }
>


Here is how to play a Clip. But what was wrong with using AudioClip?

import java.io.*;
import javax.sound.sampled.*;

public class PlayClip {
public static void main(String[] args) {
try {
AudioInputStream ais =
AudioSystem.getAudioInputStream(new File(args[0]));
AudioFormat af = ais.getFormat();
Clip line = AudioSystem.getClip();
line.open(ais);
line.start();
line.drain();
line.stop();
line.close();
} catch (Exception e) {
System.out.println(e);
}
}
}

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
 
 
 
chump
Guest
Posts: n/a
 
      01-19-2007

I ran your app. It works, plays the file and is very clean. One
thing... my app doesn't go down after the file plays. In my previous
example, I thought it was the thread I was spinning off wouldn't die,
but your code doesn't create a new thread and it still stays up for me.
Are any of the audio classes hanging onto something that is keeping the
app up?

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      01-19-2007
chump wrote:
> I ran your app. It works, plays the file and is very clean. One
> thing... my app doesn't go down after the file plays. In my previous
> example, I thought it was the thread I was spinning off wouldn't die,
> but your code doesn't create a new thread and it still stays up for me.
> Are any of the audio classes hanging onto something that is keeping the
> app up?
>


There is a change in Java Sound, I think between 1.4 and 1.5. In 1.4
there is a thread created that will hold the program and in 1.5 that was
fixed. I tried my code in 1.6 and there is another wrinkle with it. It
won't play the file unless you put a very short sleep after the start()
call. Java Sound has unfortunately not been a high priority for Sun and
there are some inconsistencies.

If you are going to use this in a production environment you might want
to consider using a LineListener to control the actions of your program.

--

Knute Johnson
email s/nospam/knute/
 
Reply With Quote
 
GNightOwl GNightOwl is offline
Junior Member
Join Date: Dec 2008
Posts: 1
 
      12-03-2008
ah thank you so much, I have been trying for hours to figure out how to reference it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I play an audio clip in a Java Application? chump Java 7 01-19-2007 04:32 AM
Play Sound Clip Once illmagination ASP .Net 3 09-28-2006 02:03 PM
use audio device to play recieved partial audio data sick dick Javascript 0 04-03-2006 04:47 AM
Accessing underlying bytes in a Audio Clip Marco Java 1 09-08-2005 06:37 PM
mpg/movie clip to play on DVD Fishb8 NZ Computing 11 04-21-2005 05:26 PM



Advertisments