Payday Loan Payday Loan

Play a .pls file in Android.

Ok , I suppose most of you know that to listen to a radio station from Shoutcast you need to play a .pls file.
If you download the .pls file on your Android phone and you’ll try to play it you’ll notice that you can’t do that.
So, in this tutorial I’ll teach you how to play a .pls file in an Android application.

Like always, I made a short sample which you’ll be able to download .I’ll explain you step by step by adding comments in the Java code. In this project I created only a class and an XML file.

Ok. Here is the ListenToTheRadio.class :

public class ListenToTheRadio extends Activity{
 
MediaPlayer mp; //We declare the mediaplayer
Button bStart,bPause,bStop; //We declare the buttons we will use
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayout);
 
mp = new MediaPlayer();
 
bStart = (Button) findViewById(R.id.bStart); //findviewbyid for this 3 buttons
bPause = (Button) findViewById(R.id.bPause);
bStop = (Button) findViewById(R.id.bStop);
 
bStart.setOnClickListener(new View.OnClickListener() { //Ok this is the most importat part, be focused !
 
public void onClick(View v) { //This happens when we click the Start button.
// TODO Auto-generated method stub
 
/* Ok, let me explain what this "if" does. The mediaplayer can be stopped or paused. If it is paused with
mp.start(); we can resume it but if it is stopped we need to setDataSource and prepare it again(to get data from the link).
So, I made as when we click the start button , it will unclickable but when you click pause or stop it will be clickable.
If this is foggy for you I think you will understand more as you read the code.
*/
if(bStop.isClickable()){
 
mp.start(); //Here we resume after we pressed the Pause button.
 
}else{
 
try {
mp.setDataSource("http://92.114.63.16:8100/"); //Here we set the source
//As you see, you need to enter the link to the shoutcast radio.
//DO NOT write /listen.pls also, write ONLY the link
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) { //This try and catch we need to put them because if we don't
//will have errors.
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mp.prepare(); //We prepare the mediaplayer to run.
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace(); //Another some boring try and catch clauses.
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
 
mp.start(); //Here we DO NOT resume, we START the MediaPlayer.
 
//Here we set the clickable value for the 3 buttons
bStart.setClickable(false);
bPause.setClickable(true);
bStop.setClickable(true);
}
}
});
 
bPause.setOnClickListener(new View.OnClickListener() {
 
public void onClick(View v) {
// TODO Auto-generated method stub
mp.pause(); //We pause the MediaPlayer
 
//Start button will be clickable, as well as the Stop button but the Pause button will be unclickable
bStart.setClickable(true);
bPause.setClickable(false);
bStop.setClickable(true);
}
});
 
bStop.setOnClickListener(new View.OnClickListener() {
 
public void onClick(View v) {
// TODO Auto-generated method stub
mp.stop(); //Here we stop the MediaPlayer
 
//Another change for the buttons clickable's values.
bStop.setClickable(false);
bPause.setClickable(false);
bStart.setClickable(true);
}
});
 
bPause.setClickable(false);
bStop.setClickable(false); //At first we will be able to click only on the start Button
 
}
}

Here is the mylayout.xml file where we have the three buttons :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
 
    <Button
        android:id="@+id/bStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" />
 
    <Button
        android:id="@+id/bPause"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pause" />
 
    <Button
        android:id="@+id/bStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop" />
 
</LinearLayout>

VERY IMPORTANT !
In your Manifest file please enter the Internet Permission :

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

You can download the entire project from here .

This is all for this tutorial. Thank you for reading and I hope you enjoyed it.

||||| 0 I Like It! |||||

Search

Popular

Sponsors