How to play mp3 from server in Java ME
Article Metadata
The method below shows how an mp3 file can be played from a server in Java ME.
public void playAudio()
{
try
{
String url = "http://server/audio.mp3";
HttpConnection conn = (HttpConnection)Connector.open(url,
Connector.READ_WRITE);
InputStream is = conn.openInputStream();
player = Manager.createPlayer(is,"audio/amr");
player.realize();
// get volume control for player and set volume to max
vc = (VolumeControl) player.getControl("VolumeControl");
if(vc != null)
{
vc.setLevel(100);
}
player.prefetch();
player.start();
}
catch(Exception e)
{}
}
Note for Series 40 Devices: - The Midlet is responsible to avoid/catch out_of_memory exceptions in case the mp3 file to be downloaded exceeds the amount of free memory.
A good practice to avoid these kind of problems may be to either: add the keyword <progressive_download; enable as an attribute to the midlets jad or manifest file; or change the url for the file to be downloaded to the format: resource://my_media_file.mp3?streamable=true In the case above this means: String url = "http://server/audio.mp3?streamable=true";
Applying one of the changes should result in streaming instead of first downloading the whole file and starting to play not before the complete file was downloaded.


25 Sep
2009
If any phone hasn’t much space to store all mp3 files of his/her choice then a midlet which contains a method defined in this article can be used to play songs from server which has a lot of space to store songs…
Here this article addresses a method playAudio(), in which mainly HttpConnnection class and player object is created.
Here an important thing to understand is that , in the creation of player instance, an instance of InputStream is used. And in the creation of InputStream instance, an instance of HttpConnection is used. And similarly in creation of HttpConnection instance, an URL from server is used as a String. So this article explains playAudio() in a very way
Change Audiotype
Change audiotype to: "audio/mpeg" for using with MP3
== Pavanragi - shoutcast Internet Rado streaming in j2me(Java) MIDP
Hiii
shoutcast Internet Rado streaming in j2me(Java)
i want to stream mp3 files from the shoutcast URL
and i want to play it in Nokia using java,can you please help me,i have tried this
using HttpConnection and Manager Class, i am facing the Exception which is "11 error in HttpOperation",java.IO.Exception.....
Thanks
Pavanpavanragi 15:28, 13 July 2012 (EEST)