Wednesday, January 9, 2013

BlackBerry playing video from a web url


We can play a videw from a web link/url in blackberry with the help of MMAPI'S of J2ME. please have a mainscreen with name PlayvideoScreen .java and use the following given code . also copy the above given images to "res" folder


package videoplaying;

import net.rim.device.api.command.Command;
import net.rim.device.api.command.CommandHandler;
import net.rim.device.api.command.ReadOnlyCommandMetadata;
import net.rim.device.api.media.control.AdvancedVideoControl;
import net.rim.device.api.media.control.StreamingBufferControl;
import net.rim.device.api.system.Bitmap;
import net.rim.device.api.system.Display;
import net.rim.device.api.ui.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.image.Image;
import net.rim.device.api.ui.image.ImageFactory;

import javax.microedition.media.*;
import javax.microedition.media.control.*;



public class PlayvideoScreen extends MainScreen implements FieldChangeListener                                                                                          



{
private Player _player;
private static final int VIDEO_WIDTH = Display.getWidth();
private static final int VIDEO_HEIGHT = 200;
    private Player player;

    private VideoControl videoControl;
    ButtonField video ;
    public PlayvideoScreen()
    {
        video = new ButtonField("click me.");

        add(video);

                video.setChangeListener(this);

    }

    public void fieldChanged(Field field, int context)
    {
        if(video == field)
        {
        Field videoField = initializeVideo("http://commonsware.com/misc/test2.3gp"+";deviceside=true");

if (videoField != null) {

// Button for pausing the video
ButtonField pauseButton = new ButtonField("Pause",
ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
pauseButton.setCommand(new Command(new CommandHandler() {
public void execute(ReadOnlyCommandMetadata metadata,
Object object) {
// Pause the video
try {
_player.stop();
} catch (Exception e) {
Dialog.inform(e.getMessage());
}
}
}));

Image pauseImage = ImageFactory.createImage(Bitmap
.getBitmapResource("pause.png"));
pauseButton.setImage(pauseImage);
this.add(pauseButton);

// Button to start/re-start the video
ButtonField playButton = new ButtonField("Play",
ButtonField.CONSUME_CLICK | ButtonField.NEVER_DIRTY);
playButton.setCommand(new Command(new CommandHandler() {
public void execute(ReadOnlyCommandMetadata metadata,
Object object) {
// Start the video
try {
_player.start();
} catch (Exception e) {
Dialog.inform(e.getMessage());
}
}
}));

Image playImage = ImageFactory.createImage(Bitmap
.getBitmapResource("play.png"));
playButton.setImage(playImage);
this.add(playButton);



this.add(videoField);

}
//            try
//            {//
//             //http://a1408.g.akamai.net/5/1408/1388/2005110405/1a1a1ad948be278cff2d96046ad90768d848b41947aa1986/sample_mpeg4.mp4
//                //              player = Manager.createPlayer("file:///SDCard/Blackberry/videos/Swept.mp4");
//                player = Manager.createPlayer("http://commonsware.com/misc/test2.3gp"+";deviceside=true");
//                player.realize();
//                player.prefetch();
//                videoControl = (VideoControl)player.getControl("VideoControl");
//               // videoControl.setDisplaySize(160, 90);
//                System.out.println("-------------- 6");
//                //              videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field");
//                Field videoField = (Field)videoControl.initDisplayMode( VideoControl.USE_GUI_PRIMITIVE, "net.rim.device.api.ui.Field" );
//
//                add(videoField);
//                player.start();
//                videoControl.setVisible(true);
//                VolumeControl volume = (VolumeControl) player.getControl("VolumeControl");
//                volume.setLevel(30);
//            }
//            catch (final Exception ex)
//            {  
//                System.out.println(ex.toString());
//                UiApplication.getUiApplication().invokeLater(new Runnable()
//                {
//                    public void run()
//                    {
//                        Dialog.alert(""+ex);
//                    }
//                });
//            }
        }

    }
private Field initializeVideo(String s) {
Field videoField = null;

try {
// Create player from input stream

_player = javax.microedition.media.Manager.createPlayer(s
);

// Realize the player
_player.realize();

// Cause playback to begin as soon as possible
// once start()is called on the Player.
StreamingBufferControl sbc = (StreamingBufferControl) _player
.getControl("net.rim.device.api.media.control.StreamingBufferControl");
sbc.setBufferTime(0);

// Obtain video control
AdvancedVideoControl vControl = (AdvancedVideoControl) _player
.getControl("net.rim.device.api.media.control.AdvancedVideoControl");

// Initialize the video control and get the video field
videoField = (Field) vControl.initDisplayMode(
AdvancedVideoControl.USE_GUI_ADVANCED,
"net.rim.device.api.ui.Field");

// Set the video to be a size other than full screen.
// This must be done after calling initDisplayMode().
vControl.setDisplaySize(VIDEO_WIDTH, VIDEO_HEIGHT);

vControl.setVisible(true);
} catch (Exception e) {
Dialog.inform(e.getMessage());
}

return videoField;
}
}

No comments:

Post a Comment