Skip to main content

AUDI and VIDEO lwuit

7 replies [Last post]
elie
Offline
Joined: 2008-06-11
Points: 0

Hello all,

I am working on lwuit since 3 weeks. i was trying lately to implement audio and video capabilities like playing audio, video file and append it to a fom and most important is to add camera functionalities to the Application like using the camer of the phone to take a snapshot.

i am getting a nullpointerexception at

vid=new MediaComponent(player);

where player is configured once as
player = Manager.createPlayer("capture://video");

and the other for audio.

Any Suggestions.

thank you

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
elie
Offline
Joined: 2008-06-11
Points: 0

add a question

elie
Offline
Joined: 2008-06-11
Points: 0

I appliedeverything and it is working fine but we have only one more issue the size of the display is very small even if we set the height or width of the media component the size of the camera is roo small.

Can anyone help?

thank you

chen

Hi,
The Component Size is determined by what Layout are you using, changing
the Component size directly is not something you should do.
If you would like to give hints to the Layout you can use the
setPreferredSize method or to change the Layout.

Regards,
Chen

what you can do is to set the preferred size of the component or to
place the Com
lwuit-users@mobileandembedded.org wrote:
> I appliedeverything and it is working fine but we have only one more issue the size of the display is very small even if we set the height or width of the media component the size of the camera is roo small.
>
> Can anyone help?
>
> thank you
> [Message sent by forum member 'elie' (elie)]
>
> http://forums.java.net/jive/thread.jspa?messageID=280716
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

technolgia
Offline
Joined: 2009-03-09
Points: 0

Hi,
I am trying to develop an application on blackberry which requires access to the camera.
My code is as below:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import com.sun.lwuit.Command;
import com.sun.lwuit.Display;
import com.sun.lwuit.MediaComponent;
import com.sun.lwuit.Form;
import com.sun.lwuit.Graphics;
import com.sun.lwuit.Image;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.geom.Dimension;
import com.sun.lwuit.layouts.BorderLayout;
import java.io.IOException;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VideoControl;

/**
*
* @author nortons
*/
public class Camera implements ActionListener {

private Command cameraCommand;
private Command backCommand, captureCommand;
private Player player;
private VideoControl videoControl = null;
private static Camera thisInstance;
Form form = new Form(" ");

public Camera() {
cameraCommand = new Command("Camera");
backCommand = new Command("Back");
captureCommand = new Command("Capture");
}

public static Camera getInstance() {
if (thisInstance == null) {
thisInstance = new Camera();
}
return thisInstance;
}

public void showCamera() {
form.removeAll();
form.setLayout(new BorderLayout());
form.addCommand(backCommand);
form.setDefaultCommand(captureCommand);
form.setCommandListener(this);
try {
player = Manager.createPlayer("capture://video");
player.prefetch();
player.realize();
videoControl = (VideoControl) player.getControl("VideoControl");
MediaComponent mediaComponent = new MediaComponent(player);
int w = Display.getInstance().getDisplayWidth() - 10;
int h = Display.getInstance().getDisplayHeight() - 100;
mediaComponent.setWidth(w);
mediaComponent.setHeight(h);
mediaComponent.setPreferredSize(new Dimension(w, h));
form.addComponent(BorderLayout.CENTER, mediaComponent);
player.start();

} catch (IOException ioe) {
System.err.println(ioe.getMessage());
} catch (MediaException me) {
System.err.println(me.getMessage());
}
form.show();

}

private void capture() {
try {
// Get the image.
byte[] raw = videoControl.getSnapshot(null);
System.out.println("Bytes Before:" + raw.toString());
player.close();
player = null;
videoControl = null;
CapturedImage.getInstance().setRaw(raw);
CapturedImage.getInstance().show();
// Shut down the player.

} catch (MediaException me) {
System.err.println(me.getMessage());
}
}

public void actionPerformed(ActionEvent arg0) {
if (arg0.getCommand() == cameraCommand) {
showCamera();
} else if (arg0.getCommand() == backCommand) {
player.close();
form.removeAll();
PatientDetailsForm.getInstance().show();
} else if (arg0.getCommand() == captureCommand) {
capture();
}
}
}

This works fine on the WTK. But when i try the same on blackberry emulator and when i try capturing the image i get the following exception i get the following exception:

[b]java.lang.illegalStateException:initDisplayMode() has not been called.[/b]

Then i added the following line of code after the player.getControl(); :

videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, null);

now when i try capturing i get a [b]nullPointerException()[/b].
I even tried the following
Item item=(Item)videoControl.initDisplayMode(VideoControl.USE_GUI_PRIMITIVE, "javax.microedition.lcdui.Item");

i still get [b]nullPointerException()[/b].

Can someone pls help me with this.

Thanking you,
Regards
Norton.

elie
Offline
Joined: 2008-06-11
Points: 0

Hello Chen,

please find below the code i am testing for camera functionalities :

import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.Font;
import com.sun.lwuit.animations.Animation;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import com.sun.lwuit.Command;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.sun.lwuit.*;
import com.sun.lwuit.Image;
import com.sun.lwuit.List;
import javax.microedition.rms.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.media.control.VideoControl;
import com.sun.lwuit.MediaComponent ;

/**
*
* @author Elie
*/
public class photo extends MIDlet implements PlayerListener {

/** Creates a new instance of photo */
public photo() {
initialize();
}

MediaComponent vid;
private Player player = null;
private String key="file://introd.wav";
private VideoControl vc = null;
Form f;

/** This method initializes UI of the application.
*/
private void initialize() {
// Insert pre-init code here

// Insert post-init code here
}

/**
* This method should return an instance of the display.

public Display getDisplay() {
return Display.getDisplay(this);
}
*/
/**
* This method should exit the midlet.
*/
public void exitMIDlet() {

destroyApp(true);
notifyDestroyed();
}

public void startApp() {
try {

playMedia();
}
catch (Exception e) {
System.err.println("Unable to play: " + e);
e.printStackTrace();
}
[b]vid=new MediaComponent(player);[/b]
try {
vid.setVisible(true);
vid.start();
} catch (MediaException ex) {
ex.printStackTrace();
}
f.addComponent(vid);
f.show();

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void playerUpdate(Player player, String string, Object object) {

vc = (VideoControl)player.getControl("VideoControl");
try{
if(vc != null) {
Item videoDisp = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE,null);
vc.setDisplayLocation(0,0);
vc.setDisplayFullScreen(true);
vc.setVisible(true);
}
}
catch (Exception mx)
{
}}
private void playMedia() throws Exception {
try{ player = Manager.createPlayer("capture://image");
player.realize();
}// realize
catch (MediaException me) {

try{
player = Manager.createPlayer("capture://video");
player.realize();
}// realize
catch (Exception e) {
}
}
}

}
********************************************************************************************

and below the test code for playing a sound.

import com.sun.lwuit.Display;
import com.sun.lwuit.Form;
import com.sun.lwuit.Label;
import com.sun.lwuit.Font;
import com.sun.lwuit.animations.Animation;
import com.sun.lwuit.animations.CommonTransitions;
import com.sun.lwuit.animations.Transition3D;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.layouts.FlowLayout;
import com.sun.lwuit.layouts.GridLayout;
import com.sun.lwuit.list.ListCellRenderer;
import com.sun.lwuit.plaf.Style;
import com.sun.lwuit.plaf.UIManager;
import com.sun.lwuit.util.Resources;
import com.sun.lwuit.Command;
import java.io.IOException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import com.sun.lwuit.*;
import com.sun.lwuit.Image;
import com.sun.lwuit.List;
import javax.microedition.rms.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.media.control.VideoControl;
import com.sun.lwuit.MediaComponent ;

/**
*
* @author Elie
*/
public class photo extends MIDlet implements PlayerListener {

/** Creates a new instance of photo */
public photo() {
initialize();
}

MediaComponent vid;
private Player player = null;
private String key="file://introd.wav";
private VideoControl vc = null;
Form f;

/** This method initializes UI of the application.
*/
private void initialize() {
// Insert pre-init code here

// Insert post-init code here
}

/**
* This method should return an instance of the display.

public Display getDisplay() {
return Display.getDisplay(this);
}
*/
/**
* This method should exit the midlet.
*/
public void exitMIDlet() {

destroyApp(true);
notifyDestroyed();
}

public void startApp() {
try {
playMedia(key, "audio/x-wav");
}
catch (Exception e) {
System.err.println("Unable to play: " + e);
e.printStackTrace();
}
[b] vid=new MediaComponent(player);[/b]
try {
vid.start();
} catch (MediaException ex) {
ex.printStackTrace();
}
f.addComponent(vid);
f.show();

}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

public void playerUpdate(Player player, String string, Object object) {
}

private void playMedia(String locator, String key) throws Exception {

try{
String file = locator.substring(locator.indexOf("file://") + 6, locator.length());
player = Manager.createPlayer(getClass().getResourceAsStream(file), key);
player.addPlayerListener(this);
player.setLoopCount(-1); // play indefinitely
player.prefetch(); // prefetch
player.realize(); // realize
}
catch (Exception e) {
}
}
}

chen

Hi Elie,
There is 2 problems in the first midlet:

1)in startApp you need to add this 2 lines at the beginning:
Display.init(this);
f = new Form();
2)in playMedia() method you try to create player using
"capture://image" url and you catch only MediaException, catch
IOException as well(the WTK throws IOException on "capture://image" ).

In The second midlet you don't need to use the MediaComponent since you
create audio player, there is nothing to show to the screen, use the
regular mmapi functionality to play this file.

Regards,
Chen

lwuit-users@mobileandembedded.org wrote:
> Hello Chen,
>
> please find below the code i am testing for camera functionalities :
>
> import com.sun.lwuit.Display;
> import com.sun.lwuit.Form;
> import com.sun.lwuit.Label;
> import com.sun.lwuit.Font;
> import com.sun.lwuit.animations.Animation;
> import com.sun.lwuit.animations.CommonTransitions;
> import com.sun.lwuit.animations.Transition3D;
> import com.sun.lwuit.events.ActionEvent;
> import com.sun.lwuit.events.ActionListener;
> import com.sun.lwuit.layouts.BorderLayout;
> import com.sun.lwuit.layouts.BoxLayout;
> import com.sun.lwuit.layouts.FlowLayout;
> import com.sun.lwuit.layouts.GridLayout;
> import com.sun.lwuit.list.ListCellRenderer;
> import com.sun.lwuit.plaf.Style;
> import com.sun.lwuit.plaf.UIManager;
> import com.sun.lwuit.util.Resources;
> import com.sun.lwuit.Command;
> import java.io.IOException;
> import javax.microedition.midlet.*;
> import javax.microedition.lcdui.*;
> import com.sun.lwuit.*;
> import com.sun.lwuit.Image;
> import com.sun.lwuit.List;
> import javax.microedition.rms.*;
> import javax.microedition.media.*;
> import javax.microedition.media.control.*;
> import javax.microedition.media.control.VideoControl;
> import com.sun.lwuit.MediaComponent ;
>
>
> /**
> *
> * @author Elie
> */
> public class photo extends MIDlet implements PlayerListener {
>
> /** Creates a new instance of photo */
> public photo() {
> initialize();
> }
>
>
> MediaComponent vid;
> private Player player = null;
> private String key="file://introd.wav";
> private VideoControl vc = null;
> Form f;
>
>
> /** This method initializes UI of the application.
> */
> private void initialize() {
> // Insert pre-init code here
>
> // Insert post-init code here
> }
>
> /**
> * This method should return an instance of the display.
>
> public Display getDisplay() {
> return Display.getDisplay(this);
> }
> */
> /**
> * This method should exit the midlet.
> */
> public void exitMIDlet() {
>
> destroyApp(true);
> notifyDestroyed();
> }
>
> public void startApp() {
> try {
>
> playMedia();
> }
> catch (Exception e) {
> System.err.println("Unable to play: " + e);
> e.printStackTrace();
> }
> [b]vid=new MediaComponent(player);[/b]
> try {
> vid.setVisible(true);
> vid.start();
> } catch (MediaException ex) {
> ex.printStackTrace();
> }
> f.addComponent(vid);
> f.show();
>
> }
>
> public void pauseApp() {
> }
>
> public void destroyApp(boolean unconditional) {
> }
>
> public void playerUpdate(Player player, String string, Object object) {
>
>
> vc = (VideoControl)player.getControl("VideoControl");
> try{
> if(vc != null) {
> Item videoDisp = (Item)vc.initDisplayMode(vc.USE_GUI_PRIMITIVE,null);
> vc.setDisplayLocation(0,0);
> vc.setDisplayFullScreen(true);
> vc.setVisible(true);
> }
> }
> catch (Exception mx)
> {
> }}
> private void playMedia() throws Exception {
> try{ player = Manager.createPlayer("capture://image");
> player.realize();
> }// realize
> catch (MediaException me) {
>
> try{
> player = Manager.createPlayer("capture://video");
> player.realize();
> }// realize
> catch (Exception e) {
> }
> }
> }
>
>
> }
> ********************************************************************************************
>
> and below the test code for playing a sound.
>
> import com.sun.lwuit.Display;
> import com.sun.lwuit.Form;
> import com.sun.lwuit.Label;
> import com.sun.lwuit.Font;
> import com.sun.lwuit.animations.Animation;
> import com.sun.lwuit.animations.CommonTransitions;
> import com.sun.lwuit.animations.Transition3D;
> import com.sun.lwuit.events.ActionEvent;
> import com.sun.lwuit.events.ActionListener;
> import com.sun.lwuit.layouts.BorderLayout;
> import com.sun.lwuit.layouts.BoxLayout;
> import com.sun.lwuit.layouts.FlowLayout;
> import com.sun.lwuit.layouts.GridLayout;
> import com.sun.lwuit.list.ListCellRenderer;
> import com.sun.lwuit.plaf.Style;
> import com.sun.lwuit.plaf.UIManager;
> import com.sun.lwuit.util.Resources;
> import com.sun.lwuit.Command;
> import java.io.IOException;
> import javax.microedition.midlet.*;
> import javax.microedition.lcdui.*;
> import com.sun.lwuit.*;
> import com.sun.lwuit.Image;
> import com.sun.lwuit.List;
> import javax.microedition.rms.*;
> import javax.microedition.media.*;
> import javax.microedition.media.control.*;
> import javax.microedition.media.control.VideoControl;
> import com.sun.lwuit.MediaComponent ;
>
>
> /**
> *
> * @author Elie
> */
> public class photo extends MIDlet implements PlayerListener {
>
> /** Creates a new instance of photo */
> public photo() {
> initialize();
> }
>
>
> MediaComponent vid;
> private Player player = null;
> private String key="file://introd.wav";
> private VideoControl vc = null;
> Form f;
>
>
> /** This method initializes UI of the application.
> */
> private void initialize() {
> // Insert pre-init code here
>
> // Insert post-init code here
> }
>
> /**
> * This method should return an instance of the display.
>
> public Display getDisplay() {
> return Display.getDisplay(this);
> }
> */
> /**
> * This method should exit the midlet.
> */
> public void exitMIDlet() {
>
> destroyApp(true);
> notifyDestroyed();
> }
>
> public void startApp() {
> try {
> playMedia(key, "audio/x-wav");
> }
> catch (Exception e) {
> System.err.println("Unable to play: " + e);
> e.printStackTrace();
> }
> [b] vid=new MediaComponent(player);[/b]
> try {
> vid.start();
> } catch (MediaException ex) {
> ex.printStackTrace();
> }
> f.addComponent(vid);
> f.show();
>
> }
>
> public void pauseApp() {
> }
>
> public void destroyApp(boolean unconditional) {
> }
>
> public void playerUpdate(Player player, String string, Object object) {
> }
>
> private void playMedia(String locator, String key) throws Exception {
>
> try{
> String file = locator.substring(locator.indexOf("file://") + 6, locator.length());
> player = Manager.createPlayer(getClass().getResourceAsStream(file), key);
> player.addPlayerListener(this);
> player.setLoopCount(-1); // play indefinitely
> player.prefetch(); // prefetch
> player.realize(); // realize
> }
> catch (Exception e) {
> }
> }
> }
> [Message sent by forum member 'elie' (elie)]
>
> http://forums.java.net/jive/thread.jspa?messageID=279927
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net

chen

Hi Elie,
Can you please send the code you are trying to implement + the stack
trace of the exception.

Thanks,
Chen

lwuit-users@mobileandembedded.org wrote:
> Hello all,
>
> I am working on lwuit since 3 weeks. i was trying lately to implement audio and video capabilities like playing audio, video file and append it to a fom and most important is to add camera functionalities to the Application like using the camer of the phone to take a snapshot.
>
> i am getting a nullpointerexception at
>
> vid=new MediaComponent(player);
>
> where player is configured once as
> player = Manager.createPlayer("capture://video");
>
> and the other for audio.
>
> Any Suggestions.
>
> thank you
> [Message sent by forum member 'elie' (elie)]
>
> http://forums.java.net/jive/thread.jspa?messageID=279903
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
> For additional commands, e-mail: users-help@lwuit.dev.java.net
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@lwuit.dev.java.net
For additional commands, e-mail: users-help@lwuit.dev.java.net