Copyright Chris Johnson, 1998.

Human Computer Interface Design Using Java


Exercises 5: Playing MP3 and Other Audio Files

A. Introduction

This exercise is intended to provide some fun and also to provide a brief introduction to Swing/JFC. The fun part is that the program will show you how to load and play audio files over the Internet. This is not done through either AWT or Swing but makes use of Java's seperate audio feedback facilities that are described in the tutorial on audio playback. However, Swing components are used to provide the interface through which the user selects the samples that will be played.

In case you haven't come across them yet, MP3 files are an emmerging strandard for transferring audio files over the Internet. You can buy MP3 players. These are like personal stereos; you download files from the Internet onto the player and can then listen to them as you move around. There are a growing number of MP3 sites such as Although, the following example doesnt explicitly show how to play MP3 files, the Java tutorial says that it is suported (see the tutorial trail and

B. The Audio Player

Here is the audio demonstration that is provided as part of the Sun Java tutorial.

</COMMENT> Your browser is ignoring the APPLET tag.

It consists of the following files:

Other files that you will need include the audio samples. Each of these is intended to show how Java can be used to play the different file formats that are used to store audio information:

C. Your Tasks

Task 1: Copy over and run your own version

Repeat the steps that you have followed in previous exercises by copying over the java files and the audio clips. Compile the applet in the usual way and write your own html file to call the copy of the applet in your local workspace.

Important: because you are making use of Swing, it is good practice to use an EMBED tag rather than the APPLET tag that you have used before in your html files. This ensures that your code can be run in browsers that do not already have Swing support. An explanation for this is given in the Java tutorial. In your html file, you must include the line:

< EMBED type="application/x-java-applet;version=1.2" 
java_CODE ="SoundApplet.class" java_CODEBASE = "../demos/audio"
java_ARCHIVE = "playing.jar" WIDTH = "500" HEIGHT = "200"

Task 2: Find/record your own Audio clip and add it to the program

There are numerous audio samples on the web in the file formats supported by the Applet (auFile, aiffFile, midiFile, rmfFile, wavFile). You can download any one of these and update to play them. Alternatively, if you have access to a machine with a microphone, you can use the SoundRecorder (look under Start->Programs->Accessories->Multimedia on most NT machines) to record your own samples as a .wav format file.

The initial section provide links to MP3 providers - you could extend the original applet to play some of the files offered by these companies. You will need to download an MP3 file and then alter to incorporate the new FileType. Important: please do not infinge copyright restrictions - there is more information about the emmerging debate over MP3 on Your ability to play the sample will depend on whether the Java plugin that this applet calls can recognise the format - it may not work on all browsers.

C. Summary

This exercise has shown you how to play audio samples via an applet. This provides a little light relief from some of the more fundamental AWT topics. However, the exercise should also give you an example of the Swing library and should convince you that you can easily transfer your knowledge of AWT to the wider JFC/Swing components.

Back to the main index...