Java Reference
In-Depth Information
CHAPTER 16
■ ■ ■
Sound, Music, and
Video: MMAPI
M IDP includes basic audio capabilities. The media APIs are a strict subset of the Mobile
Media API (MMAPI), a more general API for multimedia rendering. The full details on MMAPI
are in JSR 135:
http://jcp.org/jsr/detail/135.jsp
The MMAPI itself is kind of a pint-sized version of the Java Media Framework (JMF), which
is an optional package for J2SE. More information on the JMF is available here:
http://java.sun.com/products/java-media/jmf/
The subset of the MMAPI that is included is called the Audio Building Block (ABB). It
includes the capability to play simple tones and sampled audio. The ABB is implemented in
the javax.microedition.media and javax.microedition.media.control packages. This chapter
covers all of MIDP's ABB support and the most commonly found features of MMAPI—including
video handling. It begins with a rush of runnable code, and then backs off to explain some of
the concepts and dig into the APIs more carefully.
Quick Start
You can play tones by calling this method in javax.microedition.media.Manager :
public static void playTone(int note, int duration, int volume)
In this method, note is specified just like a MIDI note, where each integer corresponds to a
single key on a piano keyboard. Middle C is 60, and the A above middle C (a 440 Hz tone) is 69.
The duration is in milliseconds, and volume can range from 0 , silent, to 100 , loudest.
Like most other methods in the ABB, playTone() may throw a MediaException . Although
support for simple tones is required by the specification, the device may be temporarily unable
to play tones. (For example, a mobile phone might be using the tone generation hardware to
ring the phone.)
Figure 16-1 shows PianoCanvas , an example that displays a simple piano keyboard and
allows the user to navigate through the keys to play different tones. PianoCanvas is presented in
Listing 16-1. The code for playing the tones is very compact, consisting solely of a call to
playTone() in the keyPressed() method. The rest of the code is devoted to the user interface.
305
Search WWH ::




Custom Search