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.
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.
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.
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.
- In Eclipse, in the Navigator window, right-click the MediaKeysDemoMIDlet folder.
- Click Run As > BlackBerry Simulator
- If neccessary, on the Home screen of the BlackBerry Smartphone Simulator, click the Downloads folder.
- Click the Media Keys Demo MIDlet icon.
- 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.
- Press the Menu key.
- Click Switch Application.
- Click another application.
- Press a different media key than the last key displayed in step 5.
- Press the Menu key again.
- Click Switch Application.
- Click the
Media Keys Demo MIDlet icon.
- The name of the media key you pressed in step 9 is displayed on the screen.
- Clear the Media Player Mode Enabled check box.
- Repeat steps 6 to 12.
- The media key name is no longer displayed on the screen.