In this post I will be writing about how to extract meta-data info from Shoutcast url.
The reason for writing this post is that not much info is available about this stuff on Android, thus without wasting any more time, lets begin.

The first step in this procedure is to call the url, with Icy-MetaData = 1, as it’s header.
So the code will become some thing like this:

//=== Fetch ICY INT
URL url = new URL(rurl);
URLConnection conn = url.openConnection();
conn.addRequestProperty("Icy-MetaData", "1");
conn.connect();

When you call the url with this header, you will receive a header in response, icy-metaint. This field indicates after how many bytes we have the meta-data, so we will have to skip these many bytes.


int discard = Integer.parseInt(hList.get(META_INT).get(0) + "");
InputStream is = conn.getInputStream();
long read = 0;
long skipped = is.skip(discard);
while (skipped != discard) {
skipped += is.skip(discard - skipped);
}

Its time to read the chunks of meta-data , the first byte *16 of this chunk represents the number of bytes that contain the data, rest are just padded.


//=== Read 1st byte
int fb = is.read();
//=== Read metadata, first byte *16
int mdl = fb * 16;
if(mdl>0){
read = 0;
String md = "";
do {
byte[] mdb = new byte[mdl - (int) read];
read = is.read(mdb);
md += new String(mdb, 0, mdb.length);
} while (read != mdl);
}

So we have our meta data now, time to parse it.
The data is separated by ‘;’ so we just have to split now.

String metdata = new String(md.trim().getBytes(), "utf-8");
//=== Extract
String[] data = metdata.split(";");
Tag t = new Tag();
for (String tag:data){
if(tag.startsWith(STREAM_TITLE))
t.setTitle(tag.split("=")[1]);
}

So the final code looks like this:


Hope you liked this post.
Thanks

Leave a Reply