user414967
user414967

Reputation: 5325

implement Java fm radio

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?

Upvotes: 2

Views: 11178

Answers (2)

Mikle Garin
Mikle Garin

Reputation: 10153

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.

Upvotes: 8

AurA
AurA

Reputation: 12373

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.

Upvotes: 6

Related Questions