• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Play sound in Applications

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can I play a sound file (wav) in an application ???
Please help...
 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
PLEEEEAAAASEEEEE...
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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");
 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, but I can't do this in a game. The file should play immediately.
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
 
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Smilidon Sapiens
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahem, that's the jre for the MediaPlayerBean, off course.
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AudioClip should work the same in both applets and applications, I believe...
------------------
  • Ryan Burgdorfer
  • Java Acolyte in
  • Columbus, OH USA
 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
for the preservation of my sanity, could someone please post a link to the place at Sun where i can download MediaPlayer?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic