.
.
developers.sun.com java.sun.com
.
    July 15, 2004    

In this Issue

 

Welcome to the Java Technology Fundamentals Newsletter.

This monthly newsletter provides a way for you to learn the basics of the Java programming language, discover new resources, and keep up-to-date on the latest additions to Sun Developer Network's New to Java Programming Center.

- Java Programming Language Basics: Java Audio Basics
- Java Bits: Java Communications API
- Program Challenge: Audio Application
- Sun's Online Web Courses: Advanced Object-Oriented Programming
- Sun's Instructor Led Courses: Managing XML With the Java Platform
- Introducing the Java Interactive Concept Map
- For More Information: Read articles, Tech Tips, trails, and tutorials that provide
more information on the topics discussed here.


For more Java technology content, visit these sites:

java.sun.com - The latest Java platform releases, tutorials, and newsletters.

java.net - A web forum for collaborating and building solutions together.

java.com - The marketplace for Java technology, applications and services.

.

Java Programming Language Basics

Java Audio Basics

The standard Java Runtime Environment (JRE) includes support for playing audio files of type AIFF, AU, MIDI, and WAV. This is done in one of two ways. Applets support both ways, and applications are limited to one.

Basic Applet Support

The applet-specific way to play audio files is through one of the play() methods of the java.applet.Applet class. By providing a URL to play, an applet can play an audio file once.

- public void play(URL url)
- public void play(URL url, String name)

By just providing an URL or base URL and name of file to play, an applet can play an audio file to a user.

Keeping in mind that an unsigned/untrusted applet can only communicate back to the host from which it came, the two argument version of play is the more popular version used.

While you certainly can hard code a specific URL back to the originating host, more frequently you'll see the first argument being either the getCodeBase or getDocumentBase method of Applet. Then, the named file would be located relative to the .class file of the applet with getCodeBase or relative to the .html file that loaded the applet with getDocumentBase.

The following applet demonstrates the use of play and getDocumentBase, to play a sound when an applet is initialized.

import java.applet.*;

public class Play extends Applet {
public void init() {
play(getDocumentBase(), "0.au");
}

Place the following play.html file in the same directory as the file 0.au and load the HTML file in your browser (or with appletviewer).

<applet code=Play width=20 height=20> </applet>

More Advanced Support

The second way of playing sounds is with the AudioClip interface. For both applets and applications, you can call a method of Applet to get an AudioClip instance. Then, by calling one of the three methods of AudioClip, you can play the clip once with play, play the clip in a loop with loop, or stop the looping of the clip with stop.

The Applet class has two methods to get an AudioClip for an applet. They are similar to how the play methods work, passing in either a complete URL or base URL and name:

- public AudioClip getAudioClip(URL url)
- public AudioClip getAudioClip(URL url, String name)

For an application, one is more apt to use the static newAudioClip() method:

- public static final AudioClip newAudioClip(URL url)

For both applets and applications, an instance of the AudioClip interface is returned. You never know what the actual class is, nor do you need to know. Just stick to accessing the clip through the AudioClip interface. Yes, you do need to use a java.applet interface in an application. That is not a typo. It just happens to be where the interface was first located.

After identifying what clip to load and play with either getAudioClip or newAudioClip, you then have a reference to said clip and can play, loop, or stop playing the clip repeatedly.

Here's a simple application that demonstrates the above:

Here's a simple application that demonstrates the above:

import java.applet.*;
import java.io.*;
import java.net.*;

public class PlayClip {
  public static void main(String args[]) throws Exception {
    if (args.length == 0) {
      System.err.println("Specify file name to play");
      System.exit(-1);
    }
    File file = new File(args[0]);
    URL url = file.toURL();
    AudioClip clip = Applet.newAudioClip(url);
    clip.loop();
    // Wait 30 seconds
    Thread.sleep(30000);
    System.exit(0);
  }
}

The loop method does return immediately, playing the audio clip in a background thread, the Java Sound event dispatcher thread. This is like the AWT event dispatcher thread, in that once the Sound event dispatcher is started, your program won't exit, unless explicitly told to. The thread is not a daemon thread, so it keeps your program alive as long as it is running.

That's really all there is to playing sounds on the Java platform. For those interested in some of the more advanced features, be sure to explore the javax.sound.midi and javax.sound.sampled packages.

Test what you learned in this article about Java sound

.
.

Java Bits

Java Communications API

The Java Communications API can be used to write platform-independent communications applications for technologies, such as voice mail, fax, and smart cards. Two versions of the Java Communications API implementations -- 2.0.3 for Solaris/SPARC, and 2.0 for Windows and Solaris x86 -- are available for download. The Java Communications API contains support for RS232 serial ports and IEEE 1284 parallel ports. With updated functionality, developers can:

  • Enumerate ports available on the system
  • Open and claim ownership of ports
  • Resolve port ownership contention between multiple applications
  • Perform asynchronous and synchronous I/O on ports
  • Receive Beans-style events describing communication port state changes

The following FAQ answers some of the most common questions about the Java Communications API:

Q: Will it be part of the JDK?

A: The Java communications API will be a Java Standard Extension, it will not be part of the core JDK.

Q: My application does not find any ports when it enumerates available ports.

Q: BlackBox gives me a message that says "No serial ports found!"

A: In order for the Java communications API to find ports the file javax.comm.properties must be in the correct place. The preferred location is in <jdk>/lib. See the installation instructions for alternate locations and further information.

Q: Can I load my own CommPort drivers, or am I limited to the provided Serial/Parallel drivers?

A: As a result of feedback, we have added this functionality. IMPORTANT: Use the Solaris implementation's comm.jar as a framework. See the CommDriver javadocs for details.

Q: Is there a Linux version of the Java communications API?

A: We do not provide a Linux implementation. But Kevin Hester has written Java communications API drivers for Linux and uses our CommPort driver loading scheme to load his own gnu.io.RXTXCommDriver class. He gave us permission to disclose his web page:

http://www.geeksville.com/~kevinh/Linuxcomm.html
http://wass.homeLinux.net/howtos/Comm_How-To.shtml

Read more

.
.

Program Challenge

Audio Application

Create an application that prompts the user to load an AudioClip from the file system and offers the options to play, loop, or stop the playing. Show the name of the file selected in a label. Be sure that you can't play multiple clips at once.

See a possible solution

.
.

Sun's Web Courses

Advanced Object-Oriented Programming

The Advanced Object-Oriented Programming class introduces students to object interaction, including messaging, association, and composition. The course also describes object-oriented analysis and design using the unified modeling language (UML). Specifically, the course focuses on the use of various UML diagrams including use case, class conceptual, class specification, sequence, and activity.

Find out more

.
.

Sun's Instructor Led Courses

Managing XML With the Java Platform

The Managing XML With the Java Platform course focuses on parsing, managing, and using XML (Extensible Markup Language) documents and data through programs written in the Java programming language. XML is called "portable data", and Java technology is called "portable code". Used together, XML and Java technology are a powerful combination for Internet and Web application solutions.

Find out more

.
.

Introducing the Java Interactive Concept Map<

Navigate the Java technology landscape with the new interactive Java Technology Concept Map. Check out the components and relationships in the Java platform in this large-scale 'concept map' that shows how all the parts of Java interact and support each other.

Find out more

.
.

For More Information

.
.

Downloading the Java 2 Platform

For most Java development, you need the class libraries, compiler, tools, and runtime environment provided with the J2SE development kit.

.
.
Rate and Review
Tell us what you think of the content of this page.
Excellent   Good   Fair   Poor  
Comments:
If you would like a reply to your comment, please submit your email address:
Note: We may not respond to all submitted comments.
.
.

Find archived issues of the following Java technology developer newsletters or manage your current newsletter subscriptions: https://softwarereg.sun.com/registration/developer/en_US/subscriptions

Subscribe to the following newsletters for the latest information about technologies and products in other Java platforms:

- Core Java Technologies Newsletter. Learn about new products, tools, resources, and events of interest to developers working with core Java technologies.
- Mobility Developer Newsletter. Learn about the latest releases, tools, and resources for developers working on wireless and Java Card technologies and applications.
- Core Java Technologies Tech Tips (formerly JDC Tech Tips) Get expert tips, sample code solutions, and techniques for developing in the Java 2 Platform, Standard Edition (J2SE)


You can subscribe to these and other JDC publications on the JDC Newsletters and Publications page

PRIVACY STATEMENT:
Sun respects your online time and privacy. You have received this based on your e-mail preferences. If you would prefer not to receive this information, please follow the steps at the bottom of this message to unsubscribe.


IMPORTANT: Please read our Terms of Use, Privacy, and Licensing policies:
http://www.sun.com/share/text/termsofuse.html
http://www.sun.com/privacy/
http://developer.java.sun.com/berkeley_license.html


Copyright 2004 Sun Microsystems, Inc. All rights reserved. Sun Microsystems, Inc. 10 Network Circle, MPK10-209 Menlo Park, CA 94025 US

FEEDBACK
Comments? Send your feedback on the Java Technology Fundamentals Newsletter to: dana.nourie@sun.com


SUBSCRIBE/UNSUBSCRIBE
- To subscribe, go to the subscriptions page, choose the newsletters you want to subscribe to and click Update
- To unsubscribe, go to the subscriptions page, uncheck the appropriate check box, and click Update


Trademark Information: http://www.sun.com/suntrademarks/
Java, J2EE, J2SE, J2ME, and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.


Sun Microsystems, Inc.
image
image