Skip to main content

Lwuit blackberry when switch from lcdui canvas to lwuit form

Please note these java.net forums are being decommissioned and use the new and improved forums at https://community.oracle.com/community/java.
1 reply [Last post]
sushil20072007
Offline
Joined: 2011-04-19

hi ,
when i switch from lwuit form to lcdui canvas (having player ) its ok . by deintializing theme .but when i revert back from
lcdui canvas to lwuit form its hangs .
and on blackberry its shows exception
At illegal exception of push screen.

pls help its urgent my code is

public class VideoPlayerBB extends Canvas implements CommandListener, PlayerListener,Runnable {
private Displayable previous;
private Display display;
private Player player;
private VideoControl videoControl;
private String url;
private Thread initializer;
public com.sun.lwuit.Form form;
private Command close;
private Command rePlay;
private Command back,back1;
private VolumeControl vc;
private InputStream is;
MIDlet inst;
public VideoPlayerBB(Display display, com.sun.lwuit.Form previous ,MIDlet instance){
super();
player = null;
form = null;
form = previous;
inst = instance;
try
{
DeIntializetheme();
}catch(Exception e )
{
System.out.println("---"+e.getMessage());
}
this.display = display;

// this.previous = previous;
// close = new Command("close", Command.SCREEN, 1);
// back = new Command("Back",Command.BACK, 1);
// back1 = new Command("VideoBack",Command.BACK, 1);
// addCommand(close);
// addCommand(back);
// addCommand(back1);
setCommandListener(this);
}

public void DeIntializetheme()
{

//init the LWUIT Display
com.sun.lwuit.Display.deinitialize();

}
public void IntializeTheme() {
//init the LWUIT Display
com.sun.lwuit.Display.init(inst);

// Setting the application theme is discussed
// later in the theme chapter and the resources chapter
try {
Resources r = Resources.open("/LWUITtheme.res");
UIManager.getInstance().setThemeProps(
r.getTheme(r.getThemeResourceNames()[0]));

//UIManager.getInstance().setLookAndFeel(new PimpLookAndFeel());

} catch (Exception e) {
//////////////////////System.out.print("Exception in Initialize()");
}
}
public void initializeVideo(InputStream ins){
this.is = ins;
initializer = new Thread(this);
initializer.start();
}
public void startVideoPlayBack(String videofile)
{
try
{
InputStream ins = getClass().getResourceAsStream("/"+videofile+".3gp");
initializeVideo(ins);
}
catch(Exception ex)
{
}
}
public void run(){
try {
player = Manager.createPlayer(is, "video/3gpp");
player.addPlayerListener(this);
player.realize();
player.prefetch();
Control [] controls = player.getControls();
for (int i = 0; i < controls.length; i++) {
if (controls[i] instanceof VolumeControl) {
vc = (VolumeControl) controls[i];
}
}
// set a default volume
vc.setLevel(50);
playVideo();
} catch (IOException ioe) {
Alert alert = new Alert("IOException thrown", ioe.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert, this.previous);
} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert, this.previous);
} catch(Exception e){
Alert alert = new Alert("Exception thrown", e.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert, this.previous);
}
}

public void playVideo(){
try {
// Get the video control and set it to the current display.
videoControl = (VideoControl)player.getControl("VideoControl");
if (videoControl != null) {
videoControl.initDisplayMode(videoControl.USE_DIRECT_VIDEO, this);
}
int cHeight = this.getHeight();
int cWidth = this.getWidth();
videoControl.setDisplaySize(cWidth, cHeight);
display.setCurrent(this);
videoControl.setVisible(true);
player.start();

} catch (MediaException me) {
Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert);
} catch(Exception e){
Alert alert = new Alert("Exception thrown", e.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert, this.previous);
}
}

/** Paints background color */
public void paint(Graphics g){
g.setColor(128, 128, 128);
g.fillRect(0, 0, getWidth(), getHeight());
}

public void playerUpdate(Player p, String event, Object eventData) {
//add "Replay" option when video is finished
if (event == PlayerListener.END_OF_MEDIA)
{

if(player!=null)
{
try {
player.stop();
} catch (MediaException ex) {
ex.printStackTrace();
}
player.close();
player=null;
videoControl=null;
}
url = null;
initializer = null;
IntializeTheme();
com.sun.lwuit.Form f1 = new ExtendedForm(form);
// com.sun.lwuit.Form f = new com.sun.lwuit.Form("Hello world");
// form.show();
f1.show();
switchToPrevious(350, f1);

// if (rePlay == null)
// {
// rePlay = new Command("re-play", Command.SCREEN, 1);
// addCommand(rePlay);
// }
}
}
private void switchToPrevious(int duration,com.sun.lwuit.Form f1)
{
Thread thr = new Thread( new MyRunner(duration,(ExtendedForm) f1));
thr.setPriority(Thread.MAX_PRIORITY);
thr.start();
}
public void commandAction(Command c, Displayable s) {
if(c == rePlay){
try{
player.setMediaTime(0);
player.start();
}
catch (MediaException me)
{
Alert alert = new Alert("MediaException thrown", me.getMessage(), null, AlertType.ERROR);
display.setCurrent(alert);
}
}
// else if(c == close){
//
// if(player!=null)
// player.close();
//
// url=null;
// }
else if(c== back){
if(player!=null)
{
try {
player.stop();
} catch (MediaException ex) {
ex.printStackTrace();
}
player.close();
//player.deallocate();
player=null;
videoControl=null;
}
url = null;
initializer = null;
IntializeTheme();
com.sun.lwuit.Form f1 = new ExtendedForm(form);
f1.show();
switchToPrevious(350, f1);
}

else if(c== back1){
if(player!=null)
{
try {
player.stop();
} catch (MediaException ex) {
}
player.close();
player=null;
videoControl=null;
}
url = null;
initializer = null;
IntializeTheme();
//====Screen hangs here ======
form.show();

}
}
protected void keyPressed(int keyCode) {
int key = getGameAction(keyCode);
int currentVolume = vc.getLevel();
if(key==UP)
{
if(currentVolume < 100)
currentVolume = ( (currentVolume+20) > 100)?100:currentVolume+20;
}
else if(key==DOWN)
{
if(currentVolume >0)
currentVolume = ( (currentVolume-20) < 0)?0:currentVolume-20;
}
vc.setLevel(currentVolume);
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
vprise
Offline
Joined: 2003-11-07

The blackberry port uses the native RIM API, it doesn't support LCDUI back and forth.
Either port your code to LWUIT or port the LCDUI code to native blackberry API.