What is the quickest way to play sound?

P

pek

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
 
A

Andrew Thompson

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.
 
P

pek

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 ;)
 
A

Andrew Thompson

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>
 
P

pek

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>

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).
 
P

pek

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();

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).
 
A

Andrew Thompson

...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.
 
P

pek

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!
No worries.


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.
Thanks again.. ;)
 
P

pek

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

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
 
A

Andrew Thompson

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
 
A

Andrew Thompson

...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
 
A

Andrew Thompson

...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.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 
M

marciowb

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/

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);
  }}

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).
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top