Embedding youtube video to Flash project and Resizing it in AS3 - actionscript-3

Hello i'm trying to embed a youtube video into my flash project using as3.0. So far I've been able to get the video to my frame and play it. But my stage size is 500X500. So I need my video to be width:300 and height:150. I tried changing the video size but that's not worked for me. The video has it's original embedding width and height... So how can I do that?
Here is my code.
var player:Object;
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
context.securityDomain = SecurityDomain.currentDomain;
context.applicationDomain = ApplicationDomain.currentDomain;
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
Security.allowDomain("www.youtube.com")
loader.load(new URLRequest("http://www.youtube.com/v/yNfkNWfH6aU?version=3"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
loader.content.addEventListener("onError", onPlayerError);
loader.content.addEventListener("onStateChange", onPlayerStateChange);
loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
}
function onPlayerReady(event:Event):void {
// Event.data contains the event parameter, which is the Player API ID
trace("player ready:", Object(event).data);
// Once this event has been dispatched by the player, we can use
// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
// to load a particular YouTube video.
player = loader.content;
// Set appropriate player dimensions for your application
player.setSize(300, 150);
//player.x = stage.width/2; //puts the video horizontally centered
//player.y = stage.width/2; //puts the video vertically centered
//player.x=100;
//player.width=265;
//player.height=200;
}
function onPlayerError(event:Event):void {
// Event.data contains the event parameter, which is the error code
trace("player error:", Object(event).data);
}
function onPlayerStateChange(event:Event):void {
// Event.data contains the event parameter, which is the new player state
trace("player state:", Object(event).data);
}
function onVideoPlaybackQualityChange(event:Event):void {
// Event.data contains the event parameter, which is the new video quality
trace("video quality:", Object(event).data);
}

Related

How to display mp4 then pause and replay in Flash CC

I’m resurrecting a simple banner where we used an FLV movie and the following code to have the movie pause using setInterval and then play again. It’s a simple butterfly movie that flaps it’s wings and then goes back to a static state.
Looks like FLVs are now discouraged in the more recent Flash versions so I would like to know how to make a movie play, then pause using setInterval, then replay.
Here is the working AS3 when using an FLV on the Timeline in a frame based animation but does not work with an imported .mp4:
stop();
function timesUp()
{
gotoAndPlay(1,"Scene 1");
clearInterval(itv);
}
var itv=setInterval(timesUp, 15000);
Do the new suggested methods use a single frame movie where you load and play the mp4, then use setInterval as a timer to replay?
I can’t find any tutorials that have this method as an example.
I did read a tutorial on the Adobe site but the following did not load and play the movie (morpho.mp4)
var nc:NetConnection = new NetConnection();
nc.connect(null);
var vid:Video = new Video();
addChild(vid);
var ns:NetStream = new NetStream(nc);
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void
{
// handle netStatus events, described later
}
function asyncErrorHandler(event:AsyncErrorEvent):void
{
// ignore error
}
vid.attachNetStream(ns);
ns.play("morpho.mp4");
Try this :
import flash.utils.Timer
import flash.events.TimerEvent
var video_playing:Boolean = false,
timer:Timer,
nc:NetConnection,
ns:NetStream,
video:Video,
video_name:String = 'video.mp4'
nc = new NetConnection()
nc.connect(null)
ns = new NetStream(nc)
ns.addEventListener(NetStatusEvent.NET_STATUS,netStatusHandler);
ns.addEventListener(AsyncErrorEvent.ASYNC_ERROR, asyncErrorHandler);
function netStatusHandler(event:NetStatusEvent):void {
switch (event.info.code){
case 'NetStream.Play.Start' :
if(timer.currentCount == 0){
timer.start()
}
break
}
}
function asyncErrorHandler(event:AsyncErrorEvent):void {
}
video = new Video(135, 135) // set the size of video to 135x135px
video.x = 165 // set the left of video to 300 - 135 = 165px
video.y = 0 // set the top of video to 0
video.attachNetStream(ns)
addChild(video)
timer = new Timer(1000, 3) // 3 seconds, just for example
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timer_on_Complete)
function timer_on_Complete(e:TimerEvent){
if(video_playing){
video_playing = false
ns.close()
} else {
start_video()
}
timer.reset()
timer.start()
}
start_video()
function start_video(){
ns.play(video_name)
video_playing = true
}

