What is the quickest way to play sound?

Discussion in 'Java' started by pek, May 25, 2008.

  1. pek

    pek Guest

    I've been looking around for playing a single sound clip (less then 2
    seconds). The Java Tutorial is HUGE and the other alternative is using
    JMF, which I don't want to.

    So, is there anywhere a single snippet that will just play a small
    sound file? The file is in ogg format. If not, is there an easier
    tutorial than the one at sun.com?

    Thank you,
    Panagiotis
    pek, May 25, 2008
    #1
    1. Advertising

  2. On May 25, 12:50 pm, pek <> wrote:
    ...
    > So, is there anywhere a single snippet that will just play a small
    > sound file?


    Sure. Applets can do it in a couple of lines
    of code. So long as the sound is in a simple
    format like AU or WAV or AIFF. But..

    >..The file is in ogg format.


    (laughs) No. Neither an applet, nor the J2SE
    sound classes, nor the (standard) JMF will play
    an OGG file, to my knowledge. FMJ might support
    them, or JMF with you providing a decoder for OGG.

    Alternately, you might wait for Java Media
    Components in Java 7 and simply throw it onto
    the native player.

    >..If not, is there an easier
    > tutorial than the one at sun.com?


    Media is hard. People seem to think it should
    be 'easy', but that is just not the reality.

    --
    Andrew T.
    PhySci.org
    Andrew Thompson, May 25, 2008
    #2
    1. Advertising

  3. On May 25, 12:50 pm, pek <> wrote:
    ...
    > ...The file is in ogg format.


    Is there any reason the file has to *stay* in OGG
    format? Do you control it? If you can convert it
    to one of the formats supported by J2SE, it becomes
    simpler.

    --
    Andrew T.
    PhySci.org
    Andrew Thompson, May 25, 2008
    #3
  4. pek

    pek Guest

    On May 25, 7:10 am, Andrew Thompson <> wrote:
    > On May 25, 12:50 pm, pek <> wrote:
    > ...
    >
    > > ...The file is in ogg format.

    >
    > Is there any reason the file has to *stay* in OGG
    > format? Do you control it? If you can convert it
    > to one of the formats supported by J2SE, it becomes
    > simpler.

    Of course not. ;) I just found it like this. I could easily convert it
    in wav.

    And yes, I am one of those people that believe media is easy. But, in
    MHO, for the simplest thing in the world, there should be a simple
    way. I mean, come one, I just like to play one single sound.

    Think about this, if I find how to do this, probably my final
    architecture will allow me to do just this:
    AudioPlayer.play(String url)
    Obviously because I won't be writing 3294 lines of code each time I
    want to play a sound. So, what's so difficult for this to be in
    there?

    Thanks ;)
    pek, May 25, 2008
    #4
  5. On May 25, 7:09 pm, pek <> wrote:
    > On May 25, 7:10 am, Andrew Thompson <> wrote:> On May 25, 12:50 pm, pek <> wrote:
    > > ...
    > > ...If you can convert it
    > > to one of the formats supported by J2SE, it becomes
    > > simpler.

    >
    > Of course not. ;) I just found it like this. I could easily convert it
    > in wav.


    Then try something along these lines (it
    is even easier for an applet)..

    <sscce>
    import javax.sound.sampled.Clip;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.AudioInputStream;

    import java.net.*;
    import javax.swing.*;

    class PlayClip {
    public static void main(String[] args) throws Exception {
    URL soundLocation = new URL(
    "file:C:/the.wav");
    // can use one clip many times
    Clip clip = AudioSystem.getClip();

    AudioInputStream inputStream =
    AudioSystem.getAudioInputStream(soundLocation);
    clip.open( inputStream );
    clip.loop(50);
    clip.start();

    // kludge to prevent the main() from exiting.
    JFrame f = new JFrame();
    f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    f.getContentPane().add( new
    JLabel(soundLocation.toURI().toString()) );
    f.pack();

    f.setVisible(true);
    }
    }
    </sscce>

    --
    Andrew T.
    PhySci.org
    Andrew Thompson, May 25, 2008
    #5
  6. pek

    pek Guest

    On May 25, 1:35 pm, Andrew Thompson <> wrote:
    > On May 25, 7:09 pm, pek <> wrote:
    >
    > > On May 25, 7:10 am, Andrew Thompson <> wrote:> On May 25, 12:50 pm, pek <> wrote:
    > > > ...
    > > > ...If you can convert it
    > > > to one of the formats supported by J2SE, it becomes
    > > > simpler.

    >
    > > Of course not. ;) I just found it like this. I could easily convert it
    > > in wav.

    >
    > Then try something along these lines (it
    > is even easier for an applet)..
    >
    > <sscce>
    > import javax.sound.sampled.Clip;
    > import javax.sound.sampled.AudioSystem;
    > import javax.sound.sampled.LineUnavailableException;
    > import javax.sound.sampled.AudioInputStream;
    >
    > import java.net.*;
    > import javax.swing.*;
    >
    > class PlayClip {
    > public static void main(String[] args) throws Exception {
    > URL soundLocation = new URL(
    > "file:C:/the.wav");
    > // can use one clip many times
    > Clip clip = AudioSystem.getClip();
    >
    > AudioInputStream inputStream =
    > AudioSystem.getAudioInputStream(soundLocation);
    > clip.open( inputStream );
    > clip.loop(50);
    > clip.start();
    >
    > // kludge to prevent the main() from exiting.
    > JFrame f = new JFrame();
    > f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    >
    > f.getContentPane().add( new
    > JLabel(soundLocation.toURI().toString()) );
    > f.pack();
    >
    > f.setVisible(true);
    > }}
    >
    > </sscce>
    >
    > --
    > Andrew T.
    > PhySci.org


    That is exactly the code I was looking for.. ;)

    Just in case you know even more, I am using openSuSE 10.3.. I did
    convert the files in wav and they play just fine. But when I run this
    code (I create a thread so multiple sounds can play simultaneous) I
    get this error:

    I'm not expecting you to answer. Just in case you know better (I hate
    the sound system in linux).
    pek, May 25, 2008
    #6
  7. pek

    pek Guest

    On May 25, 2:52 pm, pek <> wrote:
    > On May 25, 1:35 pm, Andrew Thompson <> wrote:
    >
    >
    >
    > > On May 25, 7:09 pm, pek <> wrote:

    >
    > > > On May 25, 7:10 am, Andrew Thompson <> wrote:> On May 25, 12:50 pm, pek <> wrote:
    > > > > ...
    > > > > ...If you can convert it
    > > > > to one of the formats supported by J2SE, it becomes
    > > > > simpler.

    >
    > > > Of course not. ;) I just found it like this. I could easily convert it
    > > > in wav.

    >
    > > Then try something along these lines (it
    > > is even easier for an applet)..

    >
    > > <sscce>
    > > import javax.sound.sampled.Clip;
    > > import javax.sound.sampled.AudioSystem;
    > > import javax.sound.sampled.LineUnavailableException;
    > > import javax.sound.sampled.AudioInputStream;

    >
    > > import java.net.*;
    > > import javax.swing.*;

    >
    > > class PlayClip {
    > > public static void main(String[] args) throws Exception {
    > > URL soundLocation = new URL(
    > > "file:C:/the.wav");
    > > // can use one clip many times
    > > Clip clip = AudioSystem.getClip();

    >
    > > AudioInputStream inputStream =
    > > AudioSystem.getAudioInputStream(soundLocation);
    > > clip.open( inputStream );
    > > clip.loop(50);
    > > clip.start();

    >
    > > // kludge to prevent the main() from exiting.
    > > JFrame f = new JFrame();
    > > f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    >
    > > f.getContentPane().add( new
    > > JLabel(soundLocation.toURI().toString()) );
    > > f.pack();

    >
    > > f.setVisible(true);
    > > }}

    >
    > > </sscce>

    >
    > > --
    > > Andrew T.
    > > PhySci.org

    >
    > That is exactly the code I was looking for.. ;)
    >
    > Just in case you know even more, I am using openSuSE 10.3.. I did
    > convert the files in wav and they play just fine. But when I run this
    > code (I create a thread so multiple sounds can play simultaneous) I
    > get this error:
    >
    > I'm not expecting you to answer. Just in case you know better (I hate
    > the sound system in linux).


    OK.. Before you answer... Errm.. Linux worked! I didn't even touch it!
    I just closed eclipse, started it and it worked..! God I hate linux
    sound system. But anyways.. Thank you very much for your quick answer.
    I'm using it exactly as you said (but in a thread).
    pek, May 25, 2008
    #7
  8. On May 25, 9:54 pm, pek <> wrote:
    ...
    > ...God I hate linux
    > sound system.


    LOL! 'Linux sound' was enough to make Jamie
    Zawinski dump Linux and convert to Apple and
    MacOS.

    >..But anyways.. Thank you very much for your quick answer.


    No worries.

    > I'm using it exactly as you said (but in a thread).


    I can only imagine that any implementation based
    on a Thread would be 'cleaner' than what I did
    in that example above! Note that (as was pointed
    out to me in the last 72ish hours) a Runnable
    can do most anything that a Thread can.

    --
    Andrew T.
    PyhSci.org
    Andrew Thompson, May 25, 2008
    #8
  9. pek

    pek Guest

    On May 25, 3:28 pm, Andrew Thompson <> wrote:
    > On May 25, 9:54 pm, pek <> wrote:
    > ...
    >
    > > ...God I hate linux
    > > sound system.

    >
    > LOL! 'Linux sound' was enough to make Jamie
    > Zawinski dump Linux and convert to Apple and
    > MacOS.

    I didn't get this. Did you say this ironically? I have no idea who is
    he (other than the small bio wikipedia provides). But anyway, when I
    said "linux sound system" I was talking about the chaotic API and
    Services for sound in linux (ALSA, OSS, Pulse etc.). They can't seem
    to go along together. Once a program uses one of the APIs, the next
    can't use another. So a lot of programs cannot run together. In my
    case, I was running RealPlayer, VirtualBox and my Java app. VirtualBox
    probably was "stealing the sound", so when I stopped it, it worked!

    >
    > >..But anyways.. Thank you very much for your quick answer.

    >
    > No worries.
    >
    > > I'm using it exactly as you said (but in a thread).

    >
    > I can only imagine that any implementation based
    > on a Thread would be 'cleaner' than what I did
    > in that example above! Note that (as was pointed
    > out to me in the last 72ish hours) a Runnable
    > can do most anything that a Thread can.
    >

    Yes, in case anybody needs it, my (rather dangerous) code is this:

    public class Utility {
    private static Map<String, File> sndCache = new
    ConcurrentHashMap<String, File>();
    public static synchronized File getSound(String url) {
    if ( sndCache.containsKey(url) )
    return sndCache.get(url);

    URL sndURL = Main.class.getResource("/res/snd/" + url);
    if ( sndURL != null ) {
    try {
    sndCache.put(url, new File(sndURL.toURI()));
    return sndCache.get(url);
    } catch (URISyntaxException e) {
    e.printStackTrace();
    return null;
    }
    } else {
    System.err.println("Couldn't find file: " + url);
    return null;
    }
    }
    public static synchronized void playSound(final String url) {
    new Thread(new Runnable() {
    @Override
    public void run() {
    try {
    Clip clip = AudioSystem.getClip();
    AudioInputStream inputStream =
    AudioSystem.getAudioInputStream(getSound(url));
    clip.open(inputStream);
    clip.start();
    } catch (Exception e) {
    System.err.println(e.getMessage());
    }
    }
    }).start();
    }
    }

    I just create a thread with a runnable and run it on the fly. Although
    I didn't need it, this way the sound cannot be stopped. So change
    appropriately.
    > --
    > Andrew T.
    > PyhSci.org


    Thanks again.. ;)
    pek, May 25, 2008
    #9
  10. pek

    pek Guest

    On May 25, 5:04 pm, Lew <> wrote:
    > pek wrote:
    > > Yes, in case anybody needs it, my (rather dangerous) code is this:

    >
    > > public class Utility {
    > > private static Map<String, File> sndCache = new
    > > ConcurrentHashMap<String, File>();
    > > public static synchronized File getSound(String url) {

    > ...
    > > }
    > > public static synchronized void playSound(final String url) {

    > ...
    > > }

    >
    > > I just create a thread with a runnable and run it on the fly. Although
    > > I didn't need it, this way the sound cannot be stopped. So change
    > > appropriately.

    >
    > Personally I'd be a lot more comfortable coding those as instance methods.
    >
    > --
    > Lew


    Could you explain a little? What should be an instance member? The
    game I'm creating needs just a simple basic way of playing a sound.
    Even if a sound plays on top of the other, it's still OK since they
    are sound effects. But I would happily here your idea.

    Panagiotis
    pek, May 25, 2008
    #10
  11. On May 25, 12:50=A0pm, pek <> wrote:
    =2E..
    > So, is there anywhere a single snippet that will just play a small
    > sound file?


    Sure. Applets can do it in a none of of vinegars
    of disorder. So categorical as the sound is in a meaningless
    format like AU or WAV or AIFF. But..

    >..The file is in ogg format.


    (expends) No. Neither a shebang, nor the J2SE
    sound classes, nor the (standard) JMF will play
    an OGG file, to my damnation. FMJ might support
    them, or JMF with you taunting an item for OGG.

    Alternately, you might wait for Java Media
    Components in Java 7 and precisely throw it onto
    the rational pan.

    >..If not, is there an arguable
    > tutorial than the one at sun.com?


    Media is accurate. People seem to think it should
    be 'candid', but that is just not the lifestyle.

    --
    Kathy T.
    PhySci.org


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "I do remain confident in Linda.
    She'll make a fine labor secretary.

    From what I've read in the press accounts,
    she's perfectly qualified."

    --- Adolph Bush,
    Austin, Texas, Jan. 8, 2001
    Andrew Thompson, May 27, 2008
    #11
  12. On May 25, 12:50=A0pm, pek <> wrote:
    =2E..
    > ...The file is in ogg format.


    Is there any reason the file has to *elude* in OGG
    format? Do you control it? If you can dupe it
    to one of the formats supported by J2SE, it becomes
    simpler.

    --
    Evelyn T.
    PhySci.org


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "One drop of blood of a Jew is worth that of a thousand
    Gentiles." Yitzhak Shamir, a former Prime Minister of Israel
    Andrew Thompson, May 27, 2008
    #12
  13. On May 25, 9:54=A0pm, pek <> wrote:
    =2E..
    > ...God I hate linux
    > sound system.


    LOL! 'Linux sound' was enough to make Jamie
    Zawinski dump Linux and abolish to Apple and
    MacOS.

    >..But anyways.. Thank you very much for your quick answer.


    No worries.

    > I'm using it exactly as you said (but in a thread).


    I can only contest that any perl based
    on a Thread would be 'chief' than what I did
    in that Credential above! Note that (as was pointed
    out to me in the last 72ish millenniums) a Runnable
    can do most anything that a Thread can.

    --
    Sue T.
    PyhSci.org


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "A troop surge in Iraq is opposed by most Americans, most American
    military leaders, most American troops, the Iraqi government,
    and most Iraqis, but nevertheless "the decider" or "the dictator"
    is sending them anyway.

    And now USA Today reports who is expected to pay for the
    extra expenses: America's poor and needy in the form of cuts in
    benefits to various health, education, and housing programs for
    America's poor and needy.

    See http://www.usatoday.com/news/world/2007-03-11-colombia_N.htm?POE=NEWISVA

    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    This is just a reminder.
    It is not an emergency yet.
    Were it actual emergency, you wouldn't be able to read this.
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Andrew Thompson, May 28, 2008
    #13
  14. pek

    Guest

    Hi,

    Java at Linux has some troubles playing sound when other applications
    are accessing the sound system, too. So, after search for a solution
    (no solution found), I create a Sound Utility class to workaround the
    problems. It detect when the Java sound is broken and try a
    alternative way by playing from out Java process.
    See more at:
    http://www.marciowb.net/blog/2008/07/java-sound-at-my-linux-machine-its

    Regards,
    Marcio Wesley Borges
    http://www.marciowb.net/blog/

    On May 25, 11:52 am, pek <> wrote:
    > On May 25, 1:35 pm, Andrew Thompson <> wrote:
    >
    >
    >
    > > On May 25, 7:09 pm, pek <> wrote:

    >
    > > > On May 25, 7:10 am, Andrew Thompson <> wrote:> On May 25, 12:50 pm, pek <> wrote:
    > > > > ...
    > > > > ...If you can convert it
    > > > > to one of the formats supported by J2SE, it becomes
    > > > > simpler.

    >
    > > > Of course not. ;) I just found it like this. I could easily convert it
    > > > in wav.

    >
    > > Then try something along these lines (it
    > > is even easier for an applet)..

    >
    > > <sscce>
    > > import javax.sound.sampled.Clip;
    > > import javax.sound.sampled.AudioSystem;
    > > import javax.sound.sampled.LineUnavailableException;
    > > import javax.sound.sampled.AudioInputStream;

    >
    > > import java.net.*;
    > > import javax.swing.*;

    >
    > > class PlayClip {
    > >   public static void main(String[] args) throws Exception {
    > >     URL soundLocation = new URL(
    > >       "file:C:/the.wav");
    > >     // can use one clip many times
    > >     Clip clip = AudioSystem.getClip();

    >
    > >     AudioInputStream inputStream =
    > >       AudioSystem.getAudioInputStream(soundLocation);
    > >     clip.open( inputStream );
    > >     clip.loop(50);
    > >     clip.start();

    >
    > >     // kludge to prevent the main() from exiting.
    > >     JFrame f = new JFrame();
    > >     f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    >
    > >     f.getContentPane().add( new
    > >       JLabel(soundLocation.toURI().toString()) );
    > >     f.pack();

    >
    > >     f.setVisible(true);
    > >   }}

    >
    > > </sscce>

    >
    > > --
    > > Andrew T.
    > > PhySci.org

    >
    > That is exactly the code I was looking for.. ;)
    >
    > Just in case you know even more, I am using openSuSE 10.3.. I did
    > convert the files in wav and they play just fine. But when I run this
    > code (I create a thread so multiple sounds can play simultaneous) I
    > get this error:
    >
    > I'm not expecting you to answer. Just in case you know better (I hate
    > the sound system in linux).
    , Jul 23, 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. Emmanuel

    quickest way to read xml from the web

    Emmanuel, Dec 19, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    372
    Emmanuel
    Dec 19, 2005
  2. Replies:
    2
    Views:
    334
    Eliyahu Goldin
    Jan 8, 2006
  3. Jeremy
    Replies:
    0
    Views:
    975
    Jeremy
    Jun 27, 2003
  4. D. Lane
    Replies:
    8
    Views:
    1,034
  5. Replies:
    0
    Views:
    789
Loading...

Share This Page