In this final installment of my tutorial on YouTube hacking, I’m going to show how to build the FLV URL for YouTube videos and start streaming the FLV in your ActionScript application. As you will notice, this is very little work, because it is based on the streaming example in the AS3 language reference. I’m being lazy.
A recap of the ActionScript 3.0 YouTube player series:
playing the video using Flash’s FLV player classes [this post]
Constructing the YouTube video URL
There are several ways to get to this one. You could fire up FireBug and check the network logs, or you could check Shane’s code and open his Main.as class. Skip to line 67 and you’ll see:
What this means: to construct the full URL to a YouTube Flash video file, you take the base URL and add both the video id and the T-parameter as arguments. An example URL might look like this:
What happens might not be clear when you look at the code. That’s because we use a lot of event handlers. But if you take your time and go over the code it will become clear. What goes on is this:
We create a NetConnection.
If this goes well the netStatusHandler is called with the event “NetConnection.Connect.Success”. This in turn will launch the YouTube query to locate a video and create the URL to play it.
This part we’ve already discussed in previous posts:
Create the YouTube object and add listeners.
Get a list of videos for a tag.
When that lists returns, select the first one and get the T-parameter from our PHP script
We construct the YouTube FLV URL (see above) and call the play method.
The play method creates a NetStream and Video object and starts the play the video.
Everything else you see is basically code to handle possible errors:
In this 5th and final post of the series I have shown how to actually stream the YouTube FLV. It was particularly easy because Flash already has everything you need. It’s a matter of finding the right classes and using them.
That wraps up the 5 part tutorial. Feel free to leave a comment if you found this useful or not. I’m also open for suggestions for improvements or other subjects for future tutorials.
Great notes, best You tube approach and easy to download you zip and run! Thank you.
I get this error: Error #2044: Unhandled securityError:. text=Error #2048: Security sandbox violation: http://localhost/PageAppDep/Main.swf cannot load data from http://code.flashdynamix.com/YouTube/getVideoId.... at com.flashdynamix.net::DataLoader/load()[D:\workspaceC\pcn\src\com\flashdynamix\net\DataLoader.as:17] at com.flashdynamix.services::YouTube/loadData()[D:\workspaceC\pcn\src\com\flashdynamix\services\YouTube.as:170] at com.flashdynamix.services::YouTube/videoId()[D:\workspaceC\pcn\src\com\flashdynamix\services\YouTube.as:166] at Main/onLoaded()[D:\workspaceC\pageApp\src\Main.as:79]
When I run it local swf it runs. When I make html and run from server in a browser same swf... I get error. help?
It's odd that you are seeing this when running the program locally, but it certainly is related to the same origin policy. I've explained this a bit in the previous post: http://www.streamhead.com/tying-youtube-service... Are you running this on a local server? Or are you loading the swf directly in your browser? If it's the first, try the second: from the explorer, drag the Main.swf file to your browser and see what happens. If that doesn't solve your problem, try to deploy the PHP script locally (you'll need an Apache server for this, so it's not for the faint of heart :) ) Let me know if any of the suggestions help and if not, I'll try to offer a few more.
The Lazy ActionScript Developer’s Way to Stream Flash Video
In this final installment of my tutorial on YouTube hacking, I’m going to show how to build the FLV URL for YouTube videos and start streaming the FLV in your ActionScript application. As you will notice, this is very little work, because it is based on the streaming example in the AS3 language reference. I’m being lazy.
A recap of the ActionScript 3.0 YouTube player series:
Constructing the YouTube video URL
There are several ways to get to this one. You could fire up FireBug and check the network logs, or you could check Shane’s code and open his Main.as class. Skip to line 67 and you’ll see:
What this means: to construct the full URL to a YouTube Flash video file, you take the base URL and add both the video id and the T-parameter as arguments. An example URL might look like this:
http://www.youtube.com/get_video?video_id=v6ICEpYEcD8&t=OEgsToPDskIltmXs7gZ89kc2a3Z1SRGN
(I have not linked the URL, as it probably won’t work, because the T-parameter changes over time)
Streaming the FLV
For this one I’m going to use the existing ActionScript 3 classes. In particular flash.net.NetConnection and flash.net.NetStream. In the following code I’m adding the language reference example to the already existing code.
What happens might not be clear when you look at the code. That’s because we use a lot of event handlers. But if you take your time and go over the code it will become clear. What goes on is this:
Everything else you see is basically code to handle possible errors:
I’ve put all files we used in the tutorials in a handy zip file. Just expand and open in FlashDevelop.
Conclusion
In this 5th and final post of the series I have shown how to actually stream the YouTube FLV. It was particularly easy because Flash already has everything you need. It’s a matter of finding the right classes and using them.
That wraps up the 5 part tutorial. Feel free to leave a comment if you found this useful or not. I’m also open for suggestions for improvements or other subjects for future tutorials.
Image credit.