Actionscript 3 auto mouse click on XY stage coordinates

Is there any way to auto-click on XY stage coordinates upon when SWF is loaded?
EDIT: By the way, this is the whole code, and it is a cued youtube video. Just want to click anywhere on the cue to play it automatically after SWF loading.
Security.allowDomain("www.youtube.com");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
loader.content.addEventListener("onError", onPlayerError);
loader.content.addEventListener("onStateChange", onPlayerStateChange);
loader.content.addEventListener("onPlaybackQualityChange",
onVideoPlaybackQualityChange);
}
function onPlayerReady(event:Event):void {
trace("player ready:", Object(event).data);
player = loader.content;
player.setSize(300, 250);
player.cueVideoById("AfTCtRDsWVw",0);
player.mute();
}
function onPlayerError(event:Event):void {
trace("player error:", Object(event).data);
}
function onPlayerStateChange(event:Event):void {
trace("player state:", Object(event).data);
}
function onVideoPlaybackQualityChange(event:Event):void {
trace("video quality:", Object(event).data);
}
Yes, you should just manually dispatch your MouseEvent.CLICK Event to your stage:
var yourX:Number = 40; //Whatever you want
var yourY:Number = 40; //Whatever you want
//Make sure you have a link to the stage (usually after Event.ADDED_TO_STAGE event occured)
stage.dispatchEvent(new MouseEvent(
MouseEvent.CLICK,
true,
false,
yourX,
yourY);
If you need a certain DisplayObject to relate to this MouseEvent, you should pass an additional parameter to the MouseEvent constructor:
var yourX:Number = 40; //Whatever you want
var yourY:Number = 40; //Whatever you want
//Make sure you have a link to the stage (usually after Event.ADDED_TO_STAGE event occured)
stage.dispatchEvent(new MouseEvent(
MouseEvent.CLICK,
true,
false,
yourX,
yourY,
linkToYourRelatedDisplayObject);
==UPDATE==
Well, I've made it. A bit of "hacking" and here it is.
Your youtube player contains a safeLoader object, which contains a videoApplication, which contains a LargePlayerButton. This LargePlayerButton has a MouseEvent.CLICK event listener, so we need to dispatch out event to this button instead of dispatching it to the stage.
Here's the complete code with autoClick simulation:
import flash.events.MouseEvent;
import flash.events.Event;
import flash.display.DisplayObjectContainer;
// The player SWF file on www.youtube.com needs to communicate with your host
// SWF file. Your code must call Security.allowDomain() to allow this
// communication.
Security.allowDomain("www.youtube.com");
// This will hold the API player instance once it is initialized.
var player:Object;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
}
function autoClick():void
{
//=========================
//Some nested children, we need to dig through a bit to get to the LargePlayButton
var safeLoader:DisplayObjectContainer = (loader.content as DisplayObjectContainer).getChildAt(0) as DisplayObjectContainer;
var videoApplication:DisplayObjectContainer = safeLoader.getChildAt(0) as DisplayObjectContainer;
var largePlayBtn:DisplayObjectContainer = videoApplication.getChildAt(6) as DisplayObjectContainer;
//=========================
//And finally dispatching our event to this button. It will think that a person has clicked it
largePlayBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK,
true,
true,
stage.stageWidth / 2,
stage.stageHeight / 2));
}
function onPlayerReady(event:Event):void {
// Event.data contains the event parameter, which is the Player API ID
trace("player ready:", Object(event).data);
// Once this event has been dispatched by the player, we can use
// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
// to load a particular YouTube video.
player = loader.content;
// Set appropriate player dimensions for your application
player.setSize(300, 250);
player.cueVideoById("AfTCtRDsWVw",0);
//====================================================
//As long as player is loaded we can call our function
autoClick();
}
It was a nice exercise, cheers :)
Youtube seems to have an intelligent anti hack feature. I'm able to get the children of SafeLoader on the IDE, but on the flash player, or the web, the SafeLoader just isn't accessible, and no children after that can be controlled. This is why the code works on the IDE but not outside. Maybe it has to do with sandbox security, but maybe it's more complex.
I doubt very much that it is possible, for obvious security reasons : http://www.adobe.com/devnet/flashplayer/articles/fplayer10_uia_requirements.html

