Listen to Me

The five states give a big picture of what your media is doing, but sometimes you'll want more granularity than that. It might be nice to know when you have recorded 30 seconds of audio, or when a stream has run out of data and needs to buffer.

RIM supports these use cases by offering a standard listener interface. By implementing PlayerListener, your application can register with a Player instance, as shown in Figure 2-3. PlayerListener defines a single method, playerUpdate(), which will be invoked whenever something interesting happens.

Figure 2-3. Registering a Listener with a Player

Updates are tagged as Strings, with additional information optionally provided in an accompanying Object. This flexible design allows manufacturers to define their own event types without requiring changes to the interface. MMAPI defines a broad set of standard event names and makes them available as public fields in the PlayerListener interface. You will need to add references to any RIM-specific event types in your own code. Table 2-1 shows the Player events that can be generated during media capture.

Table 2-1. Player Events During Recording

String

Definition

Meaning

EventData Type

EventData Value

recordError

RECORD_

ERROR

An error occurred while recording. Call setRecordLocation or setRecordStream on the RecordControl to try again.

String

Detailed error message

recordStarted

RECORD_

STARTED

Recording has begun.

Long

Time

recordStopped

RECORD_

STOPPED

Recording has stopped.

Long

Time

net.rim.device.internal. media.recordCommitted

N/A

Recording commit completed.

N/A

N/A

Listing 2-1 implements a basic listener class that provides updates about the current status of a recording operation. However, a similar class could perform additional tasks as well, such as automatically restarting capture if an error occurs.

Listing 2-1. A Status Update PlayerListener import javax.microedition.media.*;

import net.rim.device.api.ui.component.LabelField;

public class RecordingListener implements PlayerListener {

private LabelField status;

public RecordingListener(LabelField status) {

this.status = status;

public void playerUpdate(Player source, String event, Object data) {

if (event.equals(PlayerListener.RECORD_STARTED))

status.setText("Recording started..."); else if (event.equals(PlayerListener.RECORD_STOPPED))

status.setText("Recording stopped..."); else if (event.equals(PlayerListener.RECORD_ERROR)) status.setText("Uh-oh! Error:" + data);

else if (event.equals("net.rim.device.internal.media.recordCommitted")) {

status.setText("Recorded data saved.");

else if (event.equals("net.rim.device.internal.media.recordCommitted")) {

status.setText("Recorded data saved.");

Attach the PlayerListener to the Player, and you will start receiving updates automatically.

player.addPlayerListener(new RecordingListener(status));

Adding a PlayerListener is purely optional. Most advanced applications will want to receive these sorts of notifications so they can update the user and take other actions, but for simple operations, you'll be fine sticking with the basic Player API

0 0

Post a comment

  • Receive news updates via email from this site