Flash Youtube Embed Issue - actionscript-3

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.

Related

Embedding youtube video to Flash project and Resizing it in AS3

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);
}

Loop youtube clip on flash As3

I have this code which playing youtube movie in flash As3.
it working fine, but i can't make that movie will play again on ending.
any ideas?
here the code:
Security.allowDomain("www.youtube.com");
var my_player:Object;
var my_loader:Loader = new Loader();
my_loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3&loop=1;"));
my_loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
function onLoaderInit(e:Event):void{
addChild(my_loader);
my_player = my_loader.content;
my_player.addEventListener("onReady", onPlayerReady);
}
function onPlayerReady(e:Event):void{
my_player.setSize(300,300);
my_player.loadVideoById("indux8D-SoA&loop=1;",0);
my_player.mute();
}
thanks a lot!
You should use "onStateChange" event listener for your my_player object.
And check when it pass 0 or YT.PlayerState.ENDED.
my_player.addEventListener("onStateChange", onPlayerStateChange);
function onPlayerStateChange(event:Event):void {
if(Object(event).data==0){
//reload, restart, rewind and so on code here;
}
}
Alternatively you could load youtube playlist with one video using getPlaylist()and use
setLoop method to loop playback of this "list"
my_player.setLoop(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

Stopping the sound of a child swf

The timeline of my child swf, there are layers with specific sound files in them to be in sync with my animation. Now the problem arises when I try to import this swf into my main flash website using a loader and the sound will continuously play everytime the button is clicked in the parent. My question is how do I get the sound to completely clear itself and restart from frame 0 of the child swf upon every click of the button that loads the child into the loader within the parent.
var myLoader:Loader = new Loader();// create a new instance of the Loader class
var project1:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Greeting_Card/GreetingCard.swf");
var project2:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Landscape/Landscape.swf");
var project3:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/SpaceInvadersTribute/Main.swf");
var project4:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/RandomImageProducer/RndImgProd.swf");
//var project5:URLRequest = new URLRequest("Projects/Q1/Flash_Projects/Tutorial/Main.swf");
var project6:URLRequest=new URLRequest("Projects/Q1/Flash_Projects/Soundboard/Main.swf");
btnQ1P1.addEventListener(MouseEvent.CLICK,Greeting);
btnQ1P2.addEventListener(MouseEvent.CLICK,landscape);
btnQ1P3.addEventListener(MouseEvent.CLICK, tribute);
btnQ1P4.addEventListener(MouseEvent.CLICK, slideshow);
//btnQ1P5.addEventListener(MouseEvent.CLICK, tutorial);
btnQ1P6.addEventListener(MouseEvent.CLICK, soundboard);
addChild(myLoader);
function Greeting(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project1);
myLoader.x=550;
myLoader.y=130;
}
//Errors with Sound clips
function landscape(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project2);
myLoader.x=440;
myLoader.y=130;
}
function tribute(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project3);
myLoader.x=550;
myLoader.y=170;
}
//Errors with slideshow Code!
function slideshow(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project4);
myLoader.x=530;
myLoader.y=130;
}
//function tutorial(event:MouseEvent):void{
//SoundMixer.stopAll();
//myLoader.unload();
//myLoader.load(project5);
//myLoader.x = 440;
//myLoader.y = 130;
//}
function soundboard(event:MouseEvent):void {
SoundMixer.stopAll();
myLoader.load(project6);
myLoader.x=550;
myLoader.y=130;
}
I had this same problem.
You need to change the sound from "Event" to "Stream". In Flash Professional, this can be accessed from the properties tab of the sound on the timeline. Click on the sound, and then go to the Properties tab and change the type from Event to Stream.
Now, the sound will stop when the timeline is stopped.