星期四, 3月 31, 2011

AS2: 用Flash播放YouTube裡的影片[一定得用控制面版版]

至於AS2控制YouTube影片的部份,Google也是有提供AS2 For YouTube Video的參考資料,但目前看來,似乎無法不使用Google提供的Video控制面版…
//指定YouTube Video ID序號, 例如:YHLWdb55iic
var 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);

1 則留言:

KY 提到...

您好~AS2的部分無法測試成功
但是AS3的部分是可以的!