Question

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

Was it helpful?

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

OTHER TIPS

Please see Last.fm Web Services docs for album.getInfo: http://www.last.fm/api/show/album.getInfo
Here is a sample response, from which you can easily see how to get cover art image url:

<album>
  <name>Believe</name>
  <artist>Cher</artist>
  <id>2026126</id>
  <mbid>61bf0388-b8a9-48f4-81d1-7eb02706dfb0</mbid>
  <url>http://www.last.fm/music/Cher/Believe</url>
  <releasedate>6 Apr 1999, 00:00</releasedate>
  <image size="small">...</image>
  <image size="medium">...</image>
  <image size="large">...</image>
  <listeners>47602</listeners>
  <playcount>212991</playcount>
  <toptags>
    <tag>
      <name>pop</name>
      <url>http://www.last.fm/tag/pop</url>
    </tag>
    ...
  </toptags>
  <tracks>
    <track rank="1">
      <name>Believe</name>
      <duration>239</duration>
      <mbid/>
      <url>http://www.last.fm/music/Cher/_/Believe</url>
      <streamable fulltrack="0">1</streamable>
      <artist>
        <name>Cher</name>
        <mbid>bfcc6d75-a6a5-4bc6-8282-47aec8531818</mbid>
        <url>http://www.last.fm/music/Cher</url>
      </artist>
    </track>
    ...
  </tracks>
</album>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top