Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 86,382 Java Programmers. There are 1,381 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Java Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

How to play audio in applications

 
Reply to this topicStart new topic

How to play audio in applications

foxon177
post 7 May, 2008 - 11:48 PM
Post #1


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 3



How to play audio in applications
Here's a step-by-step guide to playing audio files in a Java application
By Chong Ser Wah and John D. Mitchell, JavaWorld.com, 02/01/97

Playing audio files in Java applications is not officially supported in the current release of Java. But fear not, there is a way! This tip will show you how -- starting with a description of the basic steps involved in playing audio clips in Java applets and then moving on to Java application support.


Playing audio clips in applets is quite simple and involves the following steps:

Create an AudioClip object
Load .au sound file into AudioClip
Play sounds once or loop continuously
Stop playback



Here's how the code for these steps looks:

import java.applet.*;
AudioClip ac = getAudioClip(getCodeBase(), soundFile);
ac.play(); //play once
ac.stop(); //stop playing
ac.loop(); //play continuously




It would seem logical to use this same code to play audio clips in a Java application. Unfortunately, if you do that you will get errors from the compiler. Why? Because the AudioClip object and the getAudioClip() method are part of the java.applet package -- and are not part of applications. The good news is we can dive down and make things work ourselves.

The trick to solving this problem is to use some undocumented features that are provided by Sun in its JDK. Taking a peek inside the classes.zip file from the Sun JDK (using any of the various zipfile utilities), we find not only the standard Java packages such as java.applet but also sun.audio. (These are in the directory sun/audio.)

The sun.audio package contains everything we need to be able to play audio clips! Here's the code:

import sun.audio.*; //import the sun.audio package
import java.io.*;
//** add this into your application code as appropriate
// Open an input stream to the audio file.
InputStream in = new FileInputStream(Filename);
// Create an AudioStream object from the input stream.
AudioStream as = new AudioStream(in);
// Use the static class member "player" from class AudioPlayer to play
// clip.
AudioPlayer.player.start(as);
// Similarly, to stop the audio.
AudioPlayer.player.stop(as);




To use a URL as the audio stream source, substitute the following for the input stream and audio stream setup:

AudioStream as = new AudioStream (url.openStream());




Playing the audio stream continuously adds a bit more complexity:

// Create audio stream as discussed previously.
// Create AudioData source.
AudioData data = as.getData();
// Create ContinuousAudioDataStream.
ContinuousAudioDataStream cas = new ContinuousAudioDataStream (data);
// Play audio.
AudioPlayer.player.play (cas);
// Similarly, to stop the audio.
AudioPlayer.player.stop (cas);




And there you have it. Remember, this technique uses undocumented features; there are no guarantees that it will work with anything but the current Sun JDK.

Author Bio
Chong Ser Wah is a consultant at the Competency Centre for Java in Singapore. Check out the center's Java Cup Competition. Print E-Mail article Feedback Add to del.icio.us Related Article Resources

http://www.hostway.com/domain-name/index.html
http://hi.baidu.com/msdes
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


1lacca
post 8 May, 2008 - 03:53 AM
Post #2


code.rascal

Group Icon
Joined: 11 Aug, 2005
Posts: 3,192

Would you like to post it as a featured article, a snippet, a tutorial, or maybe as a blog article?
In the forums we usually deal with problems and bugs.
And welcome to DIC, I hope you'll enjoy your stay here!
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 03:41AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month