Notes on Building an application to play videos: php and MySql



Building an application with Flash, php and MySql to play videos using database information

The sample (Flash ActionScript 3.0) application is a work-in-process that presents the viewer with ways to choose video clips. Information on the video clips is maintained in a MySql database. The database is set up and maintained with a set of html and php programs separate from the Flash programs.

The interface for the user is created dynamically. Specifically, the Flash application invokes php programs to populate the dropdown menus. The chosen video clip (an .flv file located on the server) is played in an FLVPlayer. For this sample application, one video clip was already in .flv format. The others were produced using Adobe Flash CS3 Video Encoder to produce .flv files from files of type.avi or .wmv.

I first describe the application in its current form. There are two interfaces (two sets of .fla file and .as file), one called choosevideoplay and the other tagdisplay. Each one uses information from the database to present an interface to a user. NOTE: the colored squares were used to make sure I had the most up-to-date version. It is not necessary for the applications to work.

choosevideoplay

The first screen is:

[pic]

Each video belongs to a group. In the sample application, the program goes to the database to obtain a list of the distinct groups. This is displayed by the pull-down menu:

[pic]

At this point, the database only has videos in 2 groups: Hudson Trail and robotics. Clicking n Hudson trail produces:

[pic]

Clicking on the Videos pulldown produces:

[pic]

Clicking on On a snowy day causes the corresponding video to be played:

[pic]

Similarly, choosing the robotics group produces a different list:

[pic]

tagdisplay

This application creates a text field of the tags. This can be thought of as a cloud. The size of font is dependent on how many clips use that tag. In this very small test, there are only 5 tags, two of which are used twice and so appear bigger then the 3 tags used only once.

[pic]

If the user clicked on hiking, the following screen appears:

[pic]

Clicking on the ComboBox with label Videos, produces:

[pic]

Clicking on More winter day produces:

[pic]

I will now go back to the start to describe the creation of the database, the creation of tables, adding or modifying records in the tables, and deleting entries.

Databases

Databases are made up of tables. Tables are made up of records. Records are made up of fields. Each field is of a fixed type, such as integer or characters. Generally, one field in a record is a unique identifier, termed the primary key. Sometimes the primary key is two fields. A field in one record can refer to another record in the same or another table. This application has two tables: one has a record for each video, with the name and extension being the primary key; the other table has a record for each tag and video pair. Making a table for the tags is necessary because there are a variable number of them for each video clip.

Coming up with a database design is a challenging process and, often, takes repeated attempts. (Note the table name videoclips2. There was a videoclips. I kept it around when working on an improved version.) The video clip application is a teaching example for MySql and php, the connection between Flash and php, and also dynamic creation of interface elements using ActionScript. The application in its current form does not use all the data.

Here are the contents of the videoclips2 table (the output of showtable2.php):

[pic]

Note: the Date field is simply a character string. It is not used in the two applications nor is sequence. If and when it is used, I advise checking if MySql has a date data type.

The contents of the taggedclips table (produced by showtagtable2.php):

[pic]

The different database products share many features, most importantly the use of Structured Query Language (SQL). Sample SQL statements are

|SELECT * FROM videoclips2 |Generate a (temporary) table of all the fields from all the |

| |records in the table videoclips |

|SELECT fileaddr, cliptitle FROM videoclips2 where |Generate a table holding the 2 fields from all the records in |

|clipgroup='Hudson Trail' |which the clipgroup field is 'Hudson Trail' |

