YoutubeなAIRアプリを作ってみました。

サンプルとしてYoutubeAIRアプリを作ってみました。
こんな感じ

アプリのダウンロード

「Ctrl + w」もしくは右上のCloseボタンでアプリを終了することができます。
それにしてもYoutubeのページダウンロードがすこぶる重い。
今回の勉強は正規表現でflvなURLを引っこ抜いて作成です。

//youtube のページからflvのURLを作成
private function accessSuccess(evt:Event):void{
    myProgress.visible = false;
    var startIndex:int =0;
    var endIndex:int   =0;
    var pattern:RegExp = /=&t=/;
    var myArr:Array = pattern.exec(watchLoader.data);
    startIndex = myArr["index"];
    pattern = /&hl=/;
    myArr = pattern.exec(watchLoader.data);
    endIndex = myArr["index"];
    var tmpArr:Array = nowurl.split("?v=");
    var flvurl:String = "http://youtube.com/get_video.php?video_id="
                        + tmpArr[1] + "&t="
                        + watchLoader.data.substring( startIndex +4 , endIndex );
    myVideo.source = flvurl;
}

正規表現を二回使っているけど、うまくやればたぶん、一回でいけると思う。
全体のプログラムソースはこちら

<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"
    verticalScrollPolicy="off"
    showGripper="false" showInAutomationHierarchy="false" showStatusBar="false" showTitleBar="false"
    borderThickness="0"  closing="closeHandler()" keyDown="onKey(event)"
    creationComplete="init()" width="100%" height="100%" backgroundGradientAlphas="[1.0, 1.0]" backgroundGradientColors="[#6F6F6F, #000000]" xmlns:jirox="net.jirox.*">
<mx:Script>
<![CDATA[
//初期化関数
private var so:SharedObject;
private function init():void{
    this.setFocus();
    this.nativeWindow.x = 0;
    this.nativeWindow.y = 0;
    this.nativeWindow.width  = Capabilities.screenResolutionX;
    this.nativeWindow.height = Capabilities.screenResolutionY;
    //シェアードオブジェクトゲット
    so = SharedObject.getLocal("kyoutubeAir");
    if( so.data.word ){
        inputTxt.text = so.data.word;
    }else{
    }
}
//現在のウィンドウ情報を保存しとく
private function closeHandler():void{
    //so.flush();
}
//youtube検索
private function onSearch():void{
    if( inputTxt.text != "" ){
        so.data.word = inputTxt.text;
        hts.send({"word":inputTxt.text});
    }
}
//検索完了イベント
private function onResult():void{
}
//動画リストからクリックされたとき
private function onClick():void{
    playMovie();
}
//youtube の URL から flvURLをゲット
private var watchLoader:URLLoader = new URLLoader();
private var nowurl:String;
public function playMovie():void{
    myProgress.visible = true;
    var url:String = tl.selectedItem.url;
    nowurl = url;
    var watchURL:URLRequest = new URLRequest(url + "&fmt=6" );
    watchURL.method = "GET";
    watchLoader = new URLLoader();
    watchLoader.addEventListener( Event.COMPLETE , accessSuccess );
    watchLoader.addEventListener(ProgressEvent.PROGRESS , function():void{
        myProgress.label = watchLoader.bytesLoaded + "/" + watchLoader.bytesTotal;;
    });
    watchLoader.load( watchURL );
}
//youtube のページからflvのURLを作成
private function accessSuccess(evt:Event):void{
    myProgress.visible = false;
    var startIndex:int =0;
    var endIndex:int   =0;
    var pattern:RegExp = /=&t=/;
    var myArr:Array = pattern.exec(watchLoader.data);
    startIndex = myArr["index"];
    pattern = /&hl=/;
    myArr = pattern.exec(watchLoader.data);
    endIndex = myArr["index"];
    var tmpArr:Array = nowurl.split("?v=");
    var flvurl:String = "http://youtube.com/get_video.php?video_id="
                        + tmpArr[1] + "&t="
                        + watchLoader.data.substring( startIndex +4 , endIndex );
    myVideo.source = flvurl;
}
//動画再生終わったので次の動画
private function nextMovie():void{
    if( tl.selectedIndex >= hts.lastResult.item.length -1 ){
        tl.selectedIndex = tl.selectedIndex + 1;
    }else{
        tl.selectedIndex = 0;
    }
    playMovie();
}
//アプリをキーボードから閉じる
private function onKey(e:KeyboardEvent):void{
    if( e.charCode == 119 &&  e.ctrlKey ){
        this.nativeWindow.close();
    }
}
[Bindable]private var canvasVisible:Boolean = true;
private function setCanvas():void{
    if( canvasVisible ){
        canvasVisible = false;
        hideCanvas.play([myCanvas]);
    }else{
        showCanvas.play([myCanvas]);
        canvasVisible = true;
    }
}
import mx.effects.easing.*;
]]>
</mx:Script>
<mx:Style>
TileList{
   alternatingItemColors: #999999, #cccccc;
   rollOverColor: #666666;
   textRollOverColor: #ffffff;
   selectionColor: #333333;
   color: #333333;
   textSelectedColor: #ffffff;
}
Button {
   cornerRadius: 0;
   textIndent: 0;
   highlightAlphas: 0.68, 0.45;
   fillAlphas: 1, 0.94, 0.75, 0.65;
   fillColors: #333333, #ffffff, #000000, #666666;
   color: #000000;
   themeColor: #009dff;
}
</mx:Style>
<!--##########  ##########-->
<jirox:AirAutoUpdater url="http://moeten.info/air/kyoutubeAir/version.xml"/>
<!--##########  ##########-->
<mx:HTTPService id="hts"
    url="http://moeten.info/maidcafe/?m=moesearch_new&amp;type=youtube&amp;ptype=xml"
    resultFormat="e4x" result="onResult()" showBusyCursor="true"/>
