0

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

Community
  • 1
  • 1
Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52

2 Answers2

8

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

Akshay Chordiya
  • 4,761
  • 3
  • 40
  • 52
  • 1
    I have heard that we cannot use last.fm api to download cover arts for public use..isn't it ? – N Kaushik Mar 22 '16 at 10:59
  • I'm not sure about it. – Akshay Chordiya Mar 22 '16 at 12:07
  • @NandanKaushik this is true, in fact I emailed last.fm support who confirmed this, so all the apps on google play using last.fm for album art shouldn't be doing so - unless they consent, however I asked for consent and was told that I shouldn't be using for any app. – Mark Nov 08 '16 at 01:10
  • @Mark Do you know why they have made all apis for public use ? – N Kaushik Dec 29 '16 at 10:12
  • 1
    @NandanKaushik From their website - `The Agreement covers only Your use of Last.fm Data. You must not sub-license the Last.fm Data to others.` - so they are public, but for 'personal' use only, sharing any data is considered sub-license. – Mark Dec 29 '16 at 11:35
0

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>
MarcoS
  • 17,323
  • 24
  • 96
  • 174