Monday, August 1, 2016

Adding Internet Radio to Android Application

Greetings of the day!!!

I was working with my SRSM panchanga and wanted to add the internet radio station which was developed by my friend Rajaraman. This radio is a 24X7 gururaghavendra radio station: www.gururaghavendra.net/radio/  which plays Sri Raghavendra songs, pravachanas etc from Mantralaya.

You can download the working of this here:

SRSM Panchanga


  


It's very easy to develop this.

First I have added a button in the main.xml to display the radio by clicking on it, application should invoke the Radio Screen. Here is the xml code for that Button:

 <Button
            android:id="@+id/button8"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:text="Gururaghvendra Radio"
            android:textColor="#FF0000"
            android:textSize="10pt"
            android:textStyle="bold"/>


In the Main.java, I have created a referenced to this Button as below

Button  b8=(Button)findViewById(R.id.button8);

And in Main.java, for clicking of this button I want to invoke radio. So I assume I will create radio.java which contains the Radio Activity.

So Here is the code to invoke that. For that I use setOnClickListener():

 b8.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent k=new Intent(Main.this, radio.class);
startActivity(k);
}
});


For Radio activity, I create the xml as radio.xml. In this I create two buttons as Play and Stop which I am going to use to play and Stop the Radio. Here is the code for Radio.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
 

    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:text="Sri Gururaghavendra Radio"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="@color/ekadashi_text_color"
        android:textStyle="bold" />

    <Button
        android:id="@+id/play"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_margin="10dip"
        android:layout_marginLeft="17dp"
        android:layout_marginTop="70dp"
        android:text="Play" />

    <Button
        android:id="@+id/stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/play"
        android:layout_margin="10dip"
        android:layout_marginTop="21dp"
        android:text="Stop" />

   </RelativeLayout>

Now I will create Radio.java file

public class radio extends Activity {
private Button b1,b2,b3,b4;
private ProgressBar ProgressBar;
private ProgressDialog pDialog;

MediaPlayer  mPlayer = new MediaPlayer();
    private Button buttonPlay;

    private Button buttonStop;

    private MediaPlayer player;
    final String url ="<Radio URL-Stream>";// you need to update your radio link
 
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.radio);

        buttonPlay = (Button) findViewById(R.id.play);
        buttonStop = (Button) findViewById(R.id.stop);
     
        buttonStop.setEnabled(false);


buttonPlay.setOnClickListener(new OnClickListener() {
       
            public void onClick(View v) {
            
             mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
             GetConnect_Radio Rd = new GetConnect_Radio();
             Rd.execute();

  }
        });
     

 buttonStop.setOnClickListener(new OnClickListener() {
         
            public void onClick(View v) {
            
            
             if(mPlayer!=null && mPlayer.isPlaying()){
            
                     mPlayer.stop();
                   
                 }
            
              buttonStop.setEnabled(false);
                  buttonPlay.setEnabled(true);
            }
        });  
 }
 

**
     * Background Async Task to connect to Radio
     */
    class GetConnect_Radio extends AsyncTask<String, String, String> {

    
     /**
         * Before starting background thread Show Progress Dialog
         */
        @Override
        protected void onPreExecute() {
         pDialog =new ProgressDialog(radio.this);

            pDialog.setMessage("Connecting to Radio. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();

        }
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
        try {
        
            mPlayer.setDataSource(url);
        } catch (IllegalArgumentException e) {
         pDialog.dismiss();
            //Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        } catch (SecurityException e) {
         pDialog.dismiss();
           // Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        } catch (IllegalStateException e) {
         pDialog.dismiss();
           // Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
         pDialog.dismiss();
            e.printStackTrace();
        }
        try {
         //mPlayer.prepareAsync();
            mPlayer.prepare();
        } catch (IllegalStateException e) {
         pDialog.dismiss();
            //Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        } catch (IOException e) {
         pDialog.dismiss();
            //Toast.makeText(getApplicationContext(), "You might not set the URI correctly!", Toast.LENGTH_LONG).show();
        }
        mPlayer.start();
return null;
}
/**
     * After completing background task Dismiss the progress dialog
     **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog once got all details
        pDialog.dismiss();
     
        ///

        runOnUiThread(new Runnable() {
            public void run() {
               buttonStop.setEnabled(true);
                   buttonPlay.setEnabled(false);
            }
         
        });

     }
    
    }
}


 " final String url " you have to assign the Radio stream URL.  Now run this code and see how the radio plays . You can download and see this apk to run this radio here:


All the best and happy coding!!!