p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   Java Basics (http://p2p.wrox.com/java-basics-199/)
-   -   Adding sound to a java application (http://p2p.wrox.com/java-basics/38368-adding-sound-java-application.html)

john111 February 12th, 2006 02:11 PM

Adding sound to a java application
 
Hi, Can anybody explain to me how i can add sound to a stock control system. For example if somebody presses the logout button i want to play a sound. All help will be greatly appreciated. Thanks.


longjava February 12th, 2006 06:36 PM

There are two ways you can play a sound file:

The simple way is to use a static method newAudioClip() of an Applet class to get an instance of AudioClip, then invoke the play method.
For example:
JButton logout = new JButton( "Logout" );
logout.addActionListener( new ActionListener()
{
   public void actionPerformed( ActionEvent evt )
   {
      // play a sound before loging out
      try
      {
         java.applet.AudioClip clip =
            java.applet.Applet.newAudioClip(new java.net.URL( "file:/logout.wav"));
         clip.play();

         // loging out.....
      }
       catch( Exception e ) {}
   }
});


The alternative way, which is a right way to do, is using the Java Sound API. You need to learn the following classes of Java Sound API in order to play a sound file successfully:
- AudioInputStream
- AudioFormat
- DataLine
- SourceDataLine

Hope this help.



RahulSapkal February 15th, 2006 06:31 PM

Look at these references,

Playing sound in Java Applet,
http://www.javareference.com/jrexamp...mple.jsp?id=20

http://www.javareference.com/mvnforu...ead?thread=259

Hope this helps,
Rahul

rahul@javareference.com
--------------------------
http://www.javareference.com


All times are GMT -4. The time now is 11:50 AM.

Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.