Menu

在Flash中插入YouTube影片

2011-01-30 1 Min Read

最近因為工作上有需求,需要再在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

Written By

創新科技研發 / 遊戲化 (Gamification) 策略 / 互動程式開發 / 一點點動畫製作 / 一些些特效後製 / 一咪咪網站設計 / 一滴滴…

5 Comments

  1. 黃恩玖表示:

    請問一下~這個插入youtube
    能不能自定那個影片的位置啊?

  2. 小良表示:

    您好!!
    非常讚的一篇文章
    請問版大
    不知道是否有可以把插入之影片
    可以自己做視窗調整及位置擺放??

  3. 小良表示:

    請問插入的影片可以自由調整位置及大小嗎

  4. alen表示:

    請問怎樣更改影片品質以及影片大小和位置呢?
    謝謝

    1. sky表示:

      影片品質取決於Youtube的壓縮你無法更改,除非你自己有空間唷,大小和位置改程式碼

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料