//指定YouTube Video ID序號, 例如:YHLWdb55iicvar myVideoID:String = "9evx8PZEMKU";
//自動播放:true, 不自動播放:false
var autoPlayVideo:Boolean = false;
//重覆播放:true, 不重覆播放:false
var loopVideoState:Boolean = false;
//聲音大小,最大100,最小:0 (無聲)
var myVolume:Number = 30;
//有聲:false, 無聲:true
var muteVideoState:Boolean = false;
//指定影片尺寸, 寬(videoWidth), 高(videoHeight)
var videoWidth:Number = 330;
var videoHeight:Number = 245;
//Below Functions Are Edited By Perr Tang
var autoPlayState:String;
if (autoPlayVideo == true) {
autoPlayState = "&autoplay=1";
} else {
autoPlayState = "";
}
var myVideoURL:String;
if (useControlPanelState == true) {
myVideoURL = "http://www.youtube.com/v/" + myVideoID + autoPlayState;
} else if (useControlPanelState == false) {
myVideoURL = "http://www.youtube.com/apiplayer?version=3&video_id=" + myVideoID+autoPlayState;
}
Security.allowDomain("www.youtube.com");
// create a MovieClip to load the player into
var ytplayer:MovieClip = _root.createEmptyMovieClip ("ytplayer", 1);
// create a listener object for the MovieClipLoader to use
var ytPlayerLoaderListener:Object = {onLoadInit:function () {
// When the player clip first loads, we start an interval to
// check for when the player is ready
loadInterval = setInterval (checkPlayerLoaded, 250);
}};
var loadInterval:Number;
function checkPlayerLoaded ():Void {
// once the player is ready, we can subscribe to events, or in the case of
// the chromeless player, we could load videos
if (ytplayer.isPlayerLoaded ()) {
ytplayer.setSize (videoWidth,videoHeight);//指定影片大小
trace ("width= " + videoWidth + " and height= " + videoHeight);
ytplayer.setVolume (myVolume);
if (muteVideoState == true) {
ytplayer.mute ();
}
ytplayer.addEventListener ("onStateChange",onPlayerStateChange);
ytplayer.addEventListener ("onError",onPlayerError);
clearInterval (loadInterval);
}
}
function onPlayerStateChange (newState:Number) {
trace ("New player state: " + newState);
if (newState == "0") {
if (loopVideoState == true) {
ytplayer.playVideo ();
} else {
ytplayer.stopVideo ();
}
}
}
function onPlayerError (errorCode:Number) {
trace ("An error occurred: " + errorCode);
}
// create a MovieClipLoader to handle the loading of the player
var ytPlayerLoader:MovieClipLoader = new MovieClipLoader ();
ytPlayerLoader.addListener (ytPlayerLoaderListener);
// load the player
ytPlayerLoader.loadClip (myVideoURL,ytplayer);
//ytPlayerLoader.loadClip("http://www.youtube.com/v/9evx8PZEMKU?version=3&loop=1&playlist=9evx8PZEMKU", ytplayer);
星期四, 3月 31, 2011
AS2: 用Flash播放YouTube裡的影片[一定得用控制面版版]
至於AS2控制YouTube影片的部份,Google也是有提供AS2 For YouTube Video的參考資料,但目前看來,似乎無法不使用Google提供的Video控制面版…
AS3: 用Flash播放YouTube裡的影片[加強版]
後來發現,人的需求是愈來愈高,一下子要這個功能,一下子又要那個功能…所以我把目前遇到所有「想要」的功能,寫成了一個ActionScript 3.0的程式碼,現在應該都有了吧!~~
//指定YouTube Video ID序號, 例如:YHLWdb55iic
var myVideoID:String = "YHLWdb55iic";
//自動播放:true, 不自動播放:false
var autoPlayVideo:Boolean = true;
// 用控制面版:true, 不用控制面版:false
var useControlPanelState:Boolean = false;
//重覆播放:true, 不重覆播放:false
var loopVideoState:Boolean = true;
//聲音大小,最大100,最小:0 (無聲)
var myVolume:Number = 10;
//有聲:false, 無聲:true
var muteVideoState:Boolean = false;
//指定影片尺寸, 寬(videoWidth), 高(videoHeight)
var videoWidth:Number = 300;
var videoHeight:Number = 215;
//Below Functions Are Edited By Perr Tang
var autoPlayState:String;
if(autoPlayVideo == true){
autoPlayState = "&autoplay=1";
}else{
autoPlayState = "";
}
var myVideoURL:String;
if(useControlPanelState == true){
myVideoURL = "http://www.youtube.com/v/" + myVideoID+"?version=3"+autoPlayState;
}else if(useControlPanelState == false){
myVideoURL = "http://www.youtube.com/apiplayer?version=3&video_id=" + myVideoID+autoPlayState;
}
// 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.(設定允許跨網域連到youtube.com)
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&video_id=YHLWdb55iic&autoplay=1"));
loader.load(new URLRequest(myVideoURL));
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(videoWidth, videoHeight);//指定影片大小
player.setVolume(myVolume);
if(muteVideoState == true){
player.mute();
}
}
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);
if(Object(event).data == "0"){
if(loopVideoState == true){
player.playVideo();
}else{
player.stopVideo();
}
}
}
function onVideoPlaybackQualityChange(event:Event):void {
// Event.data contains the event parameter, which is the new video quality
trace("video quality:", Object(event).data);
}
訂閱:
文章 (Atom)