×

Playing video

Playing video is similar to playing audio. However, the video player needs to be told where to display the video. Therefore you get a VideoControl from the video player and display it either as a Form item or in a Canvas.

To display video as a Form item:

InputStream is = getClass().getResourceAsStream("/somefile.3gp");
Player player = Manager.createPlayer(is, "video/3gpp");
player.realize();
VideoControl vc = 
    (VideoControl)player.getControl("VideoControl");
if (vc != null)
{
    Item it =
        (Item)vc.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE,
                                 null);
    myForm.append(it);
    p.start();
}

To display video in a Canvas:

InputStream is = getClass().getResourceAsStream("/somefile.3gp");
Player player = Manager.createPlayer(is, "video/3gpp");
player.realize();
VideoControl vc =
    (VideoControl)player.getControl("VideoControl");
if (vc != null)
{
    vc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, myCanvas);
    vc.setVisible(true);
    p.start();
}
Note:

By default, a VideoControl displayed on a Canvas is not visible.

If the VideoControl's display mode is set to USE_DIRECT_VIDEO, the second parameter of the VideoControl.initDisplayMode method can be either a Canvas or an object created from Canvas.

In the VideoControl.setDisplaySize method, the image will be scaled to fit if the requested display size is different from the dimensions of the video clip. In Canvas, the VideoControl.setDisplayFullScreen(true) method sets the display to the whole Canvas, not the whole screen. The values returned by the VideoControl.getDisplayWidth and VideoControl.getDisplayHeight methods are unaffected by this call (that is, they do not afterwards return the width and height of the full screen).

Note:

Video scaling is supported in Series 40 devices from 3rd Edition, Feature Pack 2 onwards.

From Java Runtime 1.0.0 for Series 40 onwards, calling VideoControl.setDisplayFullScreen(true) automatically plays the video in landscape mode.

Saving resources when playing video multiple times

The video content is often large and playing it multiple times in a row consumes a significant amount of memory if a new Player instance is created for each play action. A player can be kept usable, so that playing can be restarted without loading the resources again. If you can assume that the memory of the mobile device does not run out, you can store created and initialized players to a player pool. That makes it faster to restart the video clip, because the content does not need to be reloaded to the memory. In addition, prefetched and realized players are somewhat faster to start.

Note:

From Series 40 3rd Edition onwards, when playing media from a file locator, it will be played directly from the file without loading it to the memory first. This can be useful when playing large media files that already exist in the file system.


Last updated 9 October 2013

Back to top