星期四, 3月 31, 2011

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

7 則留言:

匿名 提到...

讚啦!正點~謝謝你囉

匿名 提到...

感謝! 但想請問若想播放hd影片的話(不開啟功能列)該如何設定呢?謝謝

perr 提到...

建議你參考Google的設定資訊:http://code.google.com/intl/zh-TW/apis/youtube/flash_api_reference.html#Playback_quality,使用player.setPlaybackQuality()功能。

匿名 提到...

請問要怎麼設定他的座標呢....

沒放座標他都一直漂在上面 很困擾呢=__=

perr 提到...

把這段code放進MovieClip,再控制它就行了。

Xunyu Design 提到...

想請問,如何讓它也能夠有全螢幕的功能?謝謝。

匿名 提到...

您好! 我參考了您的語法後 成功了 但有個問題想要請教您,我如何再跳場景時 停止他的聲音,因為我跳場景時 雖然場景換了,但是音樂卻還在跑,也就是他的物件還在執行,再麻煩指導! 謝謝!