|
|||||||||
PREV NEXT | FRAMES NO FRAMES |
This document describes how to use the multimedia capabilities of the BlackBerry® device. It includes the following sections:
To record and play media in your BlackBerry device application, you must use the Player class, which is located in the javax.microedition.media package, and its related resources.
To create an instance of a Player class, you must invoke createPlayer() in the javax.microedition.media.Manager class.
When the Player is first created using one of the createPlayer() methods, it is in an UNREALIZED state. Before you can have access to the associated metadata or controls for the Player, you must invoke the Player object's realize() method. This method transitions the Player to a REALIZED state.
In the REALIZED state, the Player class provides access to associated resources that control playback and recording. You can use the Player object's getControl() and getControls() methods (inherited from Controllable), passing in a String specifying the Control object that you require. You can find the available controls for your media player in the javax.microedition.media.control package.
Before your application closes, you must invoke the Player object's close() method to deallocate any resources the Player has created. You can also do this at any time to free up memory or resources. After you invoke close(), you can no longer use the Player.
For more information about the states of a Player object and how to transition between them, see the API reference for the Player class.
Before you begin playing and recording audio, here is a list of audio formats supported by most BlackBerry devices running OS 4.2 or later:
BlackBerry devices that are running BlackBerry Device Software 5.0 or later also support the following audio formats:
Before your application closes, you must invoke the Player object's close() method to deallocate any resources the Player has created.
Click for code sample: Playing an audio file
try {
Player p = Manager.createPlayer("http://abc.wav");
p.realize();
VolumeControl volume = (VolumeControl) p.getControl("VolumeControl");
volume.setLevel(30);
p.prefetch();
p.start();
}
catch (MediaException pe) {
}
catch (IOException ioe) {
}
|
To start recording audio on a BlackBerry device using the device's microphone, follow these steps:
Click for code sample: Starting recording audio to a file
try {
Player player = javax.microedition.media.Manager.createPlayer("capture://audio?encoding=audio/amr");
player.realize();
RecordControl recordControl = (RecordControl) player.getControl( "RecordControl" );
recordControl.setRecordLocation( "file:///store/home/user/AudioRecordingTest.amr" );
recordControl.startRecord();
player.start();
}
catch( IOException e ) {
}
catch( MediaException e ) {
}
|
To stop recording and save your file, follow these steps:
Click for code sample: Stopping an audio recording and saving it to a file
if (player != null) {
player.close();
player = null;
}
if (recordControl != null) {
recordControl.stopRecord();
try {
recordControl.commit();
}
catch (Exception e) {
}
recordControl = null;
}
|
When your application closes, invoke the Player object's close() method to deallocate any resources that the Player has created.
Click for code sample: Playing a video file
try
{
Player videoPlayer = javax.microedition.media.Manager.createPlayer("http://);
videoPlayer.realize();
// Set up the playback
VideoControl videoControl = (VideoControl) _videoPlayer.getControl("VideoControl");
Field vField = (Field) _videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
add(vField);
VolumeControl volume = (VolumeControl) videoPlayer.getControl("VolumeControl");
volume.setLevel(30);
}
catch( Exception e )
{
Dialog.alert("Exception while initializing the playback video player\n\n" + e);
}
|
To start recording video to a file in your BlackBerry application, follow these steps:
Click for code sample: Starting a vide recording
try
{
Player player = javax.microedition.media.Manager.createPlayer("capture://video?encoding=video/sbv");
player.realize0();
VideoControl videoControl = (VideoControl) _player.getControl("VideoControl");
RecordControl recordControl = (RecordControl) _player.getControl("RecordControl");
// Initialize the video display
Field videoField = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
try
{
videoControl.setDisplaySize( Display.getWidth(), Display.getHeight() );
}
catch( MediaException me )
{
// setDisplaySize is not supported
}
add(videoField);
recordControl.setRecordLocation("file:///store/home/user/VideoRecordingTest.sbv");
recordControl.startRecord();
player.start();
}
catch( Exception e )
{
// Dispose of the player if it was created
if( _player != null )
{
_player.close();
}
_player = null;
deleteAll();
removeAllMenuItems();
VideoRecordingDemo.errorDialog(e.toString());
}
}
|
To stop recording and save your file, follow these steps:
Click for code sample: Stopping a video recording and saving it
if (player != null) {
player.close();
player = null;
}
if (recordControl != null) {
recordControl.stopRecord();
try {
recordControl.commit();
}
catch (Exception e) {
}
recordControl = null;
}
|
Copyright 1999-2011 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved. |
Legal |