MIDP 2.0 Media API

MIDP 2.0 includes a subset of the JSR-135, Mobile Media API (MMAPI) in its own packages: javax.microedition.media and javax.microedition.media.control. MIDP 2.0 devices may choose to implement just this subset or the full Mobile Media API. In either case, the phones may support only one sound at a time or support playing several simultaneously.

The feature most useful for games is the ability to play short sound files as sound effects. The MIDP 2.0 Media API specifies no mandatory sound file formats, but it is reasonable to expect at least WAV (8 kHz mono PCM) and MIDI file formats to be supported.

A code sample for playing a sound from a resource file in the MIDlet's JAR file appears below:

try {
    InputStream is = getClass().getResourceAsStream("/dog.wav");
    Player p = Manager.createPlayer(is, "audio/x-wav");
    p.prefetch();
    p.start();
}
catch (IOException ex) {
}
catch (MediaException ex) {
}

The same player can be reused by calling start again. This has no effect if the player is already playing, so to make sure it starts from the beginning you can use code like this:

    p.stop();
    p.setMediaTime(0L);
    p.start();

This approach is used in the Sheepdog game's SoundEffects class. For more information, see section Implementing the SoundEffects class).