Posts Tagged ‘Actionscript’

AS3FlexDB

Friday, January 16th, 2009

I think it’s time to write a quick and short  post about my flex library. I will only write a short description and some simple usage example. For more explicit tutorials see iTutorials or Sephiroth.

What is AS3FlexDB ?

AS3FlexDB is an open source library that allows Adobe Flex applications to connect to a MySQL server. This library use AMFPHP to acess a MySQL server.

Why AS3FlexDB ?

So before I start describe the lib let me tell you how this project got live. The ideea of this project was a simple question “Can I have all my SQL’s in AS3 and not in PHP?” how useful can that be !  To be more specific what I was wishing was a library that allow me to manipulate a MySQL Database in AS3 no need for me to write any PHP code or other server side code. So this is why? i have chose to write this library.

How AS3FlexDB execute an SQL Statement ?

So what AS3FlexDB do in background ? The flow of events from your code to MySQL is very simple. AS3FlexDB use “RemoteObject” class to access a AMFPHP service. The AMFPHP service more exactly the php “Database” class execute the SQL and return the results to Flex.

Take a look at next diagram :

As3FlexDB Arhitecture

Now you know what is AS3FlexDB, why I have develop AS3FlexDB and how it works the next important question you must put yourself is “ How can I install and use AS3FlexDB ? “

Install AS3FlexDB

To install AS3FlexDB all you have to do is to install AMFPHP (look here for a tutorial if you don’t know how) copy the file database.php (you can find this file in AS3FlexDB Kit) in your AMFPHP service directory and import in Flex as3flexdb_version.swc library and you are done.

Use AS3FlexDB

You can use AS3FlexDB entirely from MXML or from actionscript.  So first let’s try SELECT all users from a database with MXML. The code for this is :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<mx:Application
	layout="vertical"
	xmlns:phi="phi.db.*"
	xmlns:mx="http://www.adobe.com/2006/mxml">
 
	<mx:DataGrid id="dg1" width="100%" height="100%" dataProvider="{q1.Records}">
		<mx:columns>
			<mx:DataGridColumn dataField="fname" headerText="First Name"/>
			<mx:DataGridColumn dataField="lname" headerText="Last Name"/>
			<mx:DataGridColumn dataField="password" headerText="Password"/>
		</mx:columns>
	</mx:DataGrid>
 
 
 
	<phi:ConnectionData id="c1" name="mxml_conn1"
						host="localhost"
						db="test"
						username="root"
						password="root" />
 
	<phi:Database id="db1" connection="{c1}" />
	<phi:Query id="q1" database="{db1}" q="SELECT * FROM users WHERE 1" />
	<phi:QueryExecute id="q1ex" query="{q1}" />
 
</mx:Application>

If you want to select all users from database using AS3 the code for that will look like this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/** Database */
import phi.interfaces.IQuery;
import phi.interfaces.IDatabase;
import phi.db.Database;
import phi.db.Query;
/** */
 
private var db       :IDatabase;
private var query    :IQuery;
 
private function onCreateComplete():void
{
	db = Database.getInstance();
	query = new Query();
 
	db.connect("conn1", "root", "", "localhost", "flexdb", true);
	query.connect("conn1", db);}
 
private function selectUsers():void
{
	query.addEventListener(Query.QUERY_END, queryEnd);
	query.execute("SELECT * FROM users WHERE 1");
}
 
private function queryEnd(evt:Object):void
{
	users = query.Records();
}

I think now you have a very solid ideea about what this library do. So if you think it’s what you need or you want to read more documentations the next links can help you.

Documentation | Download

If you want to join the project visit project home page here.
Until next post I will close this post with one of my favorite quote:

“Talent does what it can, genius does what it must.”

Download a movie from YouTube with Flex

Saturday, January 3rd, 2009

If you search in Google “download a youtube video” you will find a lot of tools that help you to download a video from YouTube but if you want to make your own application in Flex that download a YouTube movie or you just want to use the VideoDisplay component from Flex to play a YouTube movie this is the post you must read. So this app will be totaly client no use of server side. We will do all the work in Flex.

So let’s start!

First let’s study how we can download a movie and after that we will write our code in Flex. For example if we want to download this movie from YouTube (gread commercial my favorite one :D), all we have to do is to paste in our browser this link  http://youtube.com/get_video.php?video_id=670X2MCWzK0&t=OEgsToPDskIOEdLV0E46VwO32hMEmKaT

Let’s break the link:

1.  http://youtube.com/get_video.php
2.  video_id=670X2MCWzK0
3.  t=OEgsToPDskIOEdLV0E46VwO32hMEmKaT

we can see that this link has three parts. A link and two parameters. First parameter the “video_id” is the same with the “v” parameter from first link so we can get this easy but how do we get the second parameter “t” ? Let me present you the magic link :D http://youtube.com/get_video_info?video_id=670X2MCWzK0. Maybe you will ask how do I know about this link, the answere is simple I have used a very simple flash decompiler and decompile the YouTube player and that’s where I have find the link. The “video_id” in that link is the same with the “v” parameter. So with thatk link and video_id we can get all the information we need to download a movie. So the steps are:

1. From a YouTube link we extract the value of “v” parameter this is the “video_id”. (In our example 670X2MCWzK0)
2. Get video information (title, thumbnail, token) with youtube.com/get_video_info.php and video_id
3. Get video with youtube.com/get_video.php, video_id and t (t means token)

Step 1. Extract video_id from YouTube URL

private function parseURL(url:String):String
{

     var reg:RegExp = /youtube\.com\/watch\?v=([^&\/]+)/;
     var res:Object = reg.exec(url); 

     return res[1];

Step 2. Get video information

private const GET_VIDEO :String = “http://youtube.com/get_video.php”;
private const GET_VIDEO_INFO :String = “http://youtube.com/get_video_info.php”;

private function download():void
{

     var service :HTTPService = new HTTPService();
     videoID = parseURL(siteUrl.text); // siteUrl is a mx:TextInput
     
     service.url = GET_VIDEO_INFO + “?video_id=” + videoID;
     service.resultFormat = ‘object’;

     phiBusy.showBusy();

     service.addEventListener(ResultEvent.RESULT, onLoadVideoInfoComplete);
     service.send();

}

Step 3. Get Video

private function onLoadVideoInfoComplete(evt:ResultEvent):void
{
     phiBusy.removeBusy();

     var reg:RegExp = /token=([^&\/]+)/;
     var token :Object = reg.exec(evt.result);

     downloadVideo(token[1]);
}

private function downloadVideo(token:String):void
{
     navigateToURL(new URLRequest(GET_VIDEO + “?video_id=”+ videoID +”&t=”+ token));
}

And we are done. I have used navigateToURL to open a new window and download the video but if you provide that link to a VideoDisplay component the video will play.

View the example | View Source