Last.fm Add-on
From fringDocumentation
Last.fm Add-On Source Code
Last.fm is a social music site, with an open API. You are welcome to download the Last.fm add-on source code and use it as a template for your add-on.
The following page provides deeper understanding of the Last.fm add-on source code.
The main aspects of the Last.fm add-on are:
Community – Mobilizing the Last.fm community.
Audio Streaming - Implement streaming directly from the Last.fm to the mobile device.
Mashups – Integrating the Last.fm add-on with fring VoIP and chat and other add-on services (Skype, MSN etc) with only few lines of code.
We used PHP but it could have been .NET or any other web programming environment. This code is called add-on and it communicates on one end with the Last.fm API and on the other end with the handset.
Here are the main high-lights of the Last.fm add-on:
1. User registration
2. Music streaming
3. Data Base
4. Mashup
1. User registration
Last.fm is a personal service. Our add-on prompts Last.fm credentials to provide the personalized Last.fm service. When registering the new add-on on the fring developers site we marked the check box “Password protected” so that the Last.fm add-on will get a user/password field from the user and verify these credentials with the Last.fm site.
Check the following functions related to the registration: File: eventsHandlers.php • function handleSubscriptionEvent() – parse credentials from client • function check_lastfm_credentials($userName, $userPassword) – check credentials on Last.fm • function get_radio_handshake($user, $pass) – call Last.fm API to check credentials
2. Music streaming
• The user enters his preferred music to the add-on interface - To do so we developed a form that includes one text box and a clickable image buttons for triggering actions.
• After clicking on the search image button the fring client sends request to the add-on.
• The add-on goes to Last.fm API in order to get the tracks list. The add-on gets last.fm search result and adjusts a radio station.
• If the Last.fm API managed to adjust the radio station the add-on gets a radio station with a list of tracks in XSPF format (XML format for sharing playlists).
• The XSPF is parsed to extract MP3 URL, together with the name of the song and additional track info.
• The add-on sends message feed to the client with the following:
- GUI:
--- Artwork of the track
--- Track info such as: artist name, track name, album
--- Navigation image buttons like: Stop, Next, Love, Ban, Etc…
- Request to start audio streaming with the MP3 URL we’ve got from the XSPF.
- On song finish - request next track.
• Client side description- fring client starts track playback with fring MP3 progressive playback API with MP3 URL that was extracted from the XSPF. When the add-on client side Javascript code gets Playback finished notification it sends request to the add-on server side code that goes back to the Last.fm API to extracts the next track. The information is being parsed again and the MP3 URL is sent back to the client.
Check the following functions related to music streaming: file eventsHandlers.php • function play_next($userHash, $data) – get request to search by artist or play personal library • function handle_station_request($userHash, $artist, $userdata, $station_type=PLAY_TYPE_ARTIST) – call Last.fm API to tune station and get get track list in XSPF • function faxl_build_player_view($userHash, $track, $userdata) – build player view
3. Data Base
We have just described the process for one user play, but we wish to support multi users and therefore need a DB (we chose My SQL but it can be any DB).
The DB includes the main following fields:
• User
• Mobile device screen info: for better GUI placement
• Last search: so next search will be auto filled with last search.
4. Mashup
The add-on we have just developed can interact with other add-ons on fring. For example your add-on can identify which of the user’s last.fm friends are fring users as well, and perform crosscheck to which services they are subscribed to at fring. Thus the add-on can add an item to the option items menu enabling the user to chat and VoIP with his friend according the various services they are subscribed to.
Check the following functions related to the mashup logic: file eventsHandlers.php • function get_friends($lastfm_user) – call Last.fm API to get friends list • function faxl_build_friends_list($friends, $userHash, $userdata) – for each friend in friends list we get his Last.fm id, when look in add-on subscribed users is any one registered with the same last.fm id. • function get_services($lastfmUser) - If we found registered fring user, we add to his profile image chat or call icon (using imageCopy.php tool) and register fring.OpenChat() or fring.PlaceVoiceCall() command with list item.
Now, it is recommended to go through the Quick Start Guide in order to get better understanding of fring add-ons deployment.