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 (for a full example, see the example MIDlet's source code):

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 that by default a video control displayed in a Canvas is not visible.

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

In the VideoControl's 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, VideoControl's setDisplayFullScreen(true) method sets the display to the whole Canvas, not the whole screen. The values returned by the getDisplayWidth and 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.

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 and S60 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.