Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do streaming with .pls files in android?

I want to play a .pls file for my android application using this url http://playerservices.streamtheworld.com/pls/VIRGINRADIO_DUBAIAAC.pls I know that it is not possible to play .pls files using MediaPlayer directly.So I parsed this file using a Pls parser and set each url to a media player.But it won't work .Also shows the error error (1, -2147483648).

public class PlayListParser {

    private BufferedReader reader;

    public PlayListParser(String url) {
        try {
            URL plsFileUrl = new URL(url.trim());
            URLConnection urlConnection = plsFileUrl.openConnection();
           // InputStream input = new BufferedInputStream(urlConnection.openStream());
            InputStream iStream = urlConnection.getInputStream();
            this.reader = new BufferedReader(new InputStreamReader(iStream));
            // this.reader = new BufferedReader(new FileReader(file), 1024);
        } catch (MalformedURLException e) {
            Log.e("PlayListParser", "Got MalformedURLException  = " + e.getMessage());
        } catch (IOException e) {
            Log.e("PlayListParser", "Got  IOException = " + e.getMessage());
        }
    }

    public List<String> getUrls() {
        LinkedList<String> urls = new LinkedList<String>();
        while (true) {
            try {
                String line = reader.readLine();
                if (line == null) {
                    break;
                }
                String url = parseLine(line);
                if (url != null && !url.equals("")) {
                    urls.add(url);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return urls;
    }

    private String parseLine(String line) {
        if (line == null) {
            return null;
        }
        String trimmed = line.trim();
        if (trimmed.indexOf("http") >= 0) {
            return trimmed.substring(trimmed.indexOf("http"));
        }
        return "";
    }
}


PlayListParser playListParser = new PlayListParser(URL_PLS_STREAMING);
List<String > playList = playListParser.getUrls();
if(playList != null && ! playList.isEmpty()){
    for(String url : playList){
        MediaPlayer mediaPlayer = new MediaPlayer();
        try {
        mediaPlayer.setDataSource(playList.get(0));//"http://stream2.streamq.net:8020/");
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.prepareAsync();
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

            @Override
            public void onPrepared(MediaPlayer mp) {
                mp.start();

            }
        });
        //mediaPlayer.prepare();
        } catch (IllegalArgumentException e) {
        Log.e("MainActivity","Got IllegalArgumentException = " + e.getMessage());

        } catch (IllegalStateException e) {
        Log.e("MainActivity","Got IllegalStateException = " + e.getMessage());

        } catch (IOException e) {
        Log.e("MainActivity","Got IOException = " + e.getMessage());

        }
    }
}

How can i play this .pls file? I could not find a good reference about it.Also i want to play,pause,and rewind these files.

Thanks in Advance

like image 236
Devu Soman Avatar asked Jun 05 '13 04:06

Devu Soman


People also ask

How do I play a PLS file?

PLS file extension can be opened with iTunes, Winamp Media Player, VLC, PotPlayer, Helium, Clementine, CyberLink PowerDVD, AudioStation, and other media management software programs. You can also open PLS files in Windows Media Player with Open PLS in WMP.

Can VLC play PLS file?

Compatible media player softwareiTunes, VLC media player, Totem, RealPlayer, Winamp, Yahoo! Music Jukebox, MediaMonkey, Windows Media Player, AIMP, Kodi, Rhythmbox, foobar2000, and more than 30 others are able to interpret (open) PLS files.


1 Answers

Get URL from .pls file

This will return URL like http://stream2.streamq.net:8020

package com.direct.radio.global;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.LinkedList;
import android.content.Context;
import android.util.Log;

public class GetStreamingUrl {

private static String LOGTAG = "GetStreamingUrl";
private Context mContext;

public GetStreamingUrl(Context context) {
    Log.i(LOGTAG, "call to constructor");
    this.mContext = context;

}

public LinkedList<String> getStreamingUrl(String url) {

    Log.i(LOGTAG, "get streaming url");
    final BufferedReader br;
    String murl = null;
    LinkedList<String> murls = null;
    try {
        URLConnection mUrl = new URL(url).openConnection();
        br = new BufferedReader(
                new InputStreamReader(mUrl.getInputStream()));
        murls = new LinkedList<String>();
        while (true) {
            try {
                String line = br.readLine();

                if (line == null) {
                    break;
                }
                murl = parseLine(line);
                if (murl != null && !murl.equals("")) {
                    murls.add(murl);

                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Log.i(LOGTAG, "url to stream :" + murl);
    return murls;
}

private String parseLine(String line) {
    if (line == null) {
        return null;
    }
    String trimmed = line.trim();
    if (trimmed.indexOf("http") >= 0) {
        return trimmed.substring(trimmed.indexOf("http"));
    }
    return "";
        }
  } 

Activity_Player.java or Service_Player.java

you can write this code as per your need , Define this method

LinkedList<String> urls;

private LinkedList<String> fGetPlayableUrl(String mPls) {
    GetStreamingUrl oGetStreamingUrl = new GetStreamingUrl(Activity_Splash.this);
    urls = oGetStreamingUrl.getStreamingUrl(mPls);
    return urls;
}

Here, fGetPlayableUrl(String mPls) pass .pls URL. Now you have streaming URL.

MediaPlayer  mMediaPlayer = new MediaPlayer();

Now, pass URL to

mMediaPlayer.setDataSource(urls.toString());
mMediaPlayer.prepareAsync();
mMediaPlayer.start();
like image 159
Ronak Mehta Avatar answered Oct 18 '22 08:10

Ronak Mehta