Archive for February 21st, 2009

21
Feb

mp3tunes-as3-api open source API to use MP3Tunes in Flex/AIR/AS3 projects

mp3tunes is a service to load your audio files and share them between different devices such as: desktop, mobile, game consoles and the Web. I just completed creating a foundation API for AS3.0 so you can start using the API in your Flex/AIR/AS3.0 projects.
You can see the project in google code: http://code.google.com/p/mp3tunes-as3-api/
The SWC and ASDOC can be downloaded from here: http://mp3tunes-as3-api.googlecode.com/files/Archive.zip
And here’s a simple implementation of the API in Flex 4 (Gumbo). The code login into the MP3tunes Music API, retrieve album result, artist data, track data and plays the first audio file in your account.


<FxApplication xmlns="http://ns.adobe.com/mxml/2009"
	creationComplete="creationCompleteHandler(event)">
	<Script>
		<![CDATA[
			import flash.events.Event;
			import flash.media.Sound;
			import flash.net.URLRequest;
			import com.elad.MP3tunes.vo.TrackItemVO;
			import com.elad.MP3tunes.vo.TrackListVO;
			import com.elad.MP3tunes.events.TrackDataEvent;
			import com.elad.MP3tunes.vo.AlbumListVO;
			import com.elad.MP3tunes.events.AlbumDataEvent;
			import com.elad.MP3tunes.vo.ArtistItemVO;
			import com.elad.MP3tunes.vo.ArtistListVO;
			import mx.controls.Alert;
			import com.elad.MP3tunes.events.ArtistsResultEvent;
			import com.elad.MP3tunes.events.MusicEvent;
			import mx.events.FlexEvent;
			import com.elad.MP3tunes.Music;
			private var music:Music;
			protected function creationCompleteHandler(event:FlexEvent):void
			{
				music = new Music("YOUR_DEVELOPER_KEY");
				music.addEventListener(MusicEvent.LOGIN_SUCCESSFULL, onLogin);
				music.addEventListener(MusicEvent.LOGIN_ERROR, function():void { Alert.show("Error login"); } );
				music.login("YOUR_USERNAME", "YOUR_PASSWORD");
			}
			private function onLogin(event:MusicEvent):void
			{
				music.removeEventListener(MusicEvent.LOGIN_SUCCESSFULL, onLogin);
				music.addEventListener(ArtistsResultEvent.ARTIST_RESULT_COMPLETED, onArtistsResult);
				music.addEventListener(ArtistsResultEvent.ARTIST_RESULT_ERROR, function():void { Alert.show("Error getting artist list"); });
				music.getMusicByArtists();
			}
			private function onArtistsResult(event:ArtistsResultEvent):void
			{
				music.removeEventListener(ArtistsResultEvent.ARTIST_RESULT_COMPLETED, onArtistsResult);
				var artistList:ArtistListVO = new ArtistListVO(event.artistList.list.source);
				var item:ArtistItemVO = artistList.getItem(0);
				music.addEventListener(AlbumDataEvent.ALBUM_DATA_COMPLETED, onAlbumDataComplete);
				music.addEventListener(AlbumDataEvent.ALBUM_DATA_ERROR, function():void { Alert.show("Error getting album list"); });
				music.getAlbumData(item.artistId);
			}
			private function onAlbumDataComplete(event:AlbumDataEvent):void
			{
				music.removeEventListener(AlbumDataEvent.ALBUM_DATA_COMPLETED, onAlbumDataComplete);
				music.removeEventListener(AlbumDataEvent.ALBUM_DATA_ERROR, onAlbumDataComplete);
				var albumList:AlbumListVO = new AlbumListVO(event.collection.list.source);
				var albumId:String = albumList.getItem(0).albumId;
				music.addEventListener(TrackDataEvent.TRACK_DATA_COMPLETED, onTrackDataComplete);
				music.addEventListener(TrackDataEvent.TRACK_DATA_ERROR, function():void { Alert.show("Error getting track data"); });
				music.getTrackData(albumId);
			}
			private function onTrackDataComplete(event:TrackDataEvent):void
			{
				music.removeEventListener(AlbumDataEvent.ALBUM_DATA_COMPLETED, onAlbumDataComplete);
				music.removeEventListener(TrackDataEvent.TRACK_DATA_ERROR, function():void { Alert.show("Error getting track data"); });
				var trackList:TrackListVO = new TrackListVO(event.collection.list.source);
				var trackItem:TrackItemVO = trackList.getItem(0);
				playSong(trackItem.downloadURL);
			}
			private function playSong(url:String):void
			{
				var sound:Sound = new Sound();
				var req:URLRequest = new URLRequest(url);
				sound.load(req);
				sound.play();
			}
		]]>
	<Script>
<FxApplication>
</p>
21
Feb

Ant task to generate SWC and ASDOC

Ant task to generate SWC and ASDOC for a library.

First create a property file to store location of your sdk and other information regarding your application: asdoc.properties - set properties file;


###################################################################
# Author: Elad Elrom - creates ASDOC and SWC
###################################################################
# Location of sdks directories
sdk.home 	 = /Users/user/Applications/Adobe\ Flex\ Builder\ 3\ Plug-in/sdks
# Location of sdk version
sdk.dir      = ${sdk.home}/3.2.0
# Location of asdoc.exe
asdoc.exe    = ${sdk.dir}/bin/asdoc
# ASDOC information
src.dir      = /Users/user/Documents/workspace/Library
doc.dir		 = /Users/user/Documents/workspace/Library
doc.src		 = ${doc.dir}/src/com/elad/API
output.dir   = ${doc.dir}/asdoc
# Libpath location to be included
libpath		 = ${doc.dir}/libs
# file name and folder
swc.dir		 = /Users/user/Documents/workspace/Library/swc
swcfilename	 = library.swc
FLEX_HOME	 = ${sdk.dir}
# ASDOC title for html pages
main.title   = ASDOC Title
window.title = ASDOC Title
</p>

Next, create the ant task to generate the asdoc and SWC file. Notice that I placed the flexTasks.jar in my local library, but feel free to point to it directly.


<project name="ASDoc build" default="main" >
	<!-- make sure that flexTasks.jar which is located in skd/ant/lib/flexTasks.jar otherwise you'll get an error message -->
	<taskdef resource="flexTasks.tasks" classpath="libs/flexTasks.jar" />
	<!-- defines all values for the ASDoc compiler -->
<property file="asdoc.properties" />
    <!-- main target: cleans and compiles ASDocs -->
    <target name="main" depends="clean-asdoc-directory, create-docs, clean-swc-directory, swc-generator" />
    <!-- deletes and recreates the asdoc directory -->
    <target name="clean-asdoc-directory" >
       <delete dir="${output.dir}" />
       <mkdir  dir="${output.dir}" />
    </target>
    <!-- deletes and recreates the swc directory -->
    <target name="clean-swc-directory" >
       <delete dir="${swc.dir}" />
       <mkdir  dir="${swc.dir}" />
    </target>
    <!-- runs the asdoc.exe compiler on the source -->
    <target name="create-docs" >
        <exec executable="${asdoc.exe}" failonerror="true" >
        	<arg line="-external-library-path='${libpath}/'" />
            <arg line="-doc-sources ${doc.src}" />
            <arg line="-output ${output.dir}" />
        </exec>
    	<echo>ASDOC created successfully</echo>
    </target>
	 <!-- generate the swc -->
	<target name="swc-generator" description="Compile the SWC file">
		<compc output="${swc.dir}/${swcfilename}">
			<include-libraries file="${libpath}/" />
			<!-- include our Class packages into the build (com folder) -->
			<include-sources dir="${doc.dir}/src/com" includes="*" />
		</compc>
		<echo>SWC created successfully</echo>
	</target>
</project>
</p>