<mx:Move id="hideCanvas" xFrom="0" xTo="{-myCanvas.width+30}"/>
<mx:Move id="showCanvas" xTo="0" xFrom="{-myCanvas.width+30}"/>
<!--##########  ##########-->
<mx:VideoDisplay id="myVideo" x="0" y="0" width="100%" height="100%" complete="nextMovie()"/>
<mx:ProgressBar x="0" y="0" width="100%" indeterminate="true" themeColor="#092435" label="now Loading..." id="myProgress"
    trackHeight="10" showEffect="WipeRight" hideEffect="WipeRight" visible="false" color="0xcccccc" labelPlacement="right" textAlign="right"/>
<mx:Canvas visible="{tl.selectedItem}" x="480" y="858" width="468" height="132"
    cornerRadius="10" borderThickness="3" borderColor="0x999999" borderStyle="solid"
    backgroundAlpha="0.4" backgroundColor="0x333333" bottom="10" right="10" alpha="0.8" showEffect="Fade">
    <mx:Image x="10" y="10" width="130" height="106" source="{tl.selectedItem.img}"/>
    <mx:TextArea x="148" y="36" width="310" height="80" backgroundAlpha="0"
        editable="false" selectable="false"
        htmlText="{tl.selectedItem.comment}" textAlign="left" color="0x999999" borderThickness="0" borderStyle="solid"/>
    <mx:Label x="148" y="10" text="{tl.selectedItem.title}" width="310" color="0x999999" textAlign="left"/>
</mx:Canvas>
    <mx:Canvas id="myCanvas" x="0" y="0" width="250" height="100%">
        <mx:Button x="162" y="18" label="検索" click="onSearch()"/>
        <mx:TextInput id="inputTxt" text="" x="10" y="18" width="144" enter="onSearch()"
            backgroundColor="#cccccc" color="#000000"
            borderThickness="1" borderColor="0x000000" borderStyle="solid"/>
        <mx:TileList id="tl" x="10" y="48" width="200" height="100%" alpha="0.6" backgroundAlpha="0.5"
            rowHeight="50" columnWidth="200" itemClick="onClick()"
            dataProvider="{hts.lastResult.item}" itemRenderer="thumRender"/>
        <mx:LinkButton x="212" y="15" label=">" width="34" height="25" click="setCanvas()" fontSize="12" color="0x999999"/>
    </mx:Canvas>
    <mx:LinkButton x="876" y="10" label="Close" click="{this.nativeWindow.close()}" color="0x999999" right="0"/>
</mx:WindowedApplication>