0

I'm playing media player from online stream radio:

mMediaPlayer = new MediaPlayer();
try {
    mMediaPlayer.setDataSource("http://online-radioroks.tavrmedia.ua/RadioROKS");
    mMediaPlayer.prepareAsync();

    mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

        public void onPrepared(MediaPlayer mp) {
            start();
        }
    });

    notifyCallbackListeners(PlayerCallbackEvent.PLAYER_SONG_CHANGE);
}
catch (IllegalArgumentException e) {
    e.printStackTrace();
}
catch (IOException e) {
    e.printStackTrace();
} catch (IllegalStateException ex){
    ex.printStackTrace();
}

It works fine, but can i get data from stream like what song now is playing and name of radio?

0

3 Answers 3

10

get data from stream like what song now is playing and name of radio?

For that streaming url should provide that data than accordingly that you can read data.

For the url which you have mentioned in the question its providing data in header with request Icy-MetaData and header icy-metaint. More details regarding those type of url and header data please check this link.

Now how can you parse that header data ?

You can try the below posted code

public class ParsingHeaderData {
    public class TrackData {
        public String artist = "";
        public String title = "";
    }

    protected URL streamUrl;
    private Map<String, String> metadata;
    private TrackData trackData;

    public ParsingHeaderData() {

    }

    public TrackData getTrackDetails(URL streamUrl) {
        trackData = new TrackData();
        setStreamUrl(streamUrl);
        String strTitle = "";
        String strArtist = "";
        try {
            metadata = executeToFetchData();
            if (metadata != null) {
                String streamHeading = "";
                Map<String, String> data = metadata;
                if (data != null && data.containsKey("StreamTitle")) {
                    strArtist = data.get("StreamTitle");
                    streamHeading = strArtist;
                }
                if (!TextUtils.isEmpty(strArtist) && strArtist.contains("-")) {
                    strArtist = strArtist.substring(0, strArtist.indexOf("-"));
                    trackData.artist = strArtist.trim();
                }
                if (!TextUtils.isEmpty(streamHeading)) {
                    if (streamHeading.contains("-")) {
                        strTitle = streamHeading.substring(streamHeading
                                .indexOf("-") + 1);
                        trackData.title = strTitle.trim();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return trackData;
    }

    private URLConnection con;
    private InputStream stream;
    private List<String> headerList;

    private Map<String, String> executeToFetchData() throws IOException {
        try {
            con = streamUrl.openConnection();

            con.setRequestProperty("Icy-MetaData", "1");
            // con.setRequestProperty("Connection", "close");
            // con.setRequestProperty("Accept", null);
            con.connect();

            int metaDataOffset = 0;
            Map<String, List<String>> headers = con.getHeaderFields();
            stream = con.getInputStream();

            if (headers.containsKey("icy-metaint")) {
                headerList = headers.get("icy-metaint");
                if (headerList != null) {
                    if (headerList.size() > 0) {
                        metaDataOffset = Integer.parseInt(headers.get(
                                "icy-metaint").get(0));
                    } else
                        return null;
                } else
                    return null;

            } else {
                return null;

            }

            // In case no data was sent
            if (metaDataOffset == 0) {
                return null;
            }

            // Read metadata
            int b;
            int count = 0;
            int metaDataLength = 4080; // 4080 is the max length
            boolean inData = false;
            StringBuilder metaData = new StringBuilder();
            while ((b = stream.read()) != -1) {
                count++;
                if (count == metaDataOffset + 1) {
                    metaDataLength = b * 16;
                }
                if (count > metaDataOffset + 1
                        && count < (metaDataOffset + metaDataLength)) {
                    inData = true;
                } else {
                    inData = false;
                }
                if (inData) {
                    if (b != 0) {
                        metaData.append((char) b);
                    }
                }
                if (count > (metaDataOffset + metaDataLength)) {
                    break;
                }

            }
            metadata = ParsingHeaderData.parsingMetadata(metaData.toString());
            stream.close();
        } catch (Exception e) {
            if (e != null && e.equals(null))
                Log.e("Error", e.getMessage());
        } finally {
            if (stream != null)
                stream.close();
        }
        return metadata;

    }

    public URL getStreamUrl() {
        return streamUrl;
    }

    public void setStreamUrl(URL streamUrl) {
        this.metadata = null;
        this.streamUrl = streamUrl;
    }

    public static Map<String, String> parsingMetadata(String metaString) {
        @SuppressWarnings({ "rawtypes", "unchecked" })
        Map<String, String> metadata = new HashMap();
        String[] metaParts = metaString.split(";");
        Pattern p = Pattern.compile("^([a-zA-Z]+)=\\'([^\\']*)\\'$");
        Matcher m;
        for (int i = 0; i < metaParts.length; i++) {
            m = p.matcher(metaParts[i]);
            if (m.find()) {
                metadata.put((String) m.group(1), (String) m.group(2));
            }
        }

        return metadata;
    }
}

How To call

public class Test extends AsyncTask<Void, Void, Void> {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected Void doInBackground(Void... params) {
            try {
                URL url = new URL(
                        "http://online-radioroks.tavrmedia.ua/RadioROKS");
                ParsingHeaderData streaming = new ParsingHeaderData();
                TrackData trackData = streaming.getTrackDetails(url);
                Log.e("Song Artist Name ", trackData.artist);
            Log.e("Song Artist Title", trackData.title);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

    }

Call execute Task

new Test().execute();

Log Print

2
  • 1
    thanks man, this is so good. i regret i can't vote for your reply Jan 28, 2015 at 14:00
  • @user1140237 its not working in android 4.4.2, but working in marshmallow, any solution ?
    – Jhaman Das
    Feb 6, 2017 at 11:51
0

the question is too old, this answer may help someone, in my case i was trying to get the track info from an API of radio.co .

so firstly i had to add this class to allow me execute HTTP requests name it HttpHandler :

public class HttpHandler {

private static final String TAG = HttpHandler.class.getSimpleName();

public HttpHandler() {
}

public String makeServiceCall(String reqUrl) {
    String response = null;
    try {
        URL url = new URL(reqUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        // read the response
        InputStream in = new BufferedInputStream(conn.getInputStream());
        response = convertStreamToString(in);
    } catch (MalformedURLException e) {
        Log.e(TAG, "MalformedURLException: " + e.getMessage());
    } catch (ProtocolException e) {
        Log.e(TAG, "ProtocolException: " + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "IOException: " + e.getMessage());
    } catch (Exception e) {
        Log.e(TAG, "Exception: " + e.getMessage());
    }
    return response;
}

private String convertStreamToString(InputStream is) {
    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line).append('\n');
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return sb.toString();
}
}

To get track info i've add this method to my Main Activity :

private class getTrackInfo extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //Toast.makeText(MainActivity.this,"Getting Song Info",Toast.LENGTH_LONG).show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        HttpHandler sh = new HttpHandler();
        // Making a request to url and getting response
        String url = "Your URL HERE";
        String jsonStr = sh.makeServiceCall(url);

        //Log.e(TAG, "Response from url: " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                JSONObject current_track = jsonObj.getJSONObject("current_track");
                strArtist = current_track.getString("title");
                artwork_url = current_track.getString("artwork_url");

                String streamHeading = strArtist;

                if (!TextUtils.isEmpty(strArtist) && strArtist.contains("-")) {
                    strArtist = strArtist.substring(0, strArtist.indexOf("-"));
                    trackData.artist = strArtist.trim();
                }
                if (!TextUtils.isEmpty(streamHeading)) {
                    if (streamHeading.contains("-")) {
                        strTitle = streamHeading.substring(streamHeading
                                .indexOf("-") + 1);
                        trackData.title = strTitle.trim().replace("-","");
                    }
                }
                if (!TextUtils.isEmpty(artwork_url)) {
                    trackData.artwork_url = artwork_url;
                }
            } catch (final JSONException e) {
                Log.e(TAG, "Json parsing error: " + e.getMessage());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Json parsing error: " + e.getMessage(),
                                Toast.LENGTH_LONG).show();
                    }
                });

            }

        } else {
            Log.e(TAG, "Couldn't get json from server.");
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(getApplicationContext(),
                            "Couldn't get json from server. Check LogCat for possible errors!",
                            Toast.LENGTH_LONG).show();
                }
            });
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        //Log.e(TAG, "Artist: " + trackData.artist + "\ntitle : " + trackData.title + "\nartwork_url :" + trackData.artwork_url);

        //to update my user interface with the new info
        songTitle.setText(trackData.title);
        albumTitle.setText(trackData.artist);
        new getAlbumImageTask(albumImage).execute(trackData.artwork_url);
    }
}

