Back to Posts

Live Streaming Radio

Hello friends, In this blog I am going to share my ideas about ‘Live streaming radio’. It’s bit intresting and it involves using of android MediaPlayer and interacting with the ‘URL’ to stream the music. So, friends let’s get started.

First of all start a new project on android studio. Here I am going to use Android Studio for this, you can also go with Eclipse whichever suits you.

Now follow the instructions-

Project name – Live Radio > Choose empty activity > Activity Name- MainActivity > Finish.

Before proceeding to code make sure you have the following permissions in your AndroidManifest.xml –

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.INTERNET" />

For reference check the image given below –

Then make sure your .xml or layout file is coded as given below-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="in.indilabz.www.liveradio.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="80dp"
        android:textSize="50dp"
        android:text="Live Radio"
        android:textStyle="bold"
        android:gravity="center"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/stream"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="111dp" />
</RelativeLayout>

When you will get your your layout file updated as the code given above it will look like the image given below.

Now code your .java file or Activity file as given below-

package in.indilabz.www.liveradio;

import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    Button music;
    MediaPlayer mediaPlayer;
    boolean prepared = false;
    boolean started = false;

    String stream = "http://38.107.242.59:8026/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        music = (Button)findViewById(R.id.stream);
        music.setEnabled(false);
        music.setText("Loading");
        music.setBackgroundColor(Color.CYAN);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

        new PlayerTask().execute(stream);

        music.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                if (started) {
                    mediaPlayer.pause();
                    started = false;
                    music.setText("Resume");
                    music.setBackgroundColor(Color.GREEN);

                } else {
                    mediaPlayer.start();
                    started = true;
                    music.setText("Pause");
                    music.setBackgroundColor(Color.RED);
                }
            }
        });
    }
/*
AsyncTask enables proper and easy use of the UI thread. 
This class allows you to perform background operations and
 publish results on the UI thread without having to manipulate
 threads and/or handlers.
*/
    class PlayerTask extends AsyncTask<String, Void, Boolean> {
        @Override
        protected Boolean doInBackground(String... strings) {

            try {
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.prepare();
                prepared = true;

            } catch (IOException e) {
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);

            music.setEnabled(true);
            music.setText("Resume");
            music.setBackgroundColor(Color.GREEN);
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (started) {
            mediaPlayer.start();

        }
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (started) {
            mediaPlayer.start();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (prepared) {

            mediaPlayer.release();
        }

    }

    private class PlayTask extends AsyncTask<String, Void, Boolean> {

        @Override
        protected Boolean doInBackground(String... strings) {

            try {
                mediaPlayer.setDataSource(strings[0]);
                mediaPlayer.prepare();
                prepared = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
            return prepared;
        }

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            music.setEnabled(true);
            music.setText("Resume4");
            music.setBackgroundColor(Color.GREEN);

        }
    }

}

While operating button’s background colour and button’s text changing be carefull else it well all get mess up.

Now I am showing you some of the screenshots of my live radio while operating.

Check the button’s text and button’s color at each on click.It will help you how to deal with such an operations.

Friends, the first image will be displayed when the device is connecting to the server to fetch media data. Second one will pop up when the media data is loaded and ready to stream. And the last one will be available when the media is playing and user wants to pause it.

Thank you for visiting this tutorial. If you have any doubts or queries regarding this, you can comment below in our comment section.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Posts