2

can any one tell me how to implement a standalone java client for playing FM radio. I searched over the net could not find anything useful. What are the all the API we need to implement and once the implementation is over, how to test it?

user414967
  • 5,225
  • 10
  • 40
  • 61
  • Do you have hardware and drivers for it to receive radio signal? – maksimov Jun 19 '12 at 09:44
  • I would say most pc's can't receive radio signals. You will have to stream it from the internet. – 11684 Jun 19 '12 at 09:45
  • Note: For media playback consider JavaFX instead of Swing. – Puce Jun 19 '12 at 10:00
  • @Puce there is not real need to use JavaFX just for the radio stream playback - check the answer and the example i have added (using JLayer library). – Mikle Garin Jun 19 '12 at 12:33
  • @MikleGarin Yes, you don't have to use JavaFX and there might be other solutions, but playing media is one of JavaFX core features. Besides according to Oracle, Swing is in maintenance mode. JavaFX is the upcoming rich client GUI toolkit for Java. – Puce Jun 19 '12 at 12:39
  • @Puce JavaFX is indeed might replace Swing sooner or later (i bet much, much later), but for this specific case its not necessary. Plus... where did i say anything about Swing? :) JLayer is just a small library that allows you to playback mp3/streams (and more) anywhere without using heavy JMF, JavaFX or some other platform-dependant libraries. – Mikle Garin Jun 19 '12 at 12:45
  • 1
    @MikleGarin The tag in the question mentions Swing. – Puce Jun 19 '12 at 13:03

2 Answers2

8

In addition to AurA answer...

You can use JLayer library to easily read and play most of internet radios. That library is also cross-platform and, additionally, allows you to play any mp3 file.

Here is a small stream player example:

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.Player;

import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;

public class RadioConnector
{
    public static void main ( String[] args )
    {
        try
        {
            playRadioStream ( "http://radio.flex.ru:8000/radionami" );
        }
        catch ( IOException e )
        {
            e.printStackTrace ();
        }
        catch ( JavaLayerException e )
        {
            e.printStackTrace ();
        }
    }

    private static void playRadioStream ( String spec ) throws IOException, JavaLayerException
    {
        // Connection
        URLConnection urlConnection = new URL ( spec ).openConnection ();

        // If you have proxy
        //        Properties systemSettings = System.getProperties ();
        //        systemSettings.put ( "proxySet", true );
        //        systemSettings.put ( "http.proxyHost", "host" );
        //        systemSettings.put ( "http.proxyPort", "port" );
        // If you have proxy auth
        //        BASE64Encoder encoder = new BASE64Encoder ();
        //        String encoded = encoder.encode ( ( "login:pass" ).getBytes () );
        //        urlConnection.setRequestProperty ( "Proxy-Authorization", "Basic " + encoded );

        // Connecting
        urlConnection.connect ();

        // Playing
        Player player = new Player ( urlConnection.getInputStream () );
        player.play ();
    }
}

Note that playRadioStream method will handle the thread its called from until something happes (for example connection to radio server breaks or you stop the stream).

P.S. Yes, i have included working radio URL into the example - you can try launching it and your computer will start playing the radio stream.

Mikle Garin
  • 10,083
  • 37
  • 59
  • Very nice answer!!!Mikle how i can get more like this in you example?And how i can retrieve some info like song name the radio is play?I am using it to implement an radio app. – GOXR3PLUS Apr 01 '16 at 13:51
  • 1
    @howTo Not sure what did you mean under `` - the radio stream URL? You can find them across the internet. And about retrieving radio/song name - I couldn't really help since I didn't try it, but it should certainly be possible. You can check out the various docs and projects from the JLayer site: http://www.javazoom.net/javalayer/documents.html – Mikle Garin Apr 04 '16 at 14:05
  • Yes i found the first,thanks.When you listen to music into the car radio it shows the title of the track and some other info,the name of station for example.You know how to retrieve this info via the connection(Not the bit rate,channels etc from the track.)Only the name of track and radio station name for example....https://www.youtube.com/watch?v=vvnBnqdxWmg – GOXR3PLUS Apr 06 '16 at 18:45
6

There are many Radio websites that you can access using Webservices API's

I am posting the link of the most popular radio api online.

http://www.last.fm/api/radio

Using Java Web Services you can integrate with your application easily.

AurA
  • 12,135
  • 7
  • 46
  • 63
  • How is that to do with the FM radio? – maksimov Jun 19 '12 at 09:46
  • @maksimov you can stream the radio from that site, and looking at the URL suggest's it's something like fm... You can't receive radio signal from a pc, as you said yourself. The only option is to stream it from the internet. – 11684 Jun 19 '12 at 09:49
  • @11684 I understand, this is a proposed workaround for if their requirements to play *FM* can be changed to play *any* sort of radio. All in all, it's a poorly researched question we are dealing with here. – maksimov Jun 19 '12 at 09:52
  • 1
    A very important part in that API says by itself "Note: This API does not cover mp3 playback, it only manages tuning into radio stations and retrieving playlists and the URLs of mp3 files. For mp3 decoding and playback support you will need another third party library, for example JLayer.", the Link to this quote is [here](http://code.google.com/p/lastfm-java/wiki/RadioStreaming), below Code Example. Don't know by myself, how good the API is, since never tried it myself, though the inof is helpful in some way +1 for the info part though :-) – nIcE cOw Jun 19 '12 at 09:52
  • Though the OP never tried to explain what he/she had tried so far, so that one can help a bit further :( – nIcE cOw Jun 19 '12 at 09:55
  • I tried to get API to stream stations online. But I have no idea at all how to implement this since this is a new thing for me. I am facing starting trouble what to do a basic thing for this. My first attemp will include create the UI with play,pause,stop button. Where play will stream and play and pause will do pause and stop will stop the radio.It would be nice to implement as a stand alone application. But from the answers/comments, I understood that it is not possible to implement as standalone. Am I right? – user414967 Jun 19 '12 at 10:20
  • @user414967 you can create a standalone application out of a web service, but you will require internet connectivity for it to work. – AurA Jun 19 '12 at 10:33