url request not working properly - video does not stop as3

I created an information kiosk on flash but I got a problem that is really annoying on loading my contents externally. I have exported all of my swf files and created a unique swf to load everything on it with the main menu on top. The main problem is streaming a video from youtube. It loads perfectly but when i navigate to another page, the page changes but the video does not stop if it has been played once.(the sound keeps playing). Here is my code:
loader1.fla:
// Container
var pageLoader:Loader = new Loader();
// Url Requests
var loadRequest1:URLRequest = new URLRequest("basics1.swf");
var loadRequest2:URLRequest = new URLRequest("climatechange.swf");
// Initial Page Loaded
pageLoader.load(loadRequest1)
addChild(pageLoader)
// Button Functions
function goHome (e:MouseEvent):void{
pageLoader.load(loadRequest1)
addChild(pageLoader)
}
hpage.addEventListener(MouseEvent.CLICK, goHome);
//go to climate change page
function climatePage (e:MouseEvent):void{
pageLoader.load(loadRequest2);
addChild(pageLoader);
}
climatep.addEventListener(MouseEvent.CLICK, climatePage);
climatechange.fla:
Security.allowDomain("http://www.youtube.com")
// load video
var pageLoader:Loader = new Loader();
// Url Requests
var loadRequest1:URLRequest = new URLRequest("overviewvideo.swf");
// Intial Page Loaded
pageLoader.load(loadRequest1)
addChild(pageLoader)
overviewvideo.fla:
/*youtube video*/
var player:Object;
var loader:Loader = new Loader();
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
context.securityDomain = SecurityDomain.currentDomain;
context.applicationDomain = ApplicationDomain.currentDomain;
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
Security.allowDomain("http://www.youtube.com")
loader.load(new URLRequest("http://www.youtube.com/v/6s8iiIFgPMU&list=PL9C6D9D2AF8999F85&index=12"));
function onLoaderInit(event:Event):void {
addChild(loader);
loader.x= 40;
loader.y=130;
loader.content.addEventListener("onReady", onPlayerReady);
loader.content.addEventListener("onError", onPlayerError);
loader.content.addEventListener("onStateChange", onPlayerStateChange);
loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
}
function onPlayerReady(event:Event):void {
// Event.data contains the event parameter, which is the Player API ID
trace("player ready:", Object(event).data);
// Once this event has been dispatched by the player, we can use
// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
// to load a particular YouTube video.
player = loader.content;
// Set appropriate player dimensions for your application
player.setSize(480, 260);
}
function onPlayerError(event:Event):void {
// Event.data contains the event parameter, which is the error code
trace("player error:", Object(event).data);
}
function onPlayerStateChange(event:Event):void {
// Event.data contains the event parameter, which is the new player state
trace("player state:", Object(event).data);
}
function onVideoPlaybackQualityChange(event:Event):void {
// Event.data contains the event parameter, which is the new video quality
trace("video quality:", Object(event).data);
}
Can anyone help? I'd be glad if someone can help me out. Thank you
Before you load a new swf into the loader, you need to first call:
pageLoader.unloadAndStop();
Attempts to unload child SWF file contents and stops the execution of commands from loaded SWF files. This method attempts to unload SWF files that were loaded using Loader.load() or Loader.loadBytes() by removing references to EventDispatcher, NetConnection, Timer, Sound, or Video objects of the child SWF file. As a result, the following occurs for the child SWF file and the child SWF file's display list:
Sounds are stopped.
Stage event listeners are removed.
Event listeners for enterFrame, frameConstructed, exitFrame, activate and deactivate are removed.
Timers are stopped.
Camera and Microphone instances are detached
Movie clips are stopped.
Docs at Adobe

