Play audio clip in an Application

Discussion in 'Java' started by rohayre@gmail.com, Jan 12, 2007.

  1. Guest

    I'm trying to play an audio clip by using Applet's newAudioClip()
    method. That method takes a URL. The .wav file is located in a jar file
    (the only jar file). For example:
    if myJar.jar is the only jar file
    "java -jar myJar.jar" launches the application correctly and everything
    is wonderful. My audio clip won't play because I don't know how to
    access the .wav file from the jar file.

    This code snippet works fine when I'm not running from the jar file:

    try
    {
    File currentDir = new File(".");
    URL currentDirURL = currentDir.toURL();
    URL url = new URL(currentDirURL, fileName);
    AudioClip clip = Applet.newAudioClip(url);
    clip.play();
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }

    It knows to look for the sound file in the current directory. How do I
    adjust this code to look for the sound file in the jar.

    I have a feeling it's an easy answer involving class.getResource().....
     
    , Jan 12, 2007
    #1
    1. Advertising

  2. wrote:
    > I'm trying to play an audio clip by using Applet's newAudioClip()
    > method. That method takes a URL. The .wav file is located in a jar file
    > (the only jar file). For example:
    > if myJar.jar is the only jar file
    > "java -jar myJar.jar" launches the application correctly and everything
    > is wonderful. My audio clip won't play because I don't know how to
    > access the .wav file from the jar file.
    >
    > This code snippet works fine when I'm not running from the jar file:
    >
    > try
    > {
    > File currentDir = new File(".");
    > URL currentDirURL = currentDir.toURL();
    > URL url = new URL(currentDirURL, fileName);
    > AudioClip clip = Applet.newAudioClip(url);
    > clip.play();
    > }
    > catch (Exception e)
    > {
    > e.printStackTrace();
    > }
    >
    > It knows to look for the sound file in the current directory. How do I
    > adjust this code to look for the sound file in the jar.
    >
    > I have a feeling it's an easy answer involving class.getResource().....
    >


    You answered it yourself - URL Class.getResource(String name)

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 12, 2007
    #2
    1. Advertising

  3. Guest

    Does anyone know why this wont play? The wav file is located in the jar
    file found on the classpath. What am I missing?

    import java.applet.Applet;
    import java.applet.AudioClip;
    import java.net.URL;

    public class SoundPlayer
    {
    public void playSiren()
    {
    URL url = this.getClass().getResource("threeHorn.wav");
    AudioClip clip = Applet.newAudioClip(url);
    clip.play();
    }

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





    Knute Johnson wrote:
    > wrote:
    > > I'm trying to play an audio clip by using Applet's newAudioClip()
    > > method. That method takes a URL. The .wav file is located in a jar file
    > > (the only jar file). For example:
    > > if myJar.jar is the only jar file
    > > "java -jar myJar.jar" launches the application correctly and everything
    > > is wonderful. My audio clip won't play because I don't know how to
    > > access the .wav file from the jar file.
    > >
    > > This code snippet works fine when I'm not running from the jar file:
    > >
    > > try
    > > {
    > > File currentDir = new File(".");
    > > URL currentDirURL = currentDir.toURL();
    > > URL url = new URL(currentDirURL, fileName);
    > > AudioClip clip = Applet.newAudioClip(url);
    > > clip.play();
    > > }
    > > catch (Exception e)
    > > {
    > > e.printStackTrace();
    > > }
    > >
    > > It knows to look for the sound file in the current directory. How do I
    > > adjust this code to look for the sound file in the jar.
    > >
    > > I have a feeling it's an easy answer involving class.getResource().....
    > >

    >
    > You answered it yourself - URL Class.getResource(String name)
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/
     
    , Jan 16, 2007
    #3
  4. wrote:

    Please refrain from top-posting.

    > Does anyone know why this wont play? The wav file is located in the jar
    > file found on the classpath. What am I missing?


    The only thing I can think of, is that perhaps the
    classloader is the bootstrap classloader (and
    therefore will not find the application resource).

    > URL url = this.getClass().getResource("threeHorn.wav");

    ^
    (as an aside, please change tabs to 3 or 4 spaces before
    posting, the '^' character above lines up with where I am
    seeing the code start)

    To test that theory, try printing the details of the
    classloader at this point in the code..
    ClassLoader cl = this.getClass().getClassLoader();
    System.out.println( "ClassLoader: " + cl );
    URL url = cl.getResource("threeHorn.wav");

    ...does it tell you the classloader is 'null'?

    And another thing, are you locked into 1.2*, or can
    you go to 1.3+? If so - it might be worth looking into
    the java.sound.sampled package introduced with 1.3
    (though it may also be overkill, for this simple problem.)

    * It seems your current code would be compatible with
    Java 1.2+, unless I missed something.

    Andrew T.
     
    Andrew Thompson, Jan 16, 2007
    #4
  5. wrote:
    > Does anyone know why this wont play? The wav file is located in the jar
    > file found on the classpath. What am I missing?
    >
    > import java.applet.Applet;
    > import java.applet.AudioClip;
    > import java.net.URL;
    >
    > public class SoundPlayer
    > {
    > public void playSiren()
    > {
    > URL url = this.getClass().getResource("threeHorn.wav");
    > AudioClip clip = Applet.newAudioClip(url);
    > clip.play();
    > }
    >
    > public static void main(String[] args)
    > {
    > new SoundPlayer().playSiren();
    > }
    > }
    >


    Looks like the program is ending before the AudioClip can play. Try
    putting a sleep after playSiren();

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 16, 2007
    #5
  6. chump Guest

    Andrew....

    Thanks for you help. Few things:

    - What is top-posting?
    - The classloader is not null. I am running java 1.5 in eclipse on
    MacOSX. The classloader is printing out
    "sun.misc.Launcher$AppClassLoader@a9c85c"

    Still looking.....


    Andrew Thompson wrote:
    > wrote:
    >
    > Please refrain from top-posting.
    >
    > > Does anyone know why this wont play? The wav file is located in the jar
    > > file found on the classpath. What am I missing?

    >
    > The only thing I can think of, is that perhaps the
    > classloader is the bootstrap classloader (and
    > therefore will not find the application resource).
    >
    > > URL url = this.getClass().getResource("threeHorn.wav");

    > ^
    > (as an aside, please change tabs to 3 or 4 spaces before
    > posting, the '^' character above lines up with where I am
    > seeing the code start)
    >
    > To test that theory, try printing the details of the
    > classloader at this point in the code..
    > ClassLoader cl = this.getClass().getClassLoader();
    > System.out.println( "ClassLoader: " + cl );
    > URL url = cl.getResource("threeHorn.wav");
    >
    > ..does it tell you the classloader is 'null'?
    >
    > And another thing, are you locked into 1.2*, or can
    > you go to 1.3+? If so - it might be worth looking into
    > the java.sound.sampled package introduced with 1.3
    > (though it may also be overkill, for this simple problem.)
    >
    > * It seems your current code would be compatible with
    > Java 1.2+, unless I missed something.
    >
    > Andrew T.
     
    chump, Jan 16, 2007
    #6
  7. chump Guest

    It's throwing a null pointer exception before it gets a chance to
    end....

    java.lang.NullPointerException
    at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:48)
    at java.applet.Applet.newAudioClip(Applet.java:273)
    at com.gizmo.util.SoundPlayer.playSiren(Unknown Source)
    at com.gizmo.util.SoundPlayer.main(Unknown Source)
    sleeping...
    Ending...


    Knute Johnson wrote:
    > wrote:
    > > Does anyone know why this wont play? The wav file is located in the jar
    > > file found on the classpath. What am I missing?
    > >
    > > import java.applet.Applet;
    > > import java.applet.AudioClip;
    > > import java.net.URL;
    > >
    > > public class SoundPlayer
    > > {
    > > public void playSiren()
    > > {
    > > URL url = this.getClass().getResource("threeHorn.wav");
    > > AudioClip clip = Applet.newAudioClip(url);
    > > clip.play();
    > > }
    > >
    > > public static void main(String[] args)
    > > {
    > > new SoundPlayer().playSiren();
    > > }
    > > }
    > >

    >
    > Looks like the program is ending before the AudioClip can play. Try
    > putting a sleep after playSiren();
    >
    > --
    >
    > Knute Johnson
    > email s/nospam/knute/
     
    chump, Jan 16, 2007
    #7
  8. chump wrote:
    > It's throwing a null pointer exception before it gets a chance to
    > end....
    >
    > java.lang.NullPointerException
    > at sun.applet.AppletAudioClip.<init>(AppletAudioClip.java:48)
    > at java.applet.Applet.newAudioClip(Applet.java:273)
    > at com.gizmo.util.SoundPlayer.playSiren(Unknown Source)
    > at com.gizmo.util.SoundPlayer.main(Unknown Source)
    > sleeping...
    > Ending...
    >
    >
    > Knute Johnson wrote:
    >> wrote:
    >>> Does anyone know why this wont play? The wav file is located in the jar
    >>> file found on the classpath. What am I missing?
    >>>
    >>> import java.applet.Applet;
    >>> import java.applet.AudioClip;
    >>> import java.net.URL;
    >>>
    >>> public class SoundPlayer
    >>> {
    >>> public void playSiren()
    >>> {
    >>> URL url = this.getClass().getResource("threeHorn.wav");
    >>> AudioClip clip = Applet.newAudioClip(url);
    >>> clip.play();
    >>> }
    >>>
    >>> public static void main(String[] args)
    >>> {
    >>> new SoundPlayer().playSiren();
    >>> }
    >>> }
    >>>

    >> Looks like the program is ending before the AudioClip can play. Try
    >> putting a sleep after playSiren();
    >>
    >> --
    >>
    >> Knute Johnson
    >> email s/nospam/knute/

    >


    Do you have the .wav file in the jar? I get that error if it isn't in
    the jar.

    --

    Knute Johnson
    email s/nospam/knute/
     
    Knute Johnson, Jan 16, 2007
    #8
  9. chump wrote:
    > Andrew....
    >
    > Thanks for you help.


    You're welcome.

    >...Few things:
    >
    > - What is top-posting?


    I'm glad you asked. But instead of answering, I'll
    demonstrate a handy technique that will answer that,
    as well as many other question later questions..
    <http://www.google.com/search?q=definition+top-post>

    Note that by changing anything after 'definition', you
    get the meaning of the word or phrase within one or
    two more clicks (usually).

    > - The classloader is not null. I am running java 1.5 in eclipse on
    > MacOSX. The classloader is printing out
    > "sun.misc.Launcher$AppClassLoader@a9c85c"


    OK - thanks for confirming that is not the problem.

    > Still looking.....


    I'll review the rest of the thread..

    Andrew T.
     
    Andrew Thompson, Jan 17, 2007
    #9
  10. chump Guest

    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();
    }
    }
     
    chump, Jan 17, 2007
    #10
  11. 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/
     
    Knute Johnson, Jan 17, 2007
    #11
  12. chump Guest

    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?
     
    chump, Jan 19, 2007
    #12
  13. 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/
     
    Knute Johnson, Jan 19, 2007
    #13
  14. GNightOwl

    Joined:
    Dec 3, 2008
    Messages:
    1
    thank you!

    ah thank you so much, I have been trying for hours to figure out how to reference it.
    :)
     
    GNightOwl, Dec 3, 2008
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco
    Replies:
    1
    Views:
    362
    Oliver Wong
    Sep 8, 2005
  2. Lola
    Replies:
    34
    Views:
    14,653
    Oldmancolorado_deleted
    Jun 16, 2010
  3. illmagination

    Play Sound Clip Once

    illmagination, Sep 28, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    539
    illmagination
    Sep 28, 2006
  4. chump
    Replies:
    7
    Views:
    1,011
    Andrew Thompson
    Jan 19, 2007
  5. sick dick
    Replies:
    0
    Views:
    407
    sick dick
    Apr 3, 2006
Loading...

Share This Page