to call this method :

new getTrackInfo().execute();

To load the artwork image add this method :

private class getAlbumImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public getAlbumImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

to call the method to load artwork image :

new getAlbumImageTask(albumImage).execute(trackData.artwork_url);

there are variable in this code related to my situation ignore then and adapt the code for your situation i hope that this help someone

0

Provided solution(ParsingHeaderData) works as charm however I have faced issues with Cyrillic encoding.

Here is how I patched executeToFetchData method to make it work properly:

    private Map<String, String> executeToFetchData() throws IOException {
        try {
            con = streamUrl.openConnection();
            con.setRequestProperty("Accept-Charset", "UTF-8");
            con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
            con.setRequestProperty("Icy-MetaData", "1");
            con.connect();

            int metaDataOffset = 0;
            Map<String, List<String>> headers = con.getHeaderFields();
            stream = con.getInputStream();

            if (headers.containsKey("icy-metaint")) {
                headerList = headers.get("icy-metaint");
                if (headerList != null) {
                    if (headerList.size() > 0) {
                        metaDataOffset = Integer.parseInt(headers.get(
                                "icy-metaint").get(0));
                    } else
                        return null;
                } else
                    return null;

            } else {
                return null;

            }

            // In case no data was sent
            if (metaDataOffset == 0) {
                return null;
            }

            // Read metadata
            int b;
            int count = 0;
            int metaDataLength = 4080; // 4080 is the max length

            ByteArrayOutputStream buffer = new ByteArrayOutputStream();

            while ((b = stream.read()) != -1) {
                count++;
                if (count == metaDataOffset + 1) {
                    metaDataLength = b * 16;
                }
                if (count > metaDataOffset + 1
                        && count < (metaDataOffset + metaDataLength)) {
                    if (b != 0) {
                        buffer.write(b);
                    }
                }
                if (count > (metaDataOffset + metaDataLength)) {
                    break;
                }
            }
            metadata = ParsingHeaderData.parsingMetadata(
                    new String(buffer.toByteArray(), 0, buffer.size(), "UTF-8"));
            buffer.close();
            stream.close();
        } catch (Exception e) {
            if (e != null && e.equals(null))
                Log.e("Error", e.getMessage());
        } finally {
            if (stream != null)
                stream.close();
        }
        return metadata;
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.