30 September 2007
You should be familiar with ActionScript 2 and PHP.
Additional requirements
Flash Lite 3 player
A web server supporting PHP with full access to the <cURL> resource
Advanced
Many Flash developers already know how to play an FLV and to stream it to the browser-based Flash Player. In this article, I will show you how to target the player to play FLV videos from YouTube using Flash Lite 3.
The hard part of doing this is around the YouTube website, which "hides" FLV files from downloading. However, the YouTube site can't really hide the position of the file. So what I do with this technique is read from YouTube page, parse the values, and pass the result to the player to stream the file.
A few notes about streaming files:
The technique I use here includes building the app, grabbing YouTube IDs, parsing and establishing the connection.
To get started, you need to create a new Flash file and create some layer. Follow these steps:
Follow these steps to build the interface:
3. Apply the following code to Frame 1 in the actionscript layer. You will need to change both the http://yourserver.com/leggi.php and the http://yourserver.com/get.php strings in this code to match the path of your web server to call leggi.php and get.php.
stop();
var TIMER = 0; //Buffer Timer checking
var searchedW:String="adobe"; //Searching String
var scelto=""; //Movie id
// NetConnection, NetStream Objects
var nc:NetConnection = new NetConnection();
nc.connect((null));
var ns:NetStream = new NetStream(nc);
//Forward Button
avanti.onRelease = function() {
createArea(); // To create the video list area
nextFrame();
};
// Video List Area
function createArea() {
//---Font Format
var my_fmt:TextFormat = new TextFormat();
my_fmt.bold = false;
my_fmt.font = "Arial";
my_fmt.size = 8;
my_fmt.color = 0xFFFFFF;
for (i=0; i< 14; i++) {
createEmptyMovieClip("but" + i, _root.getNextHighestDepth());
eval("but" + i).beginFill(0xFF0000, 80);
eval("but" + i).moveTo(0, 0);
eval("but" + i).lineTo(100, 0);
eval("but" + i).lineTo(100, 10);
eval("but" + i).lineTo(0, 10);
eval("but" + i).lineTo(0, 0);
eval("but" + i).endFill();
eval("but" + i)._x=2;
eval("but" + i)._y=(i*11);
eval("but" + i).createTextField("label", this.getNextHighestDepth(), 0, -2, 100, 12)
eval("but" + i).label.setNewTextFormat(my_fmt);
eval("but" + i).onRelease = function() {
scelto=files[this._name.substr(3,1)];
getN.load("http://yourserver.com/get.php?v=" + scelto);
searchedW=cercastr.text;
};
eval("but" + i).label.text = filetime[i] + " - " + filedesc[i];
}
}
files = new Array(); // Files YouTube IDs - E.g. "hcAh-AxXex8"
filedesc = new Array(); // Descriptions
filetime = new Array(); // Time
lv = new LoadVars();
lv.contentType = "text/plain"; // handling Content in text format
lv.onLoad = function() {
fl = this.filelist;
files = fl.split(",");
c = files.length-1; // Amount data searched usually 20 items for page
fl = this.filedesc;
filedesc = fl.split(",");
fl = this.filetime;
filetime = fl.split(",");
c = 14; // Setting 14 to reduce the search list displayed
for (i=0; i<c; i++) {
if (filetime[i] <> undefined ) { //Checking if the time returned is null
eval("but" + i).label.text = filetime[i] + " - " + filedesc[i];
} else {
eval("but" + i).label.text = "";
}
}
};
//Call to return the Video List parsed
lv.load("http://yourserver.com/leggi.php?cerca=" + cercastr.text);
Use these steps to add the background and search capability:
stop();
var c:Number=0; //Reset the counter list
cercastr.text= searchedW; //Assigning the searched word
//----------------------------------
//Extracting the t ID
linkaggio = new Array(); //
getN = new LoadVars();
getN.contentType = "text/plain";
getN.onLoad = function(success) {
fl = this.t;
linkaggio = fl.split(",");
hideM(); //hiding the search list
TIMER = setInterval(updateOutput, 10); //Check buffer status
nextFrame();
};
//Get buffer infos
function updateOutput()
{
tempobuf.text = ns.bufferTime;
riman.text = ns.bufferLength;;
}
//Hide video list area
function hideM()
{
for (g=0; g < 14; g++) {
eval("but" + g)._visible= false;
}
}
//Execute Search
cerca.onRelease = function() {
_root.lv.load("http://yourserver.com/leggi.php?cerca=" + cercastr.text);
}
Use these steps to build Frame 3:
4. Apply the following code to Frame 3 the actionscript layer:
var af:String= "http://www.YouTube.com/get_video?video_id=" + scelto +"&t=" + linkaggio[0];
video1.attachVideo(ns);
ns.setBufferTime(20);
ns.play(af,-2,0);
ns.onStatus = function(iObj:Object) {
//ns.setBufferTime(5);
trace(iObj.code);
switch (iObj.code) {
case "NetStream.Buffer.Full":
ns.setBufferTime(5);
tempobuf.text=2;
break;
case "NetStream.Buffer.Empty":
ns.setBufferTime(20);
tempobuf.text=5;
break;
case "NetStream.Play.StreamNotFound":
ns.close();
clearInterval(TIMER);
showM()
prevFrame();
break;
}
}
indietro.onRelease = function() {
clearInterval(TIMER);
ns.close();
showM()
prevFrame();
}
function showM() {
for (g=0; g < 14; g++) {
eval("but" + g)._visible= true;
}
}
There are two PHP web pages used in the application. You will find these in the sample files linked from the beginning of the article.
get.php
leggi.php
Leggi.php is used to read and parse vID, descriptions, and time from the video list search generated from YouTube. Once you choose the video, vID is sent to get.php page to get the last id, tID, which will give you the exact position of the FLV file.
Some notes on the PHP files. Open the two PHP files in a text editor and take a look at the code. To get the IDs:
&t= parameter. You will notice this is long 32 characters long. For example, &t=OEgsToPDskJ_KveEss25md56sSdImmHA Figure 4 shows a simulation of the final application.
Follow these steps to deploy your application: