LAST.FM RADIO API

In the as3 last.fm api provided over at google code, there is no classes provided for the radio API, which appears to be a fairly recent addition to the last.fm API. The radio API requires 2 key steps. First step is the radio.tune method, where you submit the station URL. For example lastfm://artist/radiohead/similarartists will tune you in to a station which returns Radioheads similar artists. I wrote my own radio class (in a similar style of the other classes provided) which looks a bit like this:

package
{
 import KeyValue;
 
 import flash.events.Event;
 import flash.net.URLRequest;
 import flash.net.URLRequestMethod;
 import flash.net.navigateToURL;
 import flash.net.URLVariables;
 
 public class Radio extends LastFMBase
 {
   public static const TUNE:String = "tune";
 
   public function tuneRadio(stationName:String):void
   {
     var variables:Array = new Array();
     variables.push(new KeyValue("method", "radio.tune"));
     variables.push(new KeyValue("api_key", LastFMBase.API_KEY));
     variables.push(new KeyValue("sk", LastFMBase.sk));
     variables.push(new KeyValue("station", stationName));
 
     var urlVariables:URLVariables = new URLVariables();
     urlVariables['method'] = "radio.tune";
     urlVariables['api_key'] = LastFMBase.API_KEY;
     urlVariables['sk'] = LastFMBase.sk;
     urlVariables['station'] = stationName;
     urlVariables['api_sig'] = createAPI_Signature(variables);
 
     requestURL("", URLRequestMethod.POST, urlVariables);
     loader.addEventListener(Event.COMPLETE, function (event:Event):void {dispatchEvent(new Event(Radio.TUNE))});
   }
  }
}

The second step is to fetch a playlist, using the radio.getPlaylist method. By modifying the playlist method in the Playlist.as to the following, you can parse in the playlist location, returned when the tuneRadio function is called:

public function fetch(playlistURL:String):void
{
	var playListVars:Array = new Array();
	playListVars.push(new KeyValue("method", "radio.getPlaylist"));
	playListVars.push(new KeyValue("api_key", LastFMBase.API_KEY));
	playListVars.push(new KeyValue("sk", LastFMBase.sk));
	var urlVariables:URLVariables = new URLVariables();
	urlVariables['method'] = "radio.getPlaylist";
	urlVariables['api_key'] = LastFMBase.API_KEY;
	urlVariables['sk'] = LastFMBase.sk;
	urlVariables['api_sig'] = createAPI_Signature(playListVars);
	trace("URL VARIABLES: " + urlVariables);
	requestURL("", URLRequestMethod.POST, urlVariables);
	loader.addEventListener(Event.COMPLETE, function (event:Event):void {dispatchEvent(new Event(Playlist.FETCH))});
}

So, in short to call these from your main class, it could look like the following:

package
{
	import flash.events.Event;
	import flash.utils.Dictionary;
 
	public class lastfmClass extends LastFMBase
	{
		private var _auth:Auth = new Auth();
		private var _radio:Radio = new Radio();
		private var _playlist:Playlist = new Playlist();
		public var _trackArray:Object = new Object();
 
		private function tuneMyStation(artistName:String):void
		{
			_radio.tuneRadio("lastfm://artist/"+_artistName+"/similarartists");
			_radio.addEventListener(Radio.TUNE, getRadioHandler);
		}
 
		public function getRadioHandler(event:Event):void
		{
			_radio.removeEventListener(Radio.TUNE, getRadioHandler);
			_playlist.fetch(_radio.xml.station.url);
			_playlist.addEventListener(Playlist.FETCH, fetchHandler);
		}
 
		private function fetchHandler(event:Event):void
		{
			//process results
		}		
	}
}

Tags: , , , , ,

Leave a Reply