Flash Youtube Embed Issue

Ive embedded a youtube video into a flash file with different frames for different pages, basically when i click off the page displaying my video, the youtube video is still on screen and the sound carrys on playing. I want the video only to be displayed on one frame, and the sound to stop when i switched frames. Here is my code:
Security.allowDomain("www.youtube.com");
var player : Object;
var mute : Boolean = false;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
playButton.addEventListener(MouseEvent.CLICK, playVideo);
pauseButton.addEventListener(MouseEvent.CLICK, pauseVideo);
muteButton.addEventListener(MouseEvent.CLICK, muteVideo);
function onLoaderInit(event:Event):void {
addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
}
function onPlayerReady(event:Event):void {
player = loader.content;
player.setSize(520, 265);
player.x = 230;
player.y = 370;
player.cueVideoByUrl("http://www.youtube.com/v/G-P9fO4g2Jc", 0);
}
function playVideo(event:MouseEvent):void {
player.playVideo();
}
function pauseVideo(event:MouseEvent):void {
player.pauseVideo();
}
function muteVideo(event:MouseEvent):void {
if(!mute){
player.mute();
mute = true;
return;
}
else {
player.unMute();
mute = false;
}
}
Thanks in advance
The addChild method adds child to the hole movieClip rather than to the currentFrame, actually there isn't way to add child to the give frame.
Possible solutions are:
Create empty sprite in the frame where you need to insert the player and use this sprite as the holder. Don't forget to stop the video when you leave the frame (you can listen the Event.REMOVED_FROM_STAGE event of player for example and stop video in handler).
add onEnterFrame event listener and check the currentFrame property, when it's the target frame add video otherwise remove video.

Certain functions in YouTube AS3 API return wrong values

I'm now working on a certain AS3 application. The code is pretty much the AS3 example from the documentation:
// The player SWF file on www.youtube.com needs to communicate with your host
// SWF file. Your code must call Security.allowDomain() to allow this
// communication.
Security.allowDomain("www.youtube.com");
// This will hold the API player instance once it is initialized.;
var player:Object;
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
function onLoaderInit(event:Event):void
{
stage.addChild(loader);
loader.content.addEventListener("onReady", onPlayerReady);
}
function onPlayerReady(event:Event):void
{
// Event.data contains the event parameter, which is the Player API ID
trace("player ready:", Object(event).data);
// Once this event has been dispatched by the player, we can use
// cueVideoById, loadVideoById, cueVideoByUrl and loadVideoByUrl
// to load a particular YouTube video.
player = loader.content;
player.loadVideoById("nJ3MSCLBpaM");
// Set appropriate player dimensions for your application;
setPlayerSize();
stage.addEventListener(Event.RESIZE, updatePlayerSize);
}
function setPlayerSize():void
{
player.y = 0;
player.x = 0;
player.setSize(stage.stageWidth, stage.stageHeight);
}
function getVideoBytesTotal()
{
return player.getVideoBytesTotal();
}
function getVideoBytesLoaded()
{
return player.getVideoBytesLoaded();
}
function getVideoStartBytes()
{
return player.getVideoStartBytes();
}
for some reason, when I call one of the last three functions after the video has loaded (and while it is playing) all I get is 0, from all of them. Why is that?
Those functions are deprecated.
Use
player.getVideoLoadedFraction()
instead.