Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > What is the quickest way to play sound?

Reply
Thread Tools

What is the quickest way to play sound?

 
 
pek
Guest
Posts: n/a
 
      05-25-2008
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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      05-25-2008
On May 25, 12:50*pm, pek <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      05-25-2008
On May 25, 12:50*pm, pek <(E-Mail Removed)> 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
 
Reply With Quote
 
pek
Guest
Posts: n/a
 
      05-25-2008
On May 25, 7:10 am, Andrew Thompson <(E-Mail Removed)> wrote:
> On May 25, 12:50 pm, pek <(E-Mail Removed)> 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
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-25-2008
On May 25, 7:09*pm, pek <(E-Mail Removed)> wrote:
> On May 25, 7:10 am, Andrew Thompson <(E-Mail Removed)> wrote:> On May 25, 12:50 pm, pek <(E-Mail Removed)> 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
 
Reply With Quote
 
pek
Guest
Posts: n/a
 
      05-25-2008
On May 25, 1:35 pm, Andrew Thompson <(E-Mail Removed)> wrote:
> On May 25, 7:09 pm, pek <(E-Mail Removed)> wrote:
>
> > On May 25, 7:10 am, Andrew Thompson <(E-Mail Removed)> wrote:> On May 25, 12:50 pm, pek <(E-Mail Removed)> 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).
 
Reply With Quote
 
pek
Guest
Posts: n/a
 
      05-25-2008
On May 25, 2:52 pm, pek <(E-Mail Removed)> wrote:
> On May 25, 1:35 pm, Andrew Thompson <(E-Mail Removed)> wrote:
>
>
>
> > On May 25, 7:09 pm, pek <(E-Mail Removed)> wrote:

>
> > > On May 25, 7:10 am, Andrew Thompson <(E-Mail Removed)> wrote:> On May 25, 12:50 pm, pek <(E-Mail Removed)> 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).
 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      05-25-2008
On May 25, 9:54*pm, pek <(E-Mail Removed)> 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
 
Reply With Quote
 
pek
Guest
Posts: n/a
 
      05-25-2008
On May 25, 3:28 pm, Andrew Thompson <(E-Mail Removed)> wrote:
> On May 25, 9:54 pm, pek <(E-Mail Removed)> 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..
 
Reply With Quote
 
pek
Guest
Posts: n/a
 
      05-25-2008
On May 25, 5:04 pm, Lew <(E-Mail Removed)> 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
 
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
Quickest,effective way of fetching data drewdr@yahoo.com ASP .Net 2 01-08-2006 08:02 AM
quickest way to read xml from the web Emmanuel ASP .Net 2 12-19-2005 10:04 PM
Re: quickest way to a programming job D. Lane Java 8 07-10-2003 01:00 AM
Re: quickest way to a programming job brougham3@yahoo.com Java 0 06-28-2003 02:45 AM
Re: quickest way to a programming job Jeremy Java 0 06-27-2003 06:05 PM



Advertisments