How To: Inline Video Preview

How-to

At last year’s BlackBerry Developer Conference, I hosted a session where I discussed various ways of previewing video content, and now I’d like to share the sample application that allows you to view video within a list-like structure. The application demonstrates how the BlackBerry® video API allows you to add video just like a field within your UI manager. I have included the project and its source code as an example.

The sample takes video from your memory card and displays them as a list. On focus, it starts playing inline, and stops when the focus is away. There are three simple steps to achieving this:

1. Inline Player Creation
a. Create a player using the media manager API
b. Obtain the controls of the player
c. Get a reference of the video field

2. Set relative video view position
a. Set the display position of the field
b. Set height and width of the video field
c. Add this field to the parent container to handle layout.

3. Handle Control Events
a. onFocus schedule a timer to detect Hover state
b. Start the player on Hover
c. onUnFocus stop the player

The setup code is in the InlineVideoManager class:

public InlineVideoManager(Video video, int videoWidth, int videoHeight){
p = Manager.createPlayer (url);
_player.realize();
VideoControl videoControl = (VideoControl) p.getControl("VideoControl");
Field field = (Field) videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,
"net.rim.device.api.ui.Field");
field.setPadding(3, 3, 3,3);
videoControl.setDisplayLocation(3, 3);
videoControl.setDisplaySize(videoWidth, videoHeight);
videoControl.setVisible(true);
……..
super.add(field )
}

Handling focus change events:

public void onFocus(int direction)
{
if(_player != null && _player.getState() == Player.STARTED){
try{
_player.stop();
}catch(Throwable e){}
}

_videoDelayTimer = new Timer();
_videoDelayTimer.schedule(new VideoDelayTimer(),1000);
super.onFocus(direction);
}

To get the entire package of source code, click here.

For the sake of simplicity, I have left the loading of videos from SD card synchronous, which means that if you have a lot of video, it may take some time. This just helps to demonstrate how you can embed video and play inline within your UI manager.

Have you had any experience with embedding video? Let us know in the comments.

About Shiladitya S.

R&D Manager, Consumer Software Development at Research In Motion® (RIM®). His group is responsible for various consumer multimedia applications on the BlackBerry® smartphone as well as the Desktop. Key focus of his group is to deliver rich consumer applications on the BlackBerry platform, and to drive media requirements that enable third parties to do the same. Shiladitya holds a bachelor's and masters degree in electrical engineering from Memorial University of Newfoundland.

Join the conversation

Show comments Hide comments
+ -
blog comments powered by Disqus