使用AS3和Youtube API在flash电影中单击按钮播放youtube

时间:2014-08-06 11:07:02

标签: actionscript-3 flash youtube

请跟我一起,因为我是动作脚本的新手。我正在尝试制作一个小型flash电影,当在电影中点击一个按钮时,会显示一个嵌入的YouTube视频。

构建电影很好,因为我对基本的Flash动画有一些经验,并且我已经按照教程here成功地在加载时嵌入视频,并在按钮时播放它(不是单击隐藏视频。

我已经尝试移动动作脚本以在时间轴下进一步加载视频,这导致了错误,并且已经摆弄了点击事件以仅向下显示,但两者都未成功。

我想要达到的目标是: 短暂的闪光电影,以“播放视频”的帧结束,单击时,帧消失,视频开始播放。

对正确方向的任何帮助表示赞赏。

谢谢,

1 个答案:

答案 0 :(得分:0)

粗略的例子:

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

更多信息:https://developers.google.com/youtube/flash_api_reference#Playback_controls