Monday, March 9, 2009

How To : Play video within a BlackBerry smartphone application

How To In BlackBerry Device Software 4.3.0 and later and BlackBerry JDE 4.3.0 and later, video playback within a BlackBerry smartphone application is supported that allows the application to control video playback (play, pause, etc.).

The following code sample plays back a video called Trailer.avi from the Trailers directory on the microSD card on the BlackBerry smartphone. You can open video content from any valid URL. The sample code can also be downloaded here.

Alternatively, you can play video on any BlackBerry smartphone that supports video playback (BlackBerry Device Software prior to 4.3.0) by launching the Media application on the BlackBerry smartphone. To do so, invoke the BlackBerry® Browser with a URL that points to your video content. The URL could point to a web server or to a file located on the microSD card on the BlackBerry smartphone (for instance, "file:///SDCard/yourDirectory/yourFile.avi" or http://yourServer.com/yourFile.avi).

See Here for information on how to invoke the BlackBerry Browser.

import javax.microedition.media.Player;
import javax.microedition.media.Manager;
import javax.microedition.media.control.VideoControl;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.system.Characters;


public final class PlayVideo extends UiApplication
{
private Player player;
private VideoControl videoControl;
public static void main(String[] args)
{
PlayVideo theApp = new PlayVideo();
theApp.enterEventDispatcher();
}
public PlayVideo()
{
MainScreen ms = new MainScreen(){
public boolean onClose()
{
//Clean up the player resources.
player.close();
videoControl.setVisible(false);
close();
return true;
}
//Override keyChar to capture key commands used to control video playback.
protected boolean keyChar(char c, int status, int time)
{
boolean retVal = false;
if (c == Characters.SPACE)
{
if (player.getState() == Player.STARTED)
{
//Stop playback.
try
{
player.stop();
}
catch (Exception ex)
{
System.out.println("Exception: " + ex.toString());
}
}
else
{
//Start playback.
try
{
player.start();
}
catch (Exception ex)
{
System.out.println("Exception: " + ex.toString());
}
}
retVal = true;
}
return retVal;
}
};
ms.setTitle(new LabelField("Let's play some video..."));
LabelField lf = new LabelField("Press space to start/stop/resume playback.");
ms.add(lf);
pushScreen(ms);
try
{
//Create a new Player pointing to the video file.
//This can use any valid URL.
player = Manager.createPlayer("file:///SDCard/Trailers/Trailer.avi");
player.realize();
//Create a new VideoControl.
videoControl = (VideoControl)player.getControl("VideoControl");
//Initialize the video mode using a Field.
videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
//Set the video control to be visible.
videoControl.setVisible(true);
}
catch (Exception ex)
{
System.out.println(ex.toString());
}
}
}

No comments:

Post a Comment

Place your comments here...