aspose file tools*
The moose likes Java in General and the fly likes Play sound in Applications Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Play sound in Applications" Watch "Play sound in Applications" New topic
Author

Play sound in Applications

Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
How can I play a sound file (wav) in an application ???
Please help...
Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
PLEEEEAAAASEEEEE...
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Well, assuming that the user has a tool that will PLAY a .wav file (big assumption, platform dependant ) and has a mime type set up for .wav you can just execute it using
Runtime.getRuntime().exec("whatever.wav");

"JavaRanch, where the deer and the Certified play" - David O'Meara
Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
Thanks a lot, but I can't do this in a game. The file should play immediately.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
There is some discussion of this, with code examples in the "Other Java APIs" forum at http://www.javaranch.com/ubb/Forum45/HTML/000116.html


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
Thanks again. This is a very bad way. I think it should going easier. I just want to play a sound file. Why must this so difficult? This I don't like in Java.
I will try it. The next week I have no time.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
You were thinking that it would play by magic in C++ or Visual Basic???
It didn't look so hard to me, and it's better than my idea.
Art Metzer
Ranch Hand

Joined: Oct 31, 2000
Posts: 241
Hi, Brian.
I don't know if this will help you or not, but have a look at Karl Hornell's technical notes regarding one of his music-playing applets.
Good luck.
Art
Steffen Foldager
Ranch Hand

Joined: Mar 22, 2001
Posts: 58
Use Sun's mediaplayer bean. Downloadable from their website. Plays wav's, mp3's, mpeg's you name it.
Once you get the package installed, it is very straight forward to work with. Here's a little snippet of my code for a ultra-thin jukebox, made in VAJ 3.5, after having imported the javax.media package:
setMpb(new javax.media.bean.playerbean.MediaPlayer());
getMpb().setMediaLocation("file:///"+directory+file);
getMpb().start();
This translates to something like this in a non-IDE environment(with imports and classpath correct):
Mediaplayer mp = new Mediaplayer();
mp.setMediaLocation("file:///"+directory+file);
mp.start();


Steffen Foldager<p>Sun Certified Java Programmer<br />Sun Certified Web Component Developer
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
Brain (Should that be "Brian"?),
The AudioClip interface is what you want. It has three methods:

play()
loop()
stop()

I'm pretty sure that the only supported format for sound files is .au, although this may have changed in later versions of the JDK. It is a simple matter to save a .wav file as .au though.
You can have multiple AudioClip objects playing at the same time, and they will be mixed together.
Unfortunately, the sound is not actually loaded until the first time you call play(), so there is a slight delay that first time. But not on successive plays. You may be able to avoid the delay by using a MediaTracker object...
Look both of these up in the API for more.

------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA

  • [This message has been edited by ryan burgdorfer (edited March 22, 2001).]


<UL TYPE=SQUARE><I><LI>Ryan Burgdorfer<BR><LI>Java Acolyte</I></UL>
Smilidon Sapiens
Ranch Hand

Joined: Nov 02, 2000
Posts: 66
Thanks all of you.
I still have two questions. Then I use the beans, can I start the application still on every system without installing more then the jre? The audioclip is only for applets, or? If I try it on an application it is a bit difficult, right ???
Best regards.
Steffen Foldager
Ranch Hand

Joined: Mar 22, 2001
Posts: 58
AudioClip only plays sun's .au format, which is a pretty poor 8-bit one. And yes, once you get the jre installed right, it's pretty straght forward from there. (That, and finding the damn jars, was the hard part for me).
All the info you need at: http://java.sun.com/products/java-media/jmf/2.1.1/playerbean.html
Have fun..
Steffen Foldager
Ranch Hand

Joined: Mar 22, 2001
Posts: 58
Ahem, that's the jre for the MediaPlayerBean, off course.
ryan burgdorfer
Ranch Hand

Joined: Jan 24, 2001
Posts: 219
AudioClip should work the same in both applets and applications, I believe...
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
Nick George
Ranch Hand

Joined: Nov 17, 2003
Posts: 38
for the preservation of my sanity, could someone please post a link to the place at Sun where i can download MediaPlayer?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Play sound in Applications
 
Similar Threads
playing sound from jar
Playing sounds in Apps.
Java Sound API
Sound in frame
Sound Recording in Java