iammert / RadioPlayerService

Android service library which uses AAC Player. Ready to use Radio Player Service.
254 stars 91 forks source link

How to show the song title,artist name and image upon the notification area ? + #54

Open Ishu25 opened 7 years ago

Ishu25 commented 7 years ago

In the notification area I want to show the song name , artist name and also the image of the current track an also when the track changes the notification should be updated automatically . Can anyone tell that how to do that? Thanks

GenesisVargasJ commented 7 years ago

Hi! I work with this service. For to show the song, artist name and image you must use the next method:

mRadioManager.updateNotification(String: up text, String: down text, Int: image resource id);

But, in wich part? jajajajaja Alternative, I'm using this code because the code recommend:

@Override public void onMetaDataReceived(String s, String s1) { }

never has working

This is my code:

` private static class ParsingHeaderData { class TrackData { String artist = ""; String title = ""; } URL streamUrl; private Map<String, String> metadata; private TrackData trackData;

    ParsingHeaderData() {

    }

    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.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;
                }
                inData = count > metaDataOffset + 1
                        && count < (metaDataOffset + metaDataLength);
                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;
    }

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

    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 (String metaPart : metaParts) {
            m = p.matcher(metaPart);
            if (m.find()) {
                metadata.put(m.group(1), m.group(2));
            }
        }

        return metadata;
    }
}

private class Test extends AsyncTask<String, Void, String> {

    String tit, art;
    /*
     * (non-Javadoc)
     *
     * @see android.os.AsyncTask#doInBackground(Params[])
     */

    @Override
    protected String doInBackground(String... strings) {
        try {
            URL url = new URL(URL_STREAM);
            ParsingHeaderData streaming = new ParsingHeaderData();
            ParsingHeaderData.TrackData trackData = streaming.getTrackDetails(url);
            art = trackData.artist;
            tit = trackData.title;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String s){
        super.onPostExecute(s);
        mRadioManager.updateNotification(art,tit,img);
    }
}

private class MyTimerTask extends TimerTask {
    public void run() {
        new Test().execute();
    }
}`

I call it in this part:

@Override public void onRadioStarted() { Timer timer = new Timer(); MyTimerTask task = new MyTimerTask(); timer.schedule(task, 100, 60000); }

I hope this helps

Ishu25 commented 7 years ago

hey , @GenesisVargasJ I didn't understand this part mRadioManager.updateNotification(String: up text, String: down text, Int: image resource id); Will u make it clear what actually u are saying ?

GenesisVargasJ commented 7 years ago

If you want show the song title, artist, etc you must to use these function:

mRadioManager.updateNotification(String: up text, String: down text, Int: image resource id);

The rest of code is to see the information of this, since the method that suggests the author to retrieve that information never works

@Override public void onMetaDataReceived(String s, String s1) { }

*This method only show the info in the log

And i use TimerTask for call this information and update the notification area

captura de pantalla 2017-04-24 a las 10 36 54 am

This is my notification working:

  1. String: up text = "MXL Radio"
  2. String: down text = "El no te quiere"
  3. Int: image resource id = R.drawable.image