Play a video in a UI field in a BlackBerry device application

  1. Import the required classes and interfaces.
    import net.rim.device.api.ui.*;
    import net.rim.device.api.ui.component.*;
    import net.rim.device.api.ui.container.*;
    
    import java.io.*;
    import javax.microedition.media.*;
    import javax.microedition.media.control.*;
    
  2. Create the application framework by extending the UiApplication class. In main(), create an instance of the new class and invoke enterEventDispatcher() to enable the application to receive events. In the application constructor, invoke pushScreen() to display the custom screen for the application. The VideoPlaybackDemoScreen class, which is described in step 3, represents the custom screen.
    public class VideoPlaybackDemo extends UiApplication
    {
         public static void main(String[] args)
         {
              VideoPlaybackDemo app = new VideoPlaybackDemo();
              app.enterEventDispatcher();
         }
        
         public VideoPlaybackDemo()
         {
              pushScreen(new VideoPlaybackDemoScreen());
         }
    }
  3. Create the framework for the custom screen by extending the MainScreen class.
    private class VideoPlaybackDemoScreen extends MainScreen
    {	
         public VideoPlaybackDemoScreen()
         {
         }
    }
  4. In the screen constructor, in a try/catch block, create an instance of the Player class by invoking Manager.createPlayer(String), passing in the location of the video file to play.
    try 
    {
        Player player = javax.microedition.media.Manager.createPlayer("file:///SDCard/BlackBerry/videos/soccer1.avi");
       
    }
    catch(MediaException me)
    {
        Dialog.alert(me.toString());
    }
    catch(IOException ioe)
    {
        Dialog.alert(ioe.toString());
    }
  5. To control an aspect of playback, retrieve the appropriate Control object. First, invoke the Player object's realize() method to access its associated Control object. Next , invoke Player.getControl() passing in the string, VideoControl, to retrieve the VideoControl object that is associated with Player.
    try 
    {
        Player player = javax.microedition.media.Manager.createPlayer("file:///SDCard/BlackBerry/videos/soccer1.avi");
        player.realize();
        VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
    }
    catch(MediaException me)
    {
        Dialog.alert(me.toString());
    }
    catch(IOException ioe)
    {
        Dialog.alert(ioe.toString());
    }
  6. Invoke VideoControl.initDisplayMode(int mode, Object arg) passing an arg parameter specifying the UI primitive that displays the video to initialize the mode that a video field uses. Cast the returned object as a Field object.
    Note: You can invoke initDisplayMode() in different ways to return a Field , an Item for use with MIDlets, or to display a video on a Canvas class.
    try 
    {
        Player player = javax.microedition.media.Manager.createPlayer("file:///SDCard/BlackBerry/videos/soccer1.avi");
        player.realize();
        VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
        Field videoField = (Field)videoControl.initDisplayMode( VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field" );
    
    }
    catch(MediaException me)
    {
        Dialog.alert(me.toString());
    }
    catch(IOException ioe)
    {
        Dialog.alert(ioe.toString());
    }
  7. Invoke add() to add the returned Field object to your Screen or Manager, as you would add any other component to your UI.
    try 
    {
        Player player = javax.microedition.media.Manager.createPlayer("file:///SDCard/BlackBerry/videos/soccer1.avi");
        player.realize();
        VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
        Field videoField = (Field)videoControl.initDisplayMode( VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field" );
    
        add(videoField);
    }
    catch(MediaException me)
    {
        Dialog.alert(me.toString());
    }
    catch(IOException ioe)
    {
        Dialog.alert(ioe.toString());
    }
  8. To set the volume of playback, invoke Player.getControl() passing in the string, VolumeControl, to retrieve the VolumeControl object that is associated with Player. Invoke VolumeControl.setLevel() to specify the volume of playback.
    try 
    {
        Player player = javax.microedition.media.Manager.createPlayer("file:///SDCard/BlackBerry/videos/soccer1.avi");
        player.realize();
        VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
        Field videoField = (Field)videoControl.initDisplayMode( VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field" );
    
        add(videoField);
    
        VolumeControl volume = (VolumeControl) player.getControl("VolumeControl");
        volume.setLevel(30);
    }
    catch(MediaException me)
    {
        Dialog.alert(me.toString());
    }
    catch(IOException ioe)
    {
        Dialog.alert(ioe.toString());
    }
  9. Invoke Player.start() to start playback.
    try 
    {
        Player player = javax.microedition.media.Manager.createPlayer("file:///SDCard/BlackBerry/videos/soccer1.avi");
        player.realize();
        VideoControl videoControl = (VideoControl) player.getControl("VideoControl");
        Field videoField = (Field)videoControl.initDisplayMode( VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field" );
    
        add(videoField);
    
        VolumeControl volume = (VolumeControl) player.getControl("VolumeControl");
        volume.setLevel(30);
    
        player.start();
    }
    catch(MediaException me)
    {
        Dialog.alert(me.toString());
    }
    catch(IOException ioe)
    {
        Dialog.alert(ioe.toString());
    }
Previous topic: Playing video

Was this information helpful? Send us your comments.