在Flash中插入YouTube影片

最近因為工作上有需求,需要再在Flash裡面插入Youtube影片,於是上網找找到了相關的google code API,順便就記一下當作筆記也提供給有需要的人

Ggoogle code API原文出處:http://code.google.com/intl/zh-TW/apis/youtube/flash_api_reference_as2.html#Examples

基本上一連進去就會看到他建議你用Action Script 3 撰寫,如果你已經上手的畫建議用AS3,但是如果因為任何理由需要用AS2來寫目前還是可行的,本篇以AS2的為主,AS3的教學請參照這篇原文說明

首先開一個AS2的Flash檔,並在第一個影格上輸入下面的程式碼:

// 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.addEventListener(“onStateChange”, onPlayerStateChange);
        ytplayer.addEventListener(“onError”, onPlayerError);
        clearInterval(loadInterval);
    }
}

function onPlayerStateChange(newState:Number) {
    trace(“New player state: “+ newState);
}

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(“http://www.youtube.com/v/BpWM0FNPZSs“, ytplayer);

=======

上面最後一行標紅色的地方是讓使用者可以更改的部分,請到你想插入的youtube網址上截取,以上面的原始碼為例影片來源是http://www.youtube.com/watch?v=BpWM0FNPZSs,就是取用watch?v=後方的那串BpWM0FNPZSs,官方範例下載在此,其他部分就請各位看官自行發揮囉

p.s:範例使用的youtube連結是小樽的強者學弟劉邦耀所製作的Deadline~粉有名喔,也上過電視,在此註明一下p