Would you like to tell us how we are doing?

You bet No thanks

Sorry about the red box, but we really need you to update your browser. Read this excellent article if you're wondering why we are no longer supporting this browser version. Go to Browse Happy for browser suggestions and how to update.

Media Keys sample app

The Media Keys sample application is a MIDlet that demonstrates how to respond to media key-press events on a BlackBerry device. Media keys control the playback of music, video, and pictures that are stored on the device. The sample application is designed to accept media key presses while running in either the foreground or background.

The sample application highlights the use of the MediaBehaviourControl interface, which enables a MIDlet to act as a media player. For more information about monitoring media key-press events using the BlackBerry Java API, see the Multimedia Development Guide.

File name



  • contains the constructor that initializes the MediaBehaviourControl interface
  • extends the Canvas class and implements the keyPressed(), keyReleased(), and keyRepeated() methods which handle media key-press events


  • contains constants that specify which media key was pressed


  • contains configuration information about the application
  • includes the RIM-MIDlet-MediaPlayerModeEnabled option to configure the default setting for media player mode

This sample features the following classes:


This interface allows a MIDlet to enable and disable media player mode at runtime. When media player mode is enabled, media key-press events are consumed by that application and not posted to applications running in the background. If more than one application has media player mode enabled, then the application running in the foreground will receive the events. If all applications with this mode enabled are running in the background, then the application that was running in the foreground most recently will receive the events. A MIDlet enables media player mode by invoking MediaBehaviourControl.setMediaPlayerModeEnabled().


An application processes media key-press events using the keyPressed(), keyReleased(), and keyRepeated() methods in the Canvas class. The MediaBehaviourControl generates media key-press events even while the MIDlet is running in the background.

An application determines which key was pressed by examining the keyCode parameter. For more information about keyCode values that are used by the Canvas class, see the API reference for the BlackBerry Java Development Environment.

Install the sample app

In Eclipse, on the File menu, click Import.

In the Import dialog box, expand the BlackBerry folder.

Click Import BlackBerry Samples.

Click Next.

Perform one of the following actions:

  • To specify a specific JRE, select the Use a project specific JRE option.
  • To specify the default JRE in the workspace, select the Use default JRE option.

In the BlackBerry Projects dialog box, click Deselect All.

In the BlackBerry Projects dialog box, select the check box beside the MediaKeysDemoMIDlet project.

Click Finish.

The sample app is also available in github.

Run the sample app

To interact with this sample application, press the media keys on your BlackBerry Smartphone Simulator. Media keys are often located on the sides and top of the BlackBerry device. Your BlackBerry Smartphone Simulator should have keys for volume control (Volume Up key, Volume Down key, and Mute key). It might also have a Play/Pause key, Next key, and Previous key.

  1. In Eclipse, in the Navigator window, right-click the MediaKeysDemoMIDlet folder.
  2. Click Run As > BlackBerry Simulator
  3. If neccessary, on the Home screen of the BlackBerry Smartphone Simulator, click the Downloads folder.
  4. Click the Media Keys Demo MIDlet icon.
  5. Press the media keys that are available on your BlackBerry Smartphone Simulator.
    • When you press a media key, the name of the key is displayed on the screen and printed to standard output.
  6. Press the Menu key.
  7. Click Switch Application.
  8. Click another application.
  9. Press a different media key than the last key displayed in step 5.
  10. Press the Menu key again.
  11. Click Switch Application.
  12. Click the Media Keys Demo MIDlet icon.
    • The name of the media key you pressed in step 9 is displayed on the screen.
  13. Clear the Media Player Mode Enabled check box.
  14. Repeat steps 6 to 12.
    • The media key name is no longer displayed on the screen.