playing mp3 in j2me
hi, i am a developer in j2me, i tried to play a mp3 file in a midlet. but it is always calling an exception. the code is
try
{
Player p = Manager.createPlayer("http://www.indbazaar.com/Ratheesh/test.mp3");
p.setLoopCount(5);
p.start();
}
catch(IOException e)
{
System.out.println("Error in io : " + e.toString());
}
catch(MediaException e)
{
System.out.println("Error in media : " + e.toString());
}
the exception is Error in media : javax.microedition.media.MediaException : can not create a player for com/sun/mmedia/MP3Player, anyone pls help me
I know that this thread is old and maybe it has been resolved. I just wanted to add, just in case someone else is looking into playing mp3 files that I have mine working and I have tested on a Nokia 6620 but should work on any Nokia series 60. You folks can correct me if I am wrong but I believe that the cell phone is only required to support MIDP2.0 and this should work.
My code example below downloads the mp3 file from a website passing in a number of variables. Something like this:
http://www.mymp3.com/request.php?field1=1&field2=var&field3=rt700
This is suppose to return the actual mp3 file and the code below is how it handles it.
/*
* PlayerForm.java
*
* Created on August 30, 2005, 8:08 AM
*/
package ark;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.io.*;
import java.io.*;
import javax.microedition.media.control.VolumeControl;
import javax.microedition.io.HttpConnection;
import javax.microedition.io.Connector;
/**
*
* @author js
* @version
*/
public class PlayerForm extends Form implements CommandListener, ItemStateListener, Runnable
{
Display d = null;
List p = null;
String media = null;
Player player = null;
VolumeControl vc;
/**
* constructor
*/
public PlayerForm(Display _d, List _p, String c)
{
super("Media Player Preview");
//append("Sample Text Item");
try
{
d = _d;
p = _p;
media = c;
// Set up this form to listen to command events
setCommandListener(this);
// Set up this form to listen to changes in the internal state of its interactive items
setItemStateListener(this);
// Add the Exit command
addCommand(new Command("Stop", Command.EXIT, 0));
addCommand(new Command("Back", Command.EXIT, 1));
new Thread(this).start();
}
catch(Exception e)
{
d.setCurrent( new Alert("Error", e.getMessage(),null, AlertType.ERROR) );
}
}
public void run()
{
try
{
HttpConnection conn = (HttpConnection)Connector.open(media, Connector.READ_WRITE);
InputStream is = conn.openInputStream();
player = Manager.createPlayer(is,"audio/mpeg");
player.realize();
// get volume control for player and set volume to max
vc = (VolumeControl) player.getControl("VolumeControl");
if(vc != null)
{
vc.setLevel(100);
}
// the player can start with the smallest latency
player.prefetch();
// non-blocking start
player.start();
}
catch(Exception e)
{
d.setCurrent( new Alert("Error", e.getMessage(),null, AlertType.ERROR) );
}
}
/**
* Called when user action should be handled
*/
public void commandAction(Command command, Displayable displayable)
{
String s = command.getLabel();
if( s.equals("Back") )
{
d.setCurrent(p);
}
if( s.equals("Stop") )
{
try
{
player.stop();
}
catch(Exception e)
{
d.setCurrent( new Alert("Preview Error", e.getMessage(),null, AlertType.ERROR) );
}
}
}
/**
* Called when internal state of any item changed
*/
public void itemStateChanged(Item item)
{
}
}
Read through the run method which is called from the the constructor. The entire object is a class Form. I hope that this will help someone somewhere out there. :) Thanks.
Regards,
J
linuxwannbe@yahoo.com