How to listen to radio in Java ME
This code example explains the options for accessing radio content with Java ME, and provides a more detailed implementation of how to implement progressive download with Java ME within the context of an online radio application.
Article Metadata
Code Example
Tested with
Compatibility
Article
Overview
There are a number of approaches for accessing radio content on Series 40 using Java ME:
- Progressive download over HTTP (retrieve media content from network)
- Accessing the radio tuner on the device
At time of writing progressive download is the only validated method for accessing radio content (the radio tuner is inaccessible on most devices at time of writing and RTSP streaming is only supported for video.)
HTTP progressive download
This section shows a HTTP progressive download radioMIDlet implementation.
The media types associated with progressive download must be checked prior to implementing this functionality. This is achieved as follows:
String [] list = Manager.getSupportedContentTypes("http");
...
It is quite important to find a link to an online radio that streams the media in a supported device format. The example code sample used above was tested by streaming audio mp3 to Nokia Asha 306.
Below is is a simple MIDlet that only has a play command appended to the Options Menu (at the top left corner) and a stop Command appended to Action Button 1 (at the top right corner).
Command playInputStream = new Command("Over Http InputStream", Command.OK, 2);
Command exitCommand = new Command("Exit", Command.EXIT, 0);
Command stopCommand = new Command("Stop", Command.OK, 0);
When the play button is selected from the Options Menu, a thread is started:
if(c == playInputStream) {
thread = new Thread(this);
thread.start();
}
These are the operations that perform the playback inside the thread:
HttpConnection connection = (HttpConnection) Connector.open("http://broadcast.infomaniak.ch/radiojazz-high.mp3");
InputStream inputStream = connection.openInputStream();
player = Manager.createPlayer(inputStream, "audio/mp3");
As seen above, an HttpConnection is opened to the given url and an input stream is read out of it. Then a player instance is created by providing as arguments the input stream opened from the radio's link, and the content type the player is expected to read.
The player is then realized, pre-fetched and started:
player.realize();
player.prefetch();
player.start();
When stopping the player, we need to remember to de-allocate its instance:
if(c == stopCommand) {
if(player != null) {
try {
player.stop();
player.deallocate();
} catch (MediaException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Finally, in order to enable progressive download, we need to change the MIDlet's Application Descriptor by adding the following JAD attribute:
progressive_download: enabledAccessing the radio tuner
On devices that have an MMAPI implementation which supports the "radio" capture locator, the following code can be used to listen to a radio in Java ME:
Player player = Manager.createPlayer("capture://radio?f=98.3&st=stereo");
// This will tune to 98.3 FM frequency in stereo mode


Contents
Bayop - Not supported on Nokia Asha 311
I tried this command on a Nokia Asha 311 (Developer Platform 2.0) and it failed with an invalid locator exception...bayop 21:50, 10 September 2012 (EEST)
Hamishwillee - Thanks
You've done the right thing posting here and on the java discussion board - lets hope there is an answer. I've added an ArticleNeedsUpdate so hopefully this will get looked at eventually. Thanks!
Regards
Hamishhamishwillee 07:51, 11 September 2012 (EEST)
C10t - So bad no capture radio?!
So there is nothing else for the developer! All APIs are just half baked and nothing works! and you expect asha to beat android? come on... do some basic work right!!! why capture://radio is not supported in asha 311? come on! are you kidding? why is nokia not serious?c10t 03:41, 3 March 2013 (EET)
Skalogir - The example has been updated and tested on Nokia Asha 306
New code that demonstrates how to implement an online radio with Java ME on Asha 306.skalogir 13:09, 14 March 2013 (EET)
Hamishwillee - Awesome - also work on an Asha 311?
Hi Skalogir
Thanks for this. I assume this should also work on an Asha 311? If anyone finds other devices/platforms this works on, please add the appropriate categories and also add tested information to the ArticleMetaData.
Regards
Hamishhamishwillee 05:30, 15 March 2013 (EET)
Skalogir - Nokia Asha 311 is also compatible
Hi Hamish,
Yes this code works on Nokia Asha 311.
Br,
Stratosskalogir 08:53, 15 March 2013 (EET)
Hamishwillee - Thank you!
I will delete all comments next time I visit here, as they have now been addressed.
Regards
Hamishhamishwillee 01:07, 18 March 2013 (EET)
Chaaps - Who wants online radio?
Hi,
The issue has not been addressed. The article's core has been diverted from "Radio" to "Online Radio"... The article was first edited with AMMS API's Radio feature,
"Radio" as in... the tuner radio... where you exercise capture:// protocol..
This is not a fair deal. Please revert back to the old version of the article and address the "radio" as in AMMS api.
Thanks
Chethanchaaps 10:57, 18 March 2013 (EET)
Hamishwillee - What devices does capture: actually work on
Hi Chethan
We can't find any current devices where capture actually works - which makes the protocol pretty useless. Can you confirm where you think it works?
In the meantime I have restructure the article to include using the protocol as an option.
Regards
Hamishhamishwillee 03:08, 19 March 2013 (EET)