Loop youtube clip on flash As3 - actionscript-3

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

Related

Play youtube upon clicking button in flash movie using AS3 and Youtube API

Please bear with me as I'm brand new to action script. I'm trying to build a small flash movie, that when a button is clicked in the movie, an embeded youtube video is displayed.
Building the movie is fine as I've some experience with basic flash animations, and I've followed the tutorial here to successfully embed a video on load and have it play when a button (that doesn't hide the video) is clicked.
I've tried moving the action script to load the video further down the timeline, which has caused errors, and have fiddled around with the click event to only display further down the, both have been unsuccessful though.
What I'd like to achieve is:
Short flash movie that ends with a frame that reads "Play Video", when that's clicked, the frame disappears and the video starts to play.
Any help in the right direction is appreciated.
Thanks,
Rough example:
// 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);
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, 360);
}
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);
}
more info: https://developers.google.com/youtube/flash_api_reference#Playback_controls

How to loop SWF file loaded with Loader?

I want to loop a swf file that has been loaded with via the Loader class in AS3.
My code looks as following:
public class MyLoader extends MovieClip {
public function MyLoader() {
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest("external-movie.swf");
myLoader.load(url);
myLoader.contentLoaderInfo.addEventListener("complete", function() {
});
addChild(myLoader);
}
}
From what I understand the Loader has no event for when the embedded movie is finished? Is there a way to figure that out? It must not be a AS3 implementation. I just want a movie that has been exported from Indesign to run in a loop. Thanks in advance
Especially when you have little experience programming you should avoid dirty shortcuts as they'll only get you a lot of trouble. So avoid anonymous function and avoid using string in place of static event variables.
This being said, if your loaded movie has its own timeline then it will be converted into a MovieClip. Also that movie is not embedded but loaded and that's a big difference.
Keep a reference of that movie and the loader:
private var theLoadedMovie:MovieClip;
private var myLoader:Loader;
Listen for the INIT event instead of the COMPLETE event (movies with timeline start to play when their first frame is loaded "INIT", the COMPLETE event fires when the whole movie is loaded).
myLoader = new Loader();
var url:URLRequest = new URLRequest("external-movie.swf");
myLoader.load(url);
myLoader.contentLoaderInfo.addEventListener(Event.INIT, handleInit);
In your handleInit method keep a reference of that movie:
theLoadedMovie = myLoader.content as MovieClip;
addChild(theLoadedMovie);
theLoadedMovie.addEventListener(Event.ENTERFRAME, handleEnterFrame);
in your handleEnterFrame method check the movie progress to know when it has ended:
if(theLoadedMovie.currentFrame == theLoadedMovie.totalFrames)
{
//movie has reached then end
}

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

AS3 Code - Sound not playing

I'm trying to play sound in as3 code using an external mp3 file.
So here's the code I am using:
private function playSound():void
{
trace("loading sound");
var mySound:Sound = new Sound();
mySound.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
mySound.load(new URLRequest("Menu.mp3"));
mySound.play();
trace("playing sound");
}
private function handleIOError(evt:IOErrorEvent):void
{
//handle error if needed
}
The music just doesn't play at all.
The traces "loading sound" and "playing sound" appear so the code is being run.
The mp3 file Menu.mp3 is in the same folder as the .fla file used to run the project. Is this the correct directory? I tried moving it around but still couldnt play the sound.
Any help will be appreciated, thanks!
I have a few suggestions that might help:
Declare mySound as a class level property. The garbage collector might be disposing of the variable prematurely since it is local.
mySound.play() returns a SoundChannel object. Try storing this in a class level property.
Add an event listener to the sound for Event.COMPLETE, right before loading the sound. Try playing the sound after this event occurs. As it is, you might be trying to play the sound before it has loaded.
private var mySound:Sound;
private var mySoundChannel:SoundChannel;
private function playSound():void
{
mySound = new Sound();
mySound.addEventListener(IOErrorEvent.IO_ERROR, handleIOError);
mySound.addEventListener(Event.COMPLETE, handleLoadCompletion);
mySound.load(new URLRequest("Menu.mp3"));
}
private function handleLoadCompletion(evt:Event):void
{
mySoundChannel = mySound.play();
}
private function handleIOError(evt:IOErrorEvent):void
{
//handle error if needed
}
Edit:
After reviewing the docs, I think that suggestion 3 isn't necessary.

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.