androidaudio-playerlast.fmalbumartandroid-music-player

How to get album art from last.fm Android


I'm making an Music player for Android, I want to provide feature for users to get album art of a song from last.fm.

I've got my API key too. Just need help for retrieving the image from Last.fm.

Any help in getting the image url would also be appreciated.

Thanks in advance.

P.S : For more info about my music player, check the link below https://plus.google.com/u/0/communities/115046175816530349000


Solution

  • I found an solution check below

    Add the below AsyncTask loader

    public class RetrieveFeedTask extends AsyncTask<String, Void, String> {
    
        protected String doInBackground(String... urls) {
            String albumArtUrl = null;
            try {
                XMLParser parser = new XMLParser();
                String xml = parser.getXmlFromUrl(urls[0]); // getting XML from URL
                Document doc = parser.getDomElement(xml);
                NodeList nl = doc.getElementsByTagName("image");
                for (int i = 0; i < nl.getLength(); i++) {
                    Element e = (Element) nl.item(i);
                    Log.d(LOG_TAG,"Size = " + e.getAttribute("size") + " = " + parser.getElementValue(e));
                    if(e.getAttribute("size").contentEquals("medium")){
                        albumArtUrl = parser.getElementValue(e);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return albumArtUrl;
        }
    }
    

    Call it as followed :

    StringBuilder stringBuilder = new StringBuilder("http://ws.audioscrobbler.com/2.0/");
    stringBuilder.append("?method=album.getinfo");
    stringBuilder.append("&api_key=");
    stringBuilder.append("YOUR_LAST_FM_API_KEY");
    stringBuilder.append("&artist=" + URLEncoder.encode("ARTIST_NAME_HERE", "UTF-8"));
    stringBuilder.append("&album=" + URLEncoder.encode("ALBUM_NAME_HERE", "UTF-8"));
    url = new RetrieveFeedTask().execute(stringBuilder.toString()).get();
    

    You need 2 classes : 1. XmlParser 2. DocElement Both of which will be available in link below. Xml parsing tutorial