Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: Java and Sound, Part 1

On systems that support it, sound can be an important part of many applications. Sound can be used to notify the user that her attention is required, to add the extra dimension of aural feedback to visual GUIs, or for entertainment purposes.

Tutorial Details:

The Java Sound API is a low-level interface to sound, and the default implementation does not support compressed formats such as MP3. In these excerpts from the chapter we\'ll be working with simple audio files, with types such as .wav, .aiff, and .au. Note that, although Java Sound supports the recording of sounds, there is no recording example in this chapter: setup and configuration problems on different platforms make sound recording a topic that is beyond the scope of this chapter.

Playing Sounds with AudioClip

The sound capabilities of modern computer hardware are usually much more advanced than the dumb terminals of yesterday, and typical users expect their computers to make sounds that are prettier than the coarse console bell. Java programs can do this by loading and playing a file of audio data with the java.applet.AudioClip interface. As the package name implies, the AudioClip interface was originally intended only for applets. Since Java 1.0, applets have been able to call their getAudioClip( ) instance method to read an audio file over the network. In Java 1.2, however, the static java.applet.Applet.newAudioClip( ) method was added to allow any application to read audio data from any URL (including local file: URLs). This method and the AudioClip interface make it very easy to play arbitrary sounds from your programs, as demonstrated by Example 17-2.

Invoke PlaySound with the URL of a sound file as its sole argument. If you are using a local file, be sure to prefix the filename with the file: protocol. The types of sound files supported depend on the Java implementation. Sun\'s default implementation supports .wav, .aiff, and .au files for sampled sound, .mid files for MIDI, and even .rmf files for the MIDI-related, proprietary \"Rich Music Format\" defined by Beatnik. [1]

When you run the PlaySound class of Example 17-2, you may notice that the program never exits. Like AWT applications, programs that use Java\'s sound capabilities start a background thread and do not automatically exit when the main( ) method returns. [2] To make PlaySound better behaved, we need to explicitly call System.exit( ) when the AudioClip has finished playing. But this highlights one of the shortcomings of the AudioClip interface: it allows you to play( ), stop( ), or loop( ) a sound, but it provides no way to track the progress of the sound or find out when it has finished playing. To achieve that level of control over the playback of sound, we need to use the JavaSound API, which we\'ll consider in the next section.

Example 17-2. PlaySound.java

package je3.sound;

/**
* Play a sound file from the network using the java.applet.Applet API.
*/
public class PlaySound {
public static void main(String[ ] args)
throws java.net.MalformedURLException
{
java.applet.AudioClip clip =
java.applet.Applet.newAudioClip(new java.net.URL(args[0]));
clip.play( );
}
}


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Java and Sound, Part 1

View Tutorial:
Java and Sound, Part 1

Related Tutorials:

Designing with exceptions - JavaWorld - July 1998
Designing with exceptions - JavaWorld - July 1998
 
JDK 1.2 breaks the Java sound barrier - JavaWorld August 2000
JDK 1.2 breaks the Java sound barrier - JavaWorld August 2000
 
Add MP3 capabilities to Java Sound with SPI - JavaWorld November 2000
Add MP3 capabilities to Java Sound with SPI - JavaWorld November 2000
 
Take control of the servlet environment, Part 1 - JavaWorld November 2000
Take control of the servlet environment, Part 1 - JavaWorld November 2000
 
Design for performance, Part 2: Reduce object creation - JavaWorld February 2001
Design for performance, Part 2: Reduce object creation - JavaWorld February 2001
 
Jato: The new kid on the open source block - JavaWorld March 2001
Jato: The new kid on the open source block - JavaWorld March 2001
 
Clean up your wire protocol with SOAP, Part 4 - JavaWorld July 2001
Clean up your wire protocol with SOAP, Part 4 - JavaWorld July 2001
 
Talking Java! - JavaWorld August 2001
Talking Java! - JavaWorld August 2001
 
Web services hits the Java scene, Part 1
Web services hits the Java scene, Part 1
 
XML documents on the run, Part 1
XML documents on the run, Part 1
 
Implement Design by Contract for Java using dynamic proxies
Implement Design by Contract for Java using dynamic proxies
 
Master J2ME for live data delivery
Master J2ME for live data delivery
 
Good introduction to JDO
Good introduction to JDO
 
QTJ Audio
QTJ Audio QuickTime Java can be the heart and soul of cross-platform video players and editors QTJ is also well-suited to be the engine of audio-only applications, such as MP3 players. an audio player, QTBebop, that displays song metadata, band levels,
 
Fixing the Java Memory Model, Part 1
JSR 133, which has been active for nearly three years, has recently issued its public recommendation on what to do about the Java Memory Model (JMM).
 
Java and Sound, Part 1
On systems that support it, sound can be an important part of many applications. Sound can be used to notify the user that her attention is required, to add the extra dimension of aural feedback to visual GUIs, or for entertainment purposes.
 
Java Development on Eclipse, Part 1
Java Development on Eclipse, Part 1 Author\'s note: In part one of a two-part series of excerpts from Eclipse\'s Chapter 2, we\'ll get down to the business of developing Java using Eclipse. We\'re going to take a look at using Eclipse for Java developm
 
new Version 0.9.3 of the Quake2 Java port Jake2
With the new Jake2 release 0.9.3 savegames are working. So you can take a break on your mission.
 
We are providing Downloadable Version of K12LTSP Linux
We are providing Downloadable Version of K12LTSP Linux K12LTSP Linux Now Available Linux K12LTSP 4.1.0 CD's We are providing the free downloadable version of K12LTSP 4.1.0, which is distributed under GNU public license. You have to pay only for
 
We are providing Knoppix 3.7 Live Linux CD's
We are providing Knoppix 3.7 Live Linux CD's Knoppix Linux CD's Now Available Linux Knoppix 3.7 CD's What is KNOPPIX? KNOPPIX is a bootable Linux CD with a collection of various GNU/Linux software. It auto-detects hardware and supports many
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.