読者です 読者をやめる 読者になる 読者になる

YoutubeAPIを使ってAIRなAndroidで動画を再生する方法

AIR Android Flex

YoutubeAPIを使ってAIRAndroidで動画を再生をしてみました。

ソースはこちら。

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" title="YoutubeAPI on AIR Android"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        creationComplete="view1_creationCompleteHandler(event)"
        >
    <fx:Script>
        <![CDATA[
            import flash.display.Loader;
            import mx.collections.ArrayCollection;
            import mx.events.FlexEvent;
            import mx.events.IndexChangedEvent;
            import mx.rpc.events.FaultEvent;
            import mx.rpc.events.InvokeEvent;
            import mx.rpc.events.ResultEvent;
            private var player:Object;
            private var loader:Loader;
            protected function view1_creationCompleteHandler(event:FlexEvent):void
            {
                loader=new Loader();
                loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
                loader.load(new URLRequest("http://www.youtube.com/apiplayer?version=3"));
            }
            private function onLoaderInit(event:Event):void
            {
                videoHolder.addChild(loader);
                loader.content.addEventListener("onReady", onPlayerReady);
                loader.content.addEventListener("onError", onPlayerError);
                loader.content.addEventListener("onStateChange", onPlayerStateChange);
                loader.content.addEventListener("onPlaybackQualityChange", onVideoPlaybackQualityChange);
            }
            private function onPlayerReady(event:Event):void
            {
                traceArea.text+="player ready:" + Object(event).data + "\r";
                player=loader.content;
                player.setPlaybackQuality( "small" );
                player.setSize(480,320);
                VidSelection.selectedIndex=0;
                VidSelection.dispatchEvent(new IndexChangeEvent( IndexChangedEvent.CHANGE , true , false ) );
            }
            private function cueVideo(event:Event):void
            {
                traceArea.text+="switch to:" + event.target.selectedItem.label + "\r";
                player.cueVideoById(event.target.selectedItem.data);
            }
            private function onPlayerError(event:Event):void
            {
                traceArea.text+="player error:" + Object(event).data + "\r";
            }
            private function onPlayerStateChange(event:Event):void
            {
                traceArea.text+="player state:" + Object(event).data + "\r";
            }
            private function onVideoPlaybackQualityChange(event:Event):void
            {
                traceArea.text+="video quality:" + Object(event).data + "\r";
            }
            protected function hts_resultHandler(event:ResultEvent):void
            {
            }
            protected function hts_faultHandler(event:FaultEvent):void
            {
            }
            protected function hts_invokeHandler(event:mx.rpc.events.InvokeEvent):void
            {
            }
            protected function playBtn_clickHandler(event:MouseEvent):void
            {
                player.playVideo();
            }
            protected function stopBtn_clickHandler(event:MouseEvent):void
            {
                player.stopVideo();
            }
            protected function pauseBtn_clickHandler(event:MouseEvent):void
            {
                player.pauseVideo();
            }
        ]]>
    </fx:Script>
    <fx:Declarations>
        <s:ArrayCollection id="listArr">
            <fx:Object data="fR-IIj4YClI" label="Be My Last"/>
            <fx:Object data="P8EdOtFrc5w" label="COLORS"/>
        </s:ArrayCollection>
    </fx:Declarations>
    <s:layout>
        <s:VerticalLayout gap="10"/>
    </s:layout>
    <s:HGroup width="100%" verticalAlign="middle" paddingLeft="10" paddingRight="10" paddingTop="10" paddingBottom="10">
        <s:Label text="動画選択:"/>
        <s:DropDownList id="VidSelection" width="100%" height="60" change="cueVideo(event)"
                        cornerRadius="10" dataProvider="{listArr}" prompt="動画選択" selectedIndex="0"/>
    </s:HGroup>
    <mx:UIComponent id="videoHolder" width="480" height="320" />
    <s:HGroup width="100%" height="78" paddingLeft="10" paddingRight="10">
        <s:Button label="play" id="playBtn" click="playBtn_clickHandler(event)" width="100%"/>
        <s:Button label="pause" id="pauseBtn" click="pauseBtn_clickHandler(event)" width="100%"/>
        <s:Button label="stop" id="stopBtn" click="stopBtn_clickHandler(event)" width="100%"/>
    </s:HGroup>
    <s:TextArea id="traceArea" width="100%" height="100%"/>
</s:View>

ただ、動画の再生が思ったよりも思いので、チューニングやら必要そうです。