|INSERT INTO videoclips2 VALUES ('sumo.flv','Sumo Vs. Grabber', |Add to the table videoclips2 a new record with the indicated |

|'05/2007','robotics,1) |values |

|UPDATE videoclips2 SET cliptitle = 'Sumo against Grabber' when |Update (change) the cliptitle field in the record with fileaddr =|

|fileaddr = 'sumo.flv' |to 'sumo.flv' to the [new] value 'Sumo against the Grabber' |

|SELECT v.fileaddr, v.cliptitle FROM videoclips2 as v JOIN |Using 2 tables, get the pairs of fileaddr and cliptitle for all |

|taggedclips as t ON v.fileaddr=t.fileaddr WHERE tag='hiking' |clips that have the tag 'hiking'. |

Of course, in most cases, some of the values in the SQL statements will be held in variables. You will see this in the code shown below.

Implementation

Though much of this discussion could apply to other implementations, from here on the discussion is about specific software: MySQL for the database and php for the server-side (aka middleware) language.

Before continuing, it is important to understand what is sometimes termed the 3 layers of these types of applications. The MySQL database is on a server: for me this is newmedia.purchase.edu. Creating the database; adding, deleting and modifying records; and querying the database, is done using php. The php programs also run on the server. In fact, they are located in my folders. The display of the interface to the user is done on the user's computer, also termed the client computer. Typically, the applications involve html files or (Flash) swf files downloaded to run on the client computer that invoke php programs (also called scripts) that run on the server that access the MySql engine to read and write to the database tables. These php files typically produce either HTML or input for the Flash program. So the typical flow of control is client (html or swf) to server (php) to server (MySql) back to server (php) back to client (html or swf).

Note: we are accustomed to developing and testing out html and Flash applications on our own computers and then uploading the finished product to the Web. For an application involving MySQL, it is possible to set up your computer as a server to do the development. However, I found doing that setup is fairly difficult. I find it easier to upload (ftp) the files to the server and test them on the server. That being said, it isn't easy: the cycle is write, upload (using an ftp program such as Filizilla), test and then repeat. You need to make sure that you have uploaded all the latest files.

To build this application, you need to arrange permissions with your Internet Service Provider to MySql and php. At my school, someone sets up a database (no tables yet) and supplies me with a user id and a password. Someone at my institution (his name is Mike) worked directly on the server to set up the (empty) database. In the code shown here, I have removed the user id, the password and the database name from the programs.

The php language has a few special features. You will see all these in the code.

• A php file often has html in it. The html is passed back to the browser as is. The php is indicated by the delimiters .

• The print or echo command is used to produce text that is interpreted as html by the browser or interpreted by Flash.

• All variables names start with a $ sign.

• Quoted strings can contain variable names and the value of the variable is substituted to form the value of the string.

• The period [.] is used for concatenation of strings.

• Since SQL expects strings to have quotes, many php statements have single quotes inside of double quotes.

Here is an example of a php statement that creates a query in the variable $query for use with MySQL:

$query = "UPDATE videoclips2 SET cliptitle='" . "$ct" . "' where fileaddr='$fn'";

The $ct and the $fn are variables and both their values are used to make up the expression assigned to $query. Let us say the $ct is "Hello, there" and $fn is "river.flv", then the value assigned to $query is

"UPDATE videoclips2 SET cliptitle='Hello, there' where fileaddr='river.flv'"

Leaving out one of these internal single quotation marks is a common mistake.

Here are the files (so far) for the application. Before jumping to the code, I strongly suggest looking at the tables to get a general overview. Also, when looking at the code, if you are reading this on-line, make use of the Edit/Find command to check out when variables or methods are used.

|File |Purpose |

|maketable2.php |Creates the videoclips table |

|maketagtable2.php |Creates the taggedclips table |

|addclip2t.html |Presents a form and sets up a call to addtotableform.php |

|addtotableform2t.php |Does the actual work of inserting a new record or modifying an |

| |existing record |

|showtable2.php |Show the videoclips table |

|showtagtable2.php |Show the taggedclips table |

|deleteall.php |Removes all records from both tables: this is used during the |

| |development process. |

|choosevideoplay.fla |Flash file, with document Video3.as |

|Video3.as |Generates the pulldown menus and invokes calls to getgroups.php |

| |and getgroupclips.php |

|getgroups.php |Gets the set of distinct groups mentioned in the videoclips table|

|getgroupclips.php |Gets information on all videos whose record in videoclips has the|

| |indicated group |

|tagdisplay.fla |Flash file, with document Tag.as |

|Tag.as |Generates call to gettags.php, setting up tag field and |

| |gettagclips.php. (The application goes to the database first to |

| |set up the tags in a text field and then again for the sets of |

| |clips for each tag.) |

|gettags.php |Gets all the tags cited in the taggedclips table. |

|gettagclips.php |Get the file address and file title for all the clips with a |

| |specified tag |

NOTE: the html and the php files were created and edited using Dreamweaver. The .as and the .fla files are created in Flash.

maketable2.php

maketagtable2.php

addclip2t.html

Form for Entering Clip data

For new entry:

Enter clip file name, title, date, the associated group, sequence, and up to 3 tags.

For changes, or new tags (any old tags remain): enter clip file name, changed fields, new tags.

Clip file name

Clip title

Clip date

Clip group

Sequence number

Tag:

Tag:

Tag:

addtotableform2t.php

Add film clips

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download