|
Question of the Week presents answers to key questions posed by the
developer community. The intent is to pass this
important, but not always easy-to-find information on to JDC members.
The questions are selected from the JDC newsgroups generally because:
they are frequently asked, they are significant or timely, or their answers are not easily
accessible.
Note: If you have a question to which you need an answer, try the
JDC newsgroups. You can read through
the existing newsgroups, or with your free
JDC membership, you can post new messages or threads.
Question of the Week
Archive
Topic: Using the JavaTM 2 Sound API.
Question:
How can I use the NewAudioClip method to read a local audio file?
I'm wondering how to use the NewAudioClip(URL) method to read a
sound file into an application without having to use import java.net.
I'm also wondering if there is a way to track the sound loading so I can be
sure it's available to play.
Answer:
There is now a convenience method to convert File objects to 'file:' URL. However,
you still have to catch and handle any MalformedURLExceptions, for example:
File f = new File("mysound.au");
AudioClip theSound;
try {
theSound = Applet.newAudioClip(f.toURL());
} catch (java.net.MalformedURLException e) {
theSound = null;
}
if (theSound != null) {
theSound.play();
}
If you have a small application and the play method is your
last line you might need to put in a sleep statement after loading the audio file.
This allows the sound to be loaded before the application exits.
Thread.currentThread().sleep(100000);
Many thanks to JDC members akw5c and kriff for contributing to this answer.
Sat Dec 19 01:13:51 PST 1998
Terrasea
That sure beats using:
URL url = new URL("file:" + System.getProperty("user.dir") + "/" + "Ohdear3.wav");
It is much easier to remember for a start, and it's less code to write.
Sat Dec 19 15:16:02 PST 1998
therogman
I can't download the jdc 1.2.I keep on getting a corrupt file cabinet.Can you help a begginer who wants to learn java.Thank you!
Sun Dec 20 17:32:41 PST 1998
Anonymous
</font>
Mon Dec 21 11:42:26 PST 1998
dasilva
It is important to emphasize that the line "Thread.currentThread().sleep(100000);"
should be inserted after the theSound.play() statement otherwise it has no effect at all !
Mon Dec 21 14:42:53 PST 1998
kombat
Is there a way to do it using streams? I am
interested in being able to create an audio
clip from the actual bytes that are retrieved
from, say, an object database. Any ideas?
Wed Dec 23 02:32:19 PST 1998
micifer
How do I ask the user for two values and prints each question on the next command line?
Wed Dec 23 11:40:21 PST 1998
BQ1
Can you play any soundfiles other than au-files?
i.e. wav, mp3 or mid
Thu Dec 24 03:25:22 PST 1998
marcelnijman
Why can't I play audio files from a stream
(for example a GZIPInputStream)? That would
be very convenient. And why can't I put my
next audio file in a queue so that it will
be started exactly at the moment when the
current audio file is finished? I need both
these features for my application. (14 hours
of zipped audio files on one CD, which I want
to play one after the other without any delay.)
Fri Dec 25 07:01:05 PST 1998
ramachand_ganesh
Respected sirs,
I am unable to launch my own applications using the
Runtime.getRuntime() command in Windows '98 but
works perfectly in other Operating Systems. Kindly
Help.
Yours truly,
Ramachandara Ganesh
Mon Dec 28 10:43:44 PST 1998
SnYPeR
I think this will help a lot of people who are interested in placing sounds in their applications
Tue Dec 29 15:33:03 PST 1998
26066015france
How about informations on how to convert .wav to .au file?
Tue Jan 05 01:46:41 PST 1999
rodf
This is great but it doesnt answer one of the questions.
How do you know if the sound has loaded successfully?
How do you know HOW MUCH of the sound has loaded (to do a progress bar).
And now some more questions.
How do you know how much of the sound has played and when
it has finished?
Tue Jan 05 18:54:06 PST 1999
ashwarya
How can i use Java to Capture Video of the video capture devices on the system.
Thanks
Wed Jan 06 17:42:09 PST 1999
adamskip
I have an algorithm that manipulates an array of unsigned long integers. The Java long type may cause some unintended behaviour due to its MSB sign bit. Is there a way in Java to implement arrays of unsigned integers (DWORD in Windows terminology)?
Thanks in advance!
Piotr
Thu Jan 07 21:26:07 PST 1999
anithareddy
Please give me detailed description about this
SoundAPI,how to start coding for this,how to call
au files e.t.c.,i am new to this Sound API.Please suggest
where to refer.
Thu Jan 07 21:26:14 PST 1999
Anonymous
Please give me detailed description about this
SoundAPI,how to start coding for this,how to call
au files e.t.c.,i am new to this Sound API.Please suggest
where to refer.
Fri Jan 08 02:21:37 PST 1999
bf7hp04
To run a Java application program what all files we nned to check in operating system??
Shaila
ssonawane@hotmail.com
THE INFORMATION PROVIDED IN THE JAVA DEVELOPER'S CONNECTION IS PROVIDED 'AS IS'
WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT
LIMITATION THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR, AND
NON-INFRINGEMENT.
THIS INFORMATION IS PROVIDED AT NO CHARGE. BY USE OF THE INFORMATION YOU AGREE
THAT NEITHER SUN MICROSYSTEMS, INC. NOR ITS LICENSORS WILL BE LIABLE FOR ANY
DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF
BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF SUN OR ITS LICENSORS HAVE BEEN
ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR
LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING
LIMITATION MAY NOT APPLY.
THIS INFORMATION COULD INCLUDE TECHNICAL INACCURACIES OR TYPOGRAPHICAL ERRORS.
CHANGES ARE PERIODICALLY ADDED TO THE INFORMATION HEREIN. SUN MICROSYSTEMS, INC.
MAY MAKE IMPROVEMENTS AND OTHER CHANGES IN THE PRODUCTS AND THE PROGRAMS
DESCRIBED HEREIN AT ANY TIME WITHOUT NOTICE.
|