Chapter 17. Sound

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. Other applications—such as media players, recorders, and editors—are sound-centric and therefore require advanced sound capabilities.

This chapter illustrates Java’s sound capabilities, from the simple to the complex:

  • Example 17-1 shows how to ring the terminal bell. A simple beep is all that some applications need to attract the user’s attention in case of errors or urgent conditions.

  • Example 17-2 shows how to play simple audio clips with the java.applet.AudioClip class. This example illustrates a very simple technique that is sufficient to add sounds to many applications, but does not give any control over the playback of the sound.

  • Example 17-3 shows how to use the javax.sound.sampled and javax.sound.midi packages to load and play sound clips, how to monitor and change the playback position within the clip, and how to set audio parameters such as volume, balance, and tempo for the clips.

  • Example 17-4 shows how to play streaming sounds in both sampled audio and MIDI formats.

  • Example 17-5 reads a simple musical score and converts it into a MIDI Sequence, which is then either played on a Sequencer or saved to a file.

  • Example 17-6 shows how to make music by directly controlling a MidiChannel of a Synthesizer, bypassing the Sequence ...

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.