Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse and Play a .pls file in Android

can anybody help me how to parse and play this .pls file in android

 [playlist]
 NumberOfEntries=1
 File1=http://stream.radiosai.net:8002/
like image 583
user1048958 Avatar asked Nov 26 '11 08:11

user1048958


1 Answers

A quick search resulted in this very basic PlsParser from the NPR app that, by the looks of it, will simply parse the .pls file and return all embedded URLs in a list of strings. You'll probably want to take a look at that as a starting point.

After that, you should be able to feed the URL to a MediaPlayer object, although I'm not completely sure about what formats/protocols are supported, or what limitations might apply in the case of streaming. The sequence of media player calls will look somewhat like this.

MediaPlayer mp = new MediaPlayer();
mp.setDataSource("http://stream.radiosai.net:8002/");
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.prepare(); //also consider mp.prepareAsync().
mp.start();

Update: As far as I can tell, you can almost literally take the referenced code and put it your own use. Note that the code below is by no means complete or tested.

public class PlsParser {
        private final BufferedReader reader;

        public PlsParser(String url) {
            URLConnection urlConnection = new URL(url).openConnection();
            this.reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        }

        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 "";
        }
}

Once you have that, you can simply create a new PlsParser with the url of the .pls file and call getUrls afterwards. Each list item will be a url as found in the .pls file. In your case that'll just be http://stream.radiosai.net:8002/. As said, you can then feed this to the MediaPlayer.

like image 53
MH. Avatar answered Oct 01 '22 09:10

MH.