I created a audio player using following code.
try {
InputStream is = getClass().getResourceAsStream("bell.wav");
player = Manager.createPlayer(is, "audio/X-wav");
player.realize();
player.prefetch();
player.start();
}
catch (IOException ex) {
ex.printStackTrace();
}
catch (MediaException ex) {
ex.printStackTrace();
}
This code works on the simulator without any problem. But it is not working in the phone.
MediaException is thrown. I think phone does not support for this player.
Have there any solutions for this ?
It might help to check what mime types are supported by the device by checking
Manager.getSupportedContentTypes(String protocol);
and
Manager.getSupportedProtocols(String content_type);
You can also try using an URL instead of InputStream
Manager.createPlayer(String url);
Related
When I run following code on my phone I get black screen saying there was uncaught exception but whole block is wrapped in try/catch block so it is weird, anyway when I proceed with execution code just gets to "Getting device.." so it obviously fails on this line:
LocalDevice local = LocalDevice.getLocalDevice();
Here is whole method:
public void startBT()
{
try
{
f.append("Getting device..");
LocalDevice local = LocalDevice.getLocalDevice();
f.append("Got local device..");
DiscoveryAgent agent = local.getDiscoveryAgent();
f.append("Got local discovery agent..");
connString = agent.selectService(new UUID(
"86b4d249fb8844d6a756ec265dd1f6a3", false),
ServiceRecord.NOAUTHENTICATE_NOENCRYPT, false);
f.append("Got connection string - >" + connString);
}
catch (Exception ex)
{
Alert message = new Alert("info");
message.setString(ex.getMessage());
Display.getDisplay(this).setCurrent(message);
}
}
Any ideas?
It looks like device I used doesn't support JSR-82 which is J2ME Bluetooth API(this is built into phone, no way of "installing" it) required to use Bluetooth from J2ME Midlets,here is snippet which should check for JSR-82 support:
public static boolean IsBtJsrComaptible() {
try {
Class.forName("javax.bluetooth.LocalDevice");
return true;
} catch (Exception e) {
return false;
}
}
Please note that I got uncaught exception trying to run above snippet, but maybe it would work on some other device.
I have written following piece of code. It is used to create camera player. I tested it on nokia phones. It's working fine and I am able to see camera and use its functionality.
But the issue is that when the code is tested on samsung phone it throws Media exception and eventually have to make an exit from the application. Due to this code, my inbuilt camera functionality (i.e. on samsung phone) also stops working. So what's the reason to it?
public void startCamera()
{
try
{
try
{
// if player==null
player = createPlayer();
}
catch (Exception ex)
{
ex.printStackTrace();
ErrorDialog.show(ex, "We are exiting the application.",
"Exit", new com.auric.qrev.scanqrcode.lwuit.ui.Action(){
public void actionPerformed() {
m_objMIDlet.exitApp();
}
});
}
try
{
player.realize();
player.prefetch();
}
catch (MediaException ex)
{
ex.printStackTrace();
ErrorDialog.show(ex, "We are exiting the application.",
"Exit", new com.auric.qrev.scanqrcode.lwuit.ui.Action(){
public void actionPerformed() {
m_objMIDlet.exitApp();
}
});
}
//Grab the video control and set it to the current display.
videoControl = (VideoControl)(player.getControl("VideoControl"));
if (videoControl == null)
{
//discardPlayer();
stopCamera();
ErrorDialog.show("Unsupported:\n"+
"Can't get video control\n"+
"We are exiting the application.",
"Exit", new com.auric.qrev.scanqrcode.lwuit.ui.Action(){
public void actionPerformed() {
m_objMIDlet.exitApp();
}
});
}
mediaComponent = new MediaComponent(player);
mediaComponent.setFocusable(false);
m_cameraScreen.showCamera(mediaComponent);
start();
}
catch(Exception ex)
{
ex.printStackTrace();
//discardPlayer();
stopCamera();
ErrorDialog.show("Sorry,Resources unavailable.\nWe are exiting the application.",
"Exit", new com.auric.qrev.scanqrcode.lwuit.ui.Action(){
public void actionPerformed() {
m_objMIDlet.exitApp();
}
});
}
}
private Player createPlayer()throws MediaException, Exception
{
Player mPlayer = null;
// try capture://image first for series 40 phones
try
{
mPlayer = Manager.createPlayer("capture://image");
}
catch (Exception e)
{
e.printStackTrace();
}
catch (Error e)
{
e.printStackTrace();
}
// if capture://image failed, try capture://video
if (mPlayer == null)
{
try
{
mPlayer = Manager.createPlayer("capture://video");
}
catch (Exception e)
{
e.printStackTrace();
throw new MediaException("Sorry,Resources unavailable.");
}
}
if(mPlayer == null)
throw new Exception("Sorry,Resources unavailable.");
return mPlayer;
}
First things first, you have to realize that printStackTrace() is pretty much useless outside of the emulator unless you are using a Symbian phone.
You can also use java.lang.Throwable instead of separating Exception and Error
You can figure out exactly what happens by gathering information as a String and appending it to a simple lcdui Form while you are testing:
try {
// do something that could potentially fail
} catch (Throwable th) {
myDebugLcduiForm.append("potential failure number xx." + th + th.getMessage());
// if necessary, throw a new RuntimeException
}
You might want to update/repost your question once you know exactly what line of code throws what exception.
I have a problem with playing more than 2 sound files in a game I'm developing now in j2me MIDP2 in eclipse.
Please advice me the best way for playing multiple "wav" sound files.
I created the following method that is called once when the program starts
public void setSound()
{
System.out.println("Sound on");
try {
p1=Manager.createPlayer(is1, "audio/X-wav");
p2=Manager.createPlayer(is2, "audio/X-wav");
p3=Manager.createPlayer(is3, "audio/X-wav");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and every time I need to play one of the sounds I stop the two other players (to insure that no one of them is running p2.stop(); p3.stop();) and start the third one (p1.start();) and every time I have two players stopped (being in PREFETCHED State) the third one is not running and exceptions are thrown.
The Java ME spec is not specific about this, but on actual devices you can usually have only one allocated player at a time, so just stopping a player is not enough. See also this forum post. I found it is a challenge even to deallocate a player properly before playing a new one, so in my own code I resorted to waiting for one sound to end before trying to play a new one.
in j2me there is only one player.More than 1 players are not supported.How execute the sound one sound after another or any other.If u want playing multiple sounds means then u follow the below coding
public class AudioPlayer implements javax.microedition.media.PlayerListener{
Player player;
int count=0;
public void playMedia(){
try{
player = Manager.createPlayer(getClass().getResourceAsStream(audioFiles[count]),"audio/x-amr");
player.addPlayerListener(this);
player.start();
}
catch (Exception e) {
return ;
}
}
public void playerUpdate(Player p,String event,Object eventData)
{
//Playing next audio file after completion of current audio.
}
}
I want to play a audio file from the internal speaker (that from which comes the voice during a call) it's possibile ?
Use the setSpeakerphoneOn method of the AudioManager. This requires the MODIFY_AUDIO_SETTINGS permission.
audioManager = (AudioManager)Context.getSystemService(Context.AUDIO_SERVICE);
audioManager.setSpeakerphoneOn(false);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
Question is a bit old. Anyhow, this method may help others:
public void play(MediaPlayer player, String filePath){
try {
player.setDataSource(filePath);
player.setAudioStreamType(AudioManager.STREAM_VOICE_CALL);
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
}
I want loading screen in my application such that it appear when the internet connection starts and loading screen get hide when I got the data from webservice.
I had tried to use .gif file. It is working properly in the simple application which I made which shows only .gif file. But when I integrate this code with my applicationj it is showing the error.
The error is:
TRACE: <at java.lang.IllegalArgumentException>, Exception caught in Display class
java.lang.IllegalArgumentException
at javax.microedition.media.Manager.createPlayer(), bci=8
at com.semaphore.GifCanvas.<init>(GifCanvas.java:41)
at com.semaphore.CompanySplashScreen.dismiss(CompanySplashScreen.java:89)
at com.semaphore.CompanySplashScreen.keyPressed(CompanySplashScreen.java:78)
at javax.microedition.lcdui.CanvasLFImpl.uCallKeyPressed(), bci=19
at javax.microedition.lcdui.DisplayableLFImpl.uCallKeyEvent(), bci=146
at javax.microedition.lcdui.Display$DisplayEventConsumerImpl.handleKeyEvent(), bci=30
at com.sun.midp.lcdui.DisplayEventListener.process(), bci=277
at com.sun.midp.events.EventQueue.run(), bci=179
at java.lang.Thread.run(Thread.java:619)
the code which i am using to for executing .gif file is:-
InputStream ins = getClass().getResourceAsStream("/loading.gif");
try {
player = Manager.createPlayer(ins, "image/gif");
player.prefetch();
player.setLoopCount(10);
player.start();
} catch (IOException ex) {
ex.printStackTrace();
} catch (MediaException ex) {
ex.printStackTrace();
}
if ((vidc = (VideoControl) player.getControl("VideoControl")) != null)
vidc.initDisplayMode(VideoControl.USE_DIRECT_VIDEO, this);
try {
vidc.setDisplayLocation(2, 2);
vidc.setDisplaySize(getWidth()/2,getHeight()/2);
}
catch (MediaException me) {}
vidc.setVisible(true);
How can I resolve this?
According to the documentation, your InputStream is null.
Check if you have your resource properly placed. With that path, it should be on the root dir of your project (or the src dir, I'm not sure